I try to add a css file to a new layout for a cms page (magento 2): src/app/design/frontend/NAME/default/Magento_Theme/page_layout/lpmdd-without-header.xml
<?xml version="1.0" ?>
<layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_layout.xsd">
<head>
<css src="css/lpmdd/lpmdd.css" />
</head>
<update handle="1column"/>
<referenceContainer name="header.container" remove="true"/>
<referenceContainer name="breadcrumbs" remove="true" />
<referenceContainer name="page-bottom" remove="true" />
<referenceContainer name="reassurance-container" remove="true"/>
</layout>
I have created lpmdd.less located in src/app/design/frontend/NAME/default/Magento_Theme/web/css/lpmdd/lpmdd.less
I try to clean cache and running php bin/magento setup:static-content:deploy -f
And I try to change
<css src="Magento_Theme::css/lpmdd/lpmdd.css" />
but it doesn't work, do you have any idea?
Thanks.
1 Answer 1
Use
<css src="Magento_Theme::css/lpmdd/lpmdd.css"/>instead of<css src="css/lpmdd/lpmdd.css"Run the following commands to clear static content and re-deploy static content for frontend:
rm -rf var/view_preprocessed/pub/static/frontend/
rm -rf pub/static/frontend/
bin/magento setup:static-content:deploy -f --area frontend
- You can't declare a CSS file in
page_layoutfile, you should declare it in alayoutfile.
As you need to add a CSS file to a CMS page, you can create a selectable layout file for CMS pages, then declare the CSS file there. Take a look at this post https://magetu.com/how-to-add-a-selectable-layout-to-cms-page-in-magento-2
- Finally, clear
layoutcache type to see the result:bin/magento cache:clean layout
-
If this answer works as your expected, please mark it as accepted. That indicates your issue is solved and makes it easier for others with the same issue to find a verified answer.Tu Van– Tu Van2022年11月09日 12:35:53 +00:00Commented Nov 9, 2022 at 12:35
-
Thank you for your answer but unfortunately it does not workblind– blind2022年11月09日 13:29:35 +00:00Commented Nov 9, 2022 at 13:29
-
I tested it before posting the answer for you. Are you sure the site loads your CSS file after you added it to the layout xml?Tu Van– Tu Van2022年11月09日 13:32:08 +00:00Commented Nov 9, 2022 at 13:32
-
A css file it's create in pub/static/frontend .. Just the layout does not load the file there is no line in the header that matches my fileblind– blind2022年11月09日 13:43:54 +00:00Commented Nov 9, 2022 at 13:43
-
I need to add my css only for the layout in question and thus be able to use it when creating new pages in BOblind– blind2022年11月09日 14:00:58 +00:00Commented Nov 9, 2022 at 14:00