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

Vytvorené 4y | 28. 4. 2021, 7:20:07


Ak chcete pridať komentár, prihláste sa

Ostatné príspevky v tejto skupine

New Core Team Members, 2025 Edition

A few weeks ago, I had the pleasure of announcing the formation of the Symfony UX Core Team, a dedicated group working to enhance the frontend development experience within the Symfony ecosystem. Toda

24. 2. 2025, 16:20:03 | Symfony
SymfonyLive Paris 2025 : Du lego de composants pour un bundle Gotenberg !

SymfonyLive Paris 2025, conference in French language only, will take place from March 27 to 28! The schedule is currently being revealed as we go along. More details are available here.

Al

24. 2. 2025, 13:50:07 | Symfony
A Week of Symfony #947 (17-23 February 2025)

This week, development activity focused on new security features. The upcoming Symfony 7.3 version added support for security voters to explain their vote, improved the IsGranted attribute to allow us

23. 2. 2025, 10:10:09 | Symfony
SymfonyLive Berlin 2025: Agentic Applications with Symfony

SymfonyLive Berlin 2025, conference held in English, will take place from April 1 to 4! The schedule is being revealed gradually. More details are available here.

We’re thrilled to announce

21. 2. 2025, 9:20:12 | Symfony
SymfonyLive Paris 2025 : Postgres pour vos besoins NoSQL

SymfonyLive Paris 2025, conference in French language only, will take place from March 27 to 28! The schedule is currently being revealed as we go along. More details are available here.

Al

20. 2. 2025, 10:10:13 | Symfony
SymfonyLive Berlin 2025: Asynchronous PHP

SymfonyLive Berlin 2025, conference held in English, will take place from April 1 to 4! The schedule is being revealed gradually. More details are available here.

We’re thrilled to announce

19. 2. 2025, 8:40:05 | Symfony
SymfonyLive Paris 2025 :  Le Composant Symfony Mapper

SymfonyLive Paris 2025, conference in French language only, will take place from March 27 to 28! The schedule is currently being revealed as we go along. More details are available here.

Al

18. 2. 2025, 14:10:24 | Symfony