Drupal 8 add class to content

In Drupal 8, here is a way to add a class to the content and not the entire node type.

Turn on twig debug to find template names.

Go to your theme / templates folder

Edit node.html.twig.

Wrap the article tag with a blog so you can override it in an child template.

{% block article %}
<article{{ attributes.addClass(classes) }}>
{% endblock %}

Edit your child template that extends node.html.twig.

Add classes using merge.


Drupal 8 twig snippets

Ternary operator ​​​​​

{% set foo = true %}
{{ foo ? 'FOO IS TRUE' : 'FOO NOT TRUE' }}

If then else

{% if node.getType == 'photo' %}
{% elseif node.getType in ['book', 'page'] %}
{% else %}
{% endif %}


{{ $uri = path('entity.node.canonical', {'node': node.id}) }}
{% if '/services/' in url %}
{% if uri starts with '/hismb' %}
{% if node.getType == 'photo' %} 
{% if node.getType not in ['text', 'misc' ] %}
{% if not page %}
{% if page %}

Date ranges

Drupal 8 add classes to body using URL


Turn on twig debug.

Locate view template in use.

In my case, they were in


In views-view.html.twig I found the div containing the views-content class.

Adding "container" to views-content and to views-header allowed a full-width content section to behave with expected margin yet have full width background image.



Subscribe to Front page feed