If you want to add class into HTMl tag then
you need to create event.xml file into app/code/NameSpace/ModuleName/etc/
<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="event_add_class" instance="NameSpace\ModuleName\Plugin\AddClass" shared="false" />
</event>
</config>
Then that class file
namespace NameSpace\ModuleName\Plugin;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer;
use Magento\Framework\View\Page\Config;
use Magento\Store\Model\StoreManagerInterface;
class AddClass implements ObserverInterface
{
protected $config;
protected $storeManager;
public function __construct(
Config $config,
StoreManagerInterface $storeManager
){
$this->config = $config;
$this->storeManager = $storeManager;
}
public function execute(Observer $observer){
$store = $this->storeManager->getStore();
$storeCode = $store->getCode();
$websiteCode = $store->getWebsite()->getCode();
$this->config->addBodyClass($storeCode);
$this->config->addBodyClass($websiteCode);
}
}
If you add class into body you can add using that way
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<attribute name="class" value="account"/>
</body>
</page>
Magento also doing same thing:-
https://github.com/magento/magento2/blob/2.3-develop/app/code/Magento/Customer/view/frontend/layout/customer_account.xmlng into my account section