Tailwind CSS - Ring Width
Tailwind CSS - Ring Width
Section titled “Tailwind CSS - Ring Width”Ring utilities create solid rings around elements using the box-shadow CSS property. They are an excellent, modern alternative to outline for creating highly visible focus indicators and other decorative effects, without affecting the element’s layout.
Ring vs. Border: What’s the Difference?
Section titled “Ring vs. Border: What’s the Difference?”Understanding the distinction is key to using rings effectively:
- Layout Impact:
borders are part of the CSS box model and take up space, potentially shifting other elements.rings are drawn outside the border and do not affect the layout. - Positioning:
borders are always adjacent to the element.rings can have an offset (space) between the element and the ring itself usingring-offset-*utilities. - Styling:
borders can be styled (dashed,dotted, etc.).rings are always solid.
Core Ring Width Utilities
Section titled “Core Ring Width Utilities”| Class | Effect |
|---|---|
| ring-0 | Removes any ring (sets a 0px box-shadow). |
| ring-1 | Applies a 1px wide ring. |
| ring-2 | Applies a 2px wide ring. |
| ring | Applies a 3px wide ring (this is the default). |
| ring-4 | Applies a 4px wide ring. |
| ring-8 | Applies a 8px wide ring. |
| ring-inset | Draws the ring on the inside of the element’s border instead of the outside. |
The #1 Use Case: Accessible Focus States
Section titled “The #1 Use Case: Accessible Focus States”Creating visible focus indicators is critical for keyboard accessibility (a11y). Rings are perfect for this because they can be layered on top of other styles without breaking the layout. The focus-visible variant is the modern standard for showing focus rings only to keyboard users.
Example 1: A Truly Accessible Button
Section titled “Example 1: A Truly Accessible Button”This button uses focus-visible to show a 2px blue ring with a 2px offset when a user tabs to it. The offset ensures the ring is visible even if the button is flush against another element.
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Accessible Focus Ring</title> <script src="https://cdn.tailwindcss.com"></script></head><body class="bg-slate-100 flex items-center justify-center min-h-screen p-4 space-x-4">
<button class="px-6 py-3 bg-indigo-600 text-white font-semibold rounded-lg shadow-md hover:bg-indigo-700 focus:outline-none focus-visible:ring-2 focus-visible:ring-offset-2 focus-visible:ring-indigo-500 focus-visible:ring-offset-slate-100"> Click or Tab to Me </button>
<input type="text" placeholder="Tab to me too" class="px-4 py-3 border border-gray-300 rounded-lg focus:outline-none focus-visible:ring-2 focus-visible:ring-offset-2 focus-visible:ring-green-500">
</body></html>Breaking Down the Focus Classes:
Section titled “Breaking Down the Focus Classes:”focus:outline-none: We first remove the browser’s default, often inconsistent, outline.focus-visible:ring-2: We add a 2px ring, but only for “real” focus events (like keyboard tabbing), not mouse clicks.focus-visible:ring-indigo-500: We color the ring usingring-colorutilities.focus-visible:ring-offset-2: We add a 2px offset between the element and the ring.focus-visible:ring-offset-slate-100: We can even color the offset space to match the background, ensuring a clean look.
Decorative Use and Hover States
Section titled “Decorative Use and Hover States”Rings can also be used for visual effects, such as making an element ‘pop’ on hover.
Example 2: Interactive Profile Picture
Section titled “Example 2: Interactive Profile Picture”<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Decorative Ring Example</title> <script src="https://cdn.tailwindcss.com"></script></head><body class="bg-gray-200 flex items-center justify-center min-h-screen">
<!-- The transition-all class makes the ring appear smoothly --> <div class="relative p-1 rounded-full hover:ring-4 hover:ring-pink-400 hover:ring-offset-4 hover:ring-offset-gray-200 transition-all duration-300"> <img class="w-32 h-32 rounded-full" src="https://via.placeholder.com/128" alt="Profile picture"> </div>
</body></html>Pro-Tips
Section titled “Pro-Tips”- Combine Everything: Remember to combine
ring-width,ring-color,ring-offset-width, andring-offset-colorutilities for full control. - Arbitrary Values: Need a specific ring width? Use square brackets:
ring-[5px]. - Inset for Effect: The
ring-insetutility can be used to create an inner border effect without adding to the element’s size, which is useful for things like active states on form inputs.