Is there a way to create my own CSS file that loads last in the cascade?
If so, how and where do I add my custom CSS file?
In order to do add custom css and load last, you must set up a custom theme.
--
app / design / frontend / [vendor] / [theme] / Magento_Theme / layout
app / design / frontend / [vendor] / [theme] / web / css
Create the following files:
app / design / frontend / [vendor] / [theme] / Magento_Theme / layout / default_head_blocks.xml
app / design / frontend / [vendor] / [theme] / web / css / local-m.css
app / design / frontend / [vendor] / [theme] / web / css / local-l.css
place this code within default_head_blocks.xml
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../vendor/magento/framework/Module/etc/module.xsd">
<head>
<css src="css/local-m.css" />
<css src="css/local-l.css" media="screen and (min-width: 768px)"/>
</head>
</page>
--
php bin/magento setup:static-content:deploy
In order to add custom css and load last
Follow the directory structure
app / code / vendor / modulename / view / frontend(for admin adminhtml) / web /css / filename.css
Add the css file path to the corresponding layout file as given below
<head>
<css src="Vendor_Module::css/filename.css"/>
</head>
<body>
....
</body>
Vendor_Module
is not working for me. Removing it working fine.
Commented
Mar 12, 2018 at 10:39
Vendor_Module
only it is not working.
Commented
Mar 13, 2018 at 6:02
You can add the media attribute which magento 2 will put to the end of the css in the head section. Setting a width of only 1px will enable it on all devices. default_head_blocks.xml will contain:
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<head>
<css src="css/styles-m.css"/>
<css src="css/styles-l.css" media="screen and (min-width: 768px)"/>
<css src="css/print.css" media="print"/>
<css src="css/custom.css" media="all and (min-width: 1px)"/>
</head>
</page>
Custom theme call css :
app/design/frontend/package/theme/Magento_Theme/layout/default_head_blocks.xml
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<head>
<css src="css/custom.css" />
</head>
</page>
Custom Module css call only limited module
app/code/Package/Module/view/frontend/layout/package_module_index.xml
Or Call Global Css in on website
app/code/Package/Module/view/frontend/layout/default.xml
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
<head>
<css src="Package_Module::css/custom.css"/>
</head>
<body>
<!-- Custom Code -->
</body>
</page>