Skip to main content
Tutorial Free

Filament Select: Use HTML with CSS in Options

May 01, 2024
2 min read

When building Filament forms, did you know you can add HTML code for the Select input? Imagine you have a select input with some colors, and you would like to show them.


First, the input itself must not be native and must allow HTML.

Forms\Components\Select::make('color')
->native(false)
->allowHtml()
->options([
'zinc' => 'Tailwind',
'alpine' => 'Alpine',
'laravel' => '>Laravel',
'livewire' => 'Livewire',
]),

You must ensure that the HTML is safe to render; otherwise, your application will be vulnerable to XSS attacks.

Now, for the options, we can write the HTML code.

use Filament\Support\Colors\Color;
 
Forms\Components\Select::make('color')
->native(false)
->allowHtml()
->options([
'zinc' => '<span style="color: rgb('. Color::Sky[600] .')">Tailwind</span>',
'alpine' => '<span style="color: rgb('. Color::Lime[600] .')">Alpine</span>',
'laravel' => '<span style="color: rgb('. Color::Emerald[600] .')">Laravel</span>',
'livewire' => '<span style="color: rgb('. Color::Fuchsia[600] .')">Livewire</span>',
]),

And we have options with text color.

If needed, you can use Tailwind classes, but if styles aren't applied, remember to create a custom theme.


If you want more Filament examples, you can find more real-life projects on our FilamentExamples.com.

Enjoyed This Tutorial?

Get access to all premium tutorials, video and text courses, and exclusive Laravel resources. Join our community of 10,000+ developers.

Comments & Discussion

No comments yet…

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.