Tailwind CSS - Outline Style
Modern Guide to Tailwind CSS: Outline Style
Section titled “Modern Guide to Tailwind CSS: Outline Style”The outline-style utilities in Tailwind CSS allow you to control the style of an element’s outline, such as solid, dashed, or dotted. Outlines are drawn outside an element’s border and, crucially, do not take up space in the layout, which makes them ideal for focus indicators.
Key Difference: Outline vs. Border
Section titled “Key Difference: Outline vs. Border”It’s vital to understand the difference:
border: Is part of the box model. Adding a border increases the element’s total size and can cause a layout shift.outline: Is rendered on top of the layout. Adding an outline does not affect the element’s size or the position of surrounding elements.
Core outline Utilities
Section titled “Core outline Utilities”| Class | CSS Properties |
|---|---|
outline-none | outline: 2px solid transparent; outline-offset: 2px; |
outline | outline-style: solid; (assumes outline-width and outline-color are also set) |
outline-dashed | outline-style: dashed; |
outline-dotted | outline-style: dotted; |
outline-double | outline-style: double; |
Practical Example: Accessible Form Inputs
Section titled “Practical Example: Accessible Form Inputs”The primary use case for outlines is to provide clear, visible focus indicators for interactive elements, which is a cornerstone of web accessibility (a11y). Let’s style a set of input fields to have a clear and attractive focus state using outline utilities.
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link href="/dist/output.css" rel="stylesheet"> <title>Accessible Outline Styles</title></head><body class="bg-slate-100 flex items-center justify-center min-h-screen">
<form class="bg-white p-8 rounded-lg shadow-lg w-full max-w-md"> <h1 class="text-2xl font-bold mb-6 text-slate-800">Create Your Account</h1>
<div class="mb-4"> <label for="email" class="block text-slate-700 text-sm font-bold mb-2">Email Address</label> <input id="email" type="email" class="w-full px-3 py-2 border border-slate-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent" placeholder="you@example.com"> </div>
<div class="mb-6"> <label for="password" class="block text-slate-700 text-sm font-bold mb-2">Password</label> <input id="password" type="password" class="w-full px-3 py-2 border border-slate-300 rounded-md focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-pink-500" placeholder="••••••••"> </div>
<button type="submit" class="w-full bg-blue-600 text-white font-bold py-2 px-4 rounded-md hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-2"> Sign Up </button> </form>
<div class="absolute bottom-4 right-4 bg-green-100 border-l-4 border-green-500 text-green-800 p-4 rounded-md shadow-md"> <p class="font-bold">Accessibility Best Practice: `focus-visible`</p> <p>Consider using the `focus-visible` variant instead of `focus`. This applies focus styles only when the user is navigating with a keyboard (e.g., tabbing), not on mouse click, providing a cleaner experience for mouse users while maintaining accessibility.</p> <p class="mt-2 font-mono text-sm">Example: `focus-visible:ring-2`</p> </div>
</body></html>