Search code examples
asp.net-mvcepiserverepiserver-8

CSS class on generated div element for EPiServer blocks


I have a ContentArea with a number of floating blocks. EPiServer automatically wraps each block in a div-element, which is necessary for the edit mode to function properly. So what is initially one div becomes three nested divs: content area, child element wrapper and the block view.

Is it possible to add CSS classes to the child element wrapper from the block view? So what is today:

div.ContentArea > div > div.my-class

becomes:

div.ContentArea > div.my-class

Solution

  • I ended up using a custom content area renderer:

    public class CustomContentAreaRenderer : ContentAreaRenderer 
    {
        protected override string GetContentAreaItemCssClass(HtmlHelper htmlHelper, ContentAreaItem contentAreaItem)
        {
            var tag = GetContentAreaItemTemplateTag(htmlHelper, contentAreaItem);
            return string.Format("block {0} {1} {2}", GetTypeSpecificCssClasses(contentAreaItem, ContentRepository), "my own classes", tag);
        }
    }
    

    I apply the custom renderer to the container with this code:

    container.For<ContentAreaRenderer>().Use<CustomContentAreaRenderer>();
    

    Thank you for your help!