New in Symfony 5.3: Tailwind CSS Form Theme

Contributed by Kevin Bond in #40449.

Tailwind CSS is the most popular utility-first CSS framework. Its unique (and odd-looking at first) philosophy is making it a resounding success and it’s reshaping the way many applications manage their CSS. Symfony includes ready-to-use themes to make your forms match the look and feel of other popular frameworks such as Bootstrap and Foundation. That’s why in Symfony 5.3 we’ve added a new Tailwind CSS theme for Symfony forms. The new theme is based on Tailwind CSS 2.x and its official Tailwind CSS form plugin. Due to the nature of Tailwind CSS, there’s an unlimited number of ways to style forms. That’s why the form theme applies the absolute minimum styles to make them usable and also provides customization options for each form field part. Start by applying the theme to some form: 1 2 3 4 5 6{% form_theme form 'tailwind_2_layout.html.twig' %}

{% block body %} <h1>User Sign Up:</h1> {{ form(form) }} {% endblock %}

Then, you can customize each part of the form field using the *_class options: 1 2 3 4 5 6 7 8{{ form_row(form.username, { row_class: 'mt-2 px-3 ...', label_class: 'font-semibold text-gray-600 ...', error_item_class: 'text-red-700 ...', widget_class: 'border border-gray-200 bg-gray-50 ...', widget_disabled_class: 'border-dashed text-gray-200 ...', widget_errors_class: 'border-double border-red-500 bg-red-50 ...', }) }}

If you want to apply the same styles to different fields and/or forms, you don’t have to repeat the same custom styles over and over again. Instead, create your own form theme based on the Tailwind CSS theme as follows: 1 2 3 4 5 6 7 8 9 10 11 12 13 14{# templates/form/theme.html.twig #} {% use 'tailwind_2_layout.html.twig' %}

{% block form_row %} {% set row_class = row_class|default('mt-2 px-3 ...') %} {{ parent() }} {% endblock form_row %}

{% block form_label %} {% set label_class = label_class|default('font-semibold text-gray-600 ...') %} {{ parent() }} {% endblock form_label %}

{# ... #}

Then, register and use this theme in your application the same as any other custom Symfony form theme.

                Sponsor the Symfony project.

http://feedproxy.google.com/~r/symfony/blog/~3/r3LiFSXhmKo/new-in-symfony-5-3-tailwind-css-form-theme

Created 4y | Apr 28, 2021, 7:20:07 AM


Login to add comment

Other posts in this group

A Week of Symfony #955 (April 14–20, 2025)

This week, the upcoming Symfony 7.3 version improved the AsAlias attribute by adding a new argument, introduced Clock support for UriSigner, and refined the return type of the ContainerInterface::get(

Apr 20, 2025, 8:30:06 AM | Symfony
SymfonyOnline June 2025: Rethinking File Handling in Symfony

SymfonyOnline June 2025 is almost here, starting in almost 2 months on:

June 10-11: Workshop days. It is possible to attend 1 two-day training or 2 one-day trainings. June 12-13: Online confe

Apr 16, 2025, 4:30:02 PM | Symfony
SymfonyLive Paris 2025: Recap and replay!

SymfonyLive Paris 2025 took place three weeks ago — a big thank you to everyone who joined us! The conference was held entirely in French, and now you can relive the best moments: replays, hig

Apr 15, 2025, 2:50:24 PM | Symfony
A Week of Symfony #954 (April 7–13, 2025)

This week, Symfony 7.3 entered its "feature freeze" period in preparation for its release at the end of May 2025. Development activity focused on refining and polishing its new features, including a n

Apr 13, 2025, 9:40:02 AM | Symfony
SymfonyOnline June 2025: What's New in Symfony 7.3

SymfonyOnline June 2025 is almost here, starting in almost 2 months on:

June 10-11: Workshop days. It is possible to attend 1 two-day training or 2 one-day trainings. June 12-13: Online confe

Apr 10, 2025, 4:40:34 PM | Symfony
SymfonyCon Amsterdam 2025: Last days to enjoy early bird tickets!

The international Symfony conference of the year, SymfonyCon Amsterdam 2025, will take place in the Netherlands on November 27 & 28, 2025!

⏳ Early bird registration ends this Wednesday! D

Apr 8, 2025, 9:10:13 AM | Symfony
A Week of Symfony #953 (March 31 – April 6, 2025)

This week, the upcoming Symfony 7.3 version entered its feature freeze period to tweak and polish its new features before releasing it at the end of May 2025. In addition, we celebrated the SymfonyLiv

Apr 6, 2025, 8:30:10 AM | Symfony