In vendor/magento/module-theme/view/base/templates/root.phtml:11 there is this code:

<html <?= /* @escapeNotVerified */ $htmlAttributes ?>>

It looks like we can use layout XML to add attributes to the <html> element, but I can't figure out how.

How is $htmlAttributes used?

Similar functionality

I presume it works in a similar way to adding attributes to the body or head, but I can't get it working with <html> at all.

    <attribute name="class" value="page-with-filter"/>
  • According to php storm its the only usage in all places. It doesn't seem to exist anywhere else. How come you're wanting to target the html and no the body? – andy jones Nov 13 '17 at 14:37
  • I'm not, someone asked how to add attributes to the <html> and the only method I could find is to hardcode it in root.phtml. But it looks like we should be able to do it with layout XML. Unless this was added to the template but the functionality was never added. – Ben Crook Nov 13 '17 at 14:42
  • Think thats the legit answer, the functionality doesn't seem to exist anywhere for it. – andy jones Nov 13 '17 at 14:57
  • Aye, if you want to add that as an answer and if no one adds a method in the next week or so I'll mark it as accepted as I can't find any uses either. – Ben Crook Nov 13 '17 at 15:41

Looks as though it isn't referenced anywhere, and that is the only usage of it. However I wouldn't recommend using the HTML tag to assign anything like this to, but you know this and assign them to body instead :)

I guess the only way to actually do it is edit that file. I'd like to say there is an xml way to do it, but at the moment....


Magento adding that class for body using below class Magento\Framework\View\Result\Page.php

  protected function render(HttpResponseInterface $response)
 'htmlAttributes' => $this->pageConfigRenderer->renderElementAttributes($config::ELEMENT_TYPE_HTML),

It is work same like you are adding for body

<?xml version="1.0"?>
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
        <attribute name="class" value="page-with-filter"/> 

Output: http://easycaptures.com/3327274226

  • it always returns html: This element is not expected. error – Piyush Nov 14 '17 at 4:52
  • This doesn't work for me either: Exception #0 (Magento\Framework\Config\Dom\ValidationException): Element 'html': This element is not expected. – Ben Crook Nov 14 '17 at 9:42
  • Check with production mode – Sunil Patel Nov 14 '17 at 9:52
  • I don't develop in production mode. – Ben Crook Nov 14 '17 at 10:47

This is working fine for me :)

    <attribute name="class" value="page-with-filter"/> 
  • why you did copy paste as asnwer? – Sunil Patel Nov 14 '17 at 6:09
  • Just informed this is working for me – Hitesh Koshti Nov 14 '17 at 9:09
  • Then like my comment and approved as solution – Sunil Patel Nov 14 '17 at 9:12
  • Doesn't work for me, it returns this error. Exception #0 (Magento\Framework\Config\Dom\ValidationException): Element 'html': This element is not expected. – Ben Crook Nov 14 '17 at 9:42

I use Magento 2.2 and it doesnt work for me either (dev mode), This is probably a bug. As a patch until this is fixed you can edit:


Add under root element (you'll see that the other schema tags of head/body/elements are also there):

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:include schemaLocation="urn:magento:framework:View/Layout/etc/elements.xsd"/>
    <xs:include schemaLocation="urn:magento:framework:View/Layout/etc/head.xsd"/>
    <xs:include schemaLocation="urn:magento:framework:View/Layout/etc/body.xsd"/>
    <!--ADD THE NEXT LINE-->
    <xs:include schemaLocation="urn:magento:framework:View/Layout/etc/html.xsd"/>

Then you should add under xs:complexType tag with name="layoutType" (around line 33) under 'xs:choice' tag >> the element tag for that html schema (htmlType):

<xs:complexType name="layoutType">
            Layout Type definition
    <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element ref="referenceContainer" minOccurs="0" maxOccurs="unbounded"/>
        <xs:element name="container" type="containerType" minOccurs="0" maxOccurs="unbounded"/>
        <xs:element ref="update" minOccurs="0" maxOccurs="unbounded"/>
        <xs:element ref="remove" minOccurs="0" maxOccurs="unbounded"/>
        <xs:element ref="move" minOccurs="0" maxOccurs="unbounded"/>
        <xs:element ref="block" minOccurs="0" maxOccurs="unbounded"/>
        <xs:element ref="referenceBlock" minOccurs="0" maxOccurs="unbounded"/>
        <xs:element name="body" type="bodyType" minOccurs="0" maxOccurs="unbounded"/>
        <xs:element name="head" type="headType" minOccurs="0" maxOccurs="unbounded"/>
        <!--ADD THE NEXT LINE-->
        <xs:element name="html" type="htmlType" minOccurs="0" maxOccurs="unbounded"/>

Then it should work by adding the the proper xml in any xml layout file, e.g.:

<?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">
        <attribute name="class" value="page-with-filter" />

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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