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"
        <css src="css/lpmdd/lpmdd.css" />
    <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"/>

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?


1 Answer 1

  1. Use <css src="Magento_Theme::css/lpmdd/lpmdd.css"/> instead of <css src="css/lpmdd/lpmdd.css"

  2. 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
  1. You can't declare a CSS file in page_layout file, you should declare it in a layout file.

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

  1. Finally, clear layout cache 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
    Commented Nov 9, 2022 at 12:35
  • Thank you for your answer but unfortunately it does not work
    – blind
    Commented 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
    Commented 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 file
    – blind
    Commented 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 BO
    – blind
    Commented Nov 9, 2022 at 14:00

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.