Introduction to Fragments

Get introduced to the concept of Fragments in Thymeleaf.

Our index.html page currently contains some significant duplication. If you are not familiar with Tailwind CSS, you might try to tackle the many classes issue by creating custom CSS classes.

However, it is more helpful to think in terms of full components we can reuse. When we do this, we won’t need to define our own CSS classes to reduce the duplication.

Fragments

Thymeleaf uses the concept of fragments, which are basically reusable snippets of HTML. They are very similar to methods. Just as we use methods to more effectively structure our Java code, we use fragments to structure our HTML pages better. We define a fragment using th:fragment.

Get hands-on with 1200+ tech skills courses.