'Django - Wagtail: How to add classes to generated HTML elements from RichTextField

Using Wagtail 2.16.1, I have a BlogPage model that contains a body field which is a RichTextField.

When I put {{ page.body|richtext }} into the template, the HTML that is rendered contains various html elements, as expected. I'd like to specify (tailwind) classes for these elements.

E.g. every <p> element in a richtextfield should have <p class="mx-2 my-3">, h2 elements should have <h2 class="mt-5">, etc.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source