Skip to content

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.

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 using ring-offset-* utilities.
  • Styling: borders can be styled (dashed, dotted, etc.). rings are always solid.
ClassEffect
ring-0Removes any ring (sets a 0px box-shadow).
ring-1Applies a 1px wide ring.
ring-2Applies a 2px wide ring.
ringApplies a 3px wide ring (this is the default).
ring-4Applies a 4px wide ring.
ring-8Applies a 8px wide ring.
ring-insetDraws the ring on the inside of the element’s border instead of the outside.

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.

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>
  • 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 using ring-color utilities.
  • 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.

Rings can also be used for visual effects, such as making an element ‘pop’ on hover.

<!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>
  • Combine Everything: Remember to combine ring-width, ring-color, ring-offset-width, and ring-offset-color utilities for full control.
  • Arbitrary Values: Need a specific ring width? Use square brackets: ring-[5px].
  • Inset for Effect: The ring-inset utility 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.