0

I am trying to add custom CSS without creating a theme and these are my steps.

Step 1 I create a mycss.css file

body{
 background-color:green;
 }

and this is the path

app/code/vendor/module/view/frontend/web/css/mycss.css

Step 2 I create a test_index_index.xml file with this content

?xml version="1.0"?>
 <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_layout.xsd">
 <head>
 <css src="vendor_Module::css/mycss.
 </head> 
 <body>
 <referenceContainer name="content">
 <block class="vendor\Module\Block\class" template="vendor_Module::class.phtml"/>
 </referenceContainer> 
 </body> 
 </page>

Which is located at

app/code/vendor/module/view/frontend/layout/test_index_index.xml

Reference Magento 2: How to add css and js in custom module in magento 2?

Unfortunately, on my frontend nothing changes. Does anybody know why?

3
  • Why you just can't create a custom theme? It would make development a lot easier. Commented Feb 25, 2018 at 20:41
  • It was successful with the theme but I wanted to try to decrease the files and the complexity only to a css and a xml file because the only needed change is to margin a text field with 10 px. Commented Feb 27, 2018 at 9:57
  • Sooner or later you will need to do some more changes. There is no project where all you need is 10px margin. Also in my opinion, themes adds some clarity not complexity. Commented Feb 28, 2018 at 17:49

2 Answers 2

1

test_index_index.xml

<?xml version="1.0"?>
 <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_layout.xsd">
 <head>
 <css src="Vendor_Module::css/mycss.css"/>
 </head> 
 <body>
 <referenceContainer name="content">
 <block class="Vendor\Module\Block\Class" template="Vendor_Module::file.phtml"/>
 </referenceContainer> 
 </body> 
 </page>

php bin/magento cache:clean

php bin/magento setup:static-content:deploy -f

answered Feb 25, 2018 at 15:46
2
  • Thank you for the response! Question: for xsi:noNamespaceSchemaLocation="should I put my path leading up to that file and shouldn't Vendor be with a lower case? Commented Feb 25, 2018 at 16:33
  • Look my update please, more information about xml schema validation: devdocs.magento.com/guides/v2.0/extension-dev-guide/build/… Commented Feb 25, 2018 at 22:28
1
  1. Create _module.less file inside Vendor\Module\view\frontend\web\css\source\ and write below code inside the file:
& when(@media-common = true) {
 // write here your css 
}

Then run below commands:

  1. php bin/magento setup:upgrade
  2. php bin/magento setup:static-content:deploy -f
  3. php bin/magento cache:flush
answered Apr 25, 2019 at 11:10

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.