Magento2 Layout – How to Add CSS to a CMS Page Using Layout Update XML

cmslayoutmagento2

I need to add a specific CSS file to a CMS page using Magento 2. I am wanting to achieve this by using the Layout Update XML area in the admin. However, I cannot figure out or find any working examples of just how to do this.

My home page CMS is the page I am trying to add the CSS to. I can add specific CSS files in the head of the entire site by calling them out in the default_head_blocks.xml. But in this case I need it to be used on ONLY the home page CMS.

It appears as though I should be using <referenceContainer> but after that I am not sure what to do.

Best Answer

The solution I found was to create an XML file specifically called cms_index_index.xml.
Inside that file I was able to simply call the css I needed

<?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/mycustom.css" after="-" />
    </head>
</page>