cancel
Showing results for 
Search instead for 
Did you mean: 

Add custom CSS/JS to CMS page layout

Add custom CSS/JS to CMS page layout

Hi. I’m following this guide on how to create a custom layout that can be applied to multiple cms pages. I want to create a new layout that will apply to a group of pages instead of creating multiple cms_page_view_selectable_…xml files for each page, and I want to use a custom css and js on these pages.

The example from the guide shows how to add a new block to the new layout, but it doesn’t mention how to add custom css or js file.

I’m adding these two files:

 

app/design/frontend/<VendorName>/<ThemeName>/Magento_Theme/page_layout/1-column-custom-footer.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">
    <update handle="1column"/>
    <head>
        <css src="css/pages/custom-footer.css" />
    </head>
</layout>

 

app/design/frontend/<VendorName>/<ThemeName>/Magento_Theme/layouts.xml

<?xml version="1.0" encoding="UTF-8"?>
<page_layouts xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/PageLayout/etc/layouts.xsd">
    <layout id="1-column-custom-footer">
        <label translate="true">1 Column with Custom Footer</label>
    </layout>
</page_layouts>

Unfortunately this doesn’t add new css file to the page, which makes me think that I can’t add css to <layout …/>, only to <page …/>.

 

How can I add custom js and css to a new layout?

Thanks.

3 REPLIES 3

Re: How to add custom css and js to a custom page layout?

Hello @imagendemi8edd 

 

Create the Custom Layout XML

In view/frontend/layout/your_custom_layout.xml, define the layout and specify the JS and CSS files:

 

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <head>
        <css src="Custom_Module::css/custom.css"/>
        <js src="Custom_Module::js/custom.js"/>
    </head>
    <body>
        <!-- Add your layout content here -->
    </body>
</page>

 

Create the Custom JS and CSS Files

Add your custom CSS in view/frontend/web/css/custom.css and your custom JS in view/frontend/web/js/custom.js.

php bin/magento setup:static-content:deploy
php bin/magento cache:clean
php bin/magento cache:flush

Hope it helps !

If you find our reply helpful, please give us kudos.

 

A Leading Magento Development Agency That Delivers Powerful Results, Innovation, and Secure Digital Transformation.

 

WebDesk Solution Support Team

Get a Free Quote | | Adobe Commerce Partner | Hire Us | Call Us 877.536.3789

 

Thank You,


WebDesk Solution Support Team
Get a Free Quote | Email | Adobe Commerce Partner | Hire Us | Call Us 877.536.3789


Location: 150 King St. W. Toronto, ON M5H 1J9

Re: How to add custom css and js to a custom page layout?

Hello @johnwebdes40a5,

 

Thanks for your reply. Here’s what I’m trying to achieve:

 

Screenshot 2024-11-04 at 11.01.10.png

 

I want to create a layout that can be selected from admin and applied to multiple pages, and that layout should have custom css and js files.

Creating custom layout like you’re suggesting won’t add it to admin.

Re: Add custom CSS/JS to CMS page layout

Hello @imagendemi8edd,

It can be majorly used when you want to add any custom CSS and JS to your custom CMS page.

Magento provides us the following layout:

-> Empty

-> 1 column

-> 2 columns with left bar

-> 2 columns with right bar

-> 3 columns

If you want to create your own customized layout for the cms pages or some specific pages then you need to focus on the following steps:

Steps to Create a Custom Layout with Custom CSS and JS in Magento 2

Step 1: Define a New Layout

First, we’ll create the layouts.xml file to define a custom layout.

File: app/code/Milople/CustomLayout/view/frontend/layouts.xml

<?xml version="1.0" encoding="UTF-8"?>

<page_layouts xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

              xsi:noNamespaceSchemaLocation="urn:magento:framework:View/PageLayout/etc/layouts.xsd">

    <layout id="customlayout">

        <label translate="true">Custom CMS Page Layout</label>

    </layout>

</page_layouts>

Step 2: Create a Layout File

 

The layout file specifies the structure and containers.

File: app/code/Milople/CustomLayout/view/frontend/page_layout/customlayout.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">

    <update handle="3columns"/>

    <referenceContainer name="page.wrapper">

        <container name="footer-custom" as="footer-custom" after="footer" label="Custom Footer"

                   htmlTag="footer" htmlClass="custom-footer-section">

            <container name="footer-content" as="footer-content" htmlTag="div" htmlClass="custom-footer-content">

                <block class="Magento\Framework\View\Element\Template"

                       name="footer.bugs" template="Magento_Theme::html/bugreport.phtml"/>

            </container>

        </container>

    </referenceContainer>

</layout>

 

Step 3: Define a Custom Route

Now, let’s add a route for the custom module.

File: app/code/Milople/CustomLayout/etc/frontend/routes.xml

 

<?xml version="1.0" ?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

        xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">

    <router id="standard">

        <route id="customlayout" frontName="customlayout">

            <module name="Milople_CustomLayout"/>

        </route>

    </router>

</config>

 

Step 4: Controller for the Custom Page

 

We need to create an action to render the layout.

File: app/code/Milople/CustomLayout/Controller/Index/Index.php

<?php

namespace Milople\CustomLayout\Controller\Index;

use Magento\Framework\App\Action\Action;

use Magento\Framework\App\Action\Context;

use Magento\Framework\View\Result\PageFactory;

class Index extends Action

{
    protected $resultPageFactory;

    public function __construct(

        Context $context,

        PageFactory $resultPageFactory
    ) {

        parent::__construct($context);

        $this->resultPageFactory = $resultPageFactory;

    }

    public function execute()

    {
        return $this->resultPageFactory->create();

    }
}


Step 5: Add CSS and JS to the Layout

 

This file adds the custom JS and CSS for the layout.

File: app/code/Milople/CustomLayout/view/frontend/layout/customlayout_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_configuration.xsd">

    <head>

        <css src="Milople_CustomLayout::css/custom_layout.css"/>

        <script src="Milople_CustomLayout::js/custom_layout.js"/>

    </head>

</page>


Step 6: Create CSS and JS Files

Add your custom CSS and JS.

CSS File: app/code/Milople/CustomLayout/view/frontend/web/css/custom_layout.css

.custom-footer-section {

    background-color: #f4f4f4;

    text-align: center;

    padding: 20px;

}

.custom-footer-content {

    font-size: 14px;

    color: #333;

}

JS File: app/code/Milople/CustomLayout/view/frontend/web/js/custom_layout.js

require(['jquery'], function ($) {

    'use strict';

    $(document).ready(function () {

        console.log("Custom Layout JS loaded!");

    });

});

Step 7: Add Event Observer

Now, add an observer to conditionally load the layout handle.

File: app/code/Milople/CustomLayout/etc/events.xml

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

        xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">

    <event name="layout_load_before">

        <observer name="milople_custom_layout" instance="Milople\CustomLayout\Observer\LayoutHandler"/>

</event>

</config>

Step 8: Create the Observer

This observer checks the page layout and adds the correct handle.

File: app/code/Milople/CustomLayout/Observer/LayoutHandler.php

<?php

namespace Milople\CustomLayout\Observer;

use Magento\Framework\Event\ObserverInterface;

use Magento\Framework\Event\Observer;

class LayoutHandler implements ObserverInterface

{

    public function execute(Observer $observer)

    {

        $layout = $observer->getData('layout');

        $update = $layout->getUpdate();

        $handles = $update->getHandles();

        if (in_array('customlayout_index_index', $handles)) {

            $update->addHandle('customlayout_index_index');

        }

    }

}

Now your custom layout page is ready with CSS and JS integrated!

 

If the issue is resolved, click Kudos and accept it as a solution.