Skip to content

Tailwind CSS - Ring Offset Color

Utilities for setting the color of an element’s outline ring offset. The ring offset is the visible space between an element and its focus ring, crucial for ensuring the ring is visible against any background.

When you apply a focus ring (ring-2, ring-4, etc.), it appears directly outside the element’s border. If the element is placed on a background of the same color as the ring, the ring becomes invisible. This is a significant accessibility issue.

The ring-offset-color utilities solve this by creating a solid-colored space around the element, inside the ring. This ensures the focus ring always has a contrasting background, making it consistently visible.

To set the ring offset color, use the ring-offset-{color} utilities. These classes are typically combined with ring-{width} and ring-offset-{width} classes. The full default color palette is available.

Class PatternCSS Properties
ring-offset-{color}--tw-ring-offset-color: {colorValue};
ring-offset-white--tw-ring-offset-color: #fff;
ring-offset-slate-900--tw-ring-offset-color: #0f172a;
ring-offset-blue-500--tw-ring-offset-color: #3b82f6;

Note: For a complete list of all available color utilities, please refer to the official Tailwind CSS documentation on Ring Offset Color.

A common use case is styling buttons to have clear and accessible focus indicators. In this example, we’ll create a button that, when focused, displays a blue ring with a white offset. This ensures the blue ring is visible even if the button is placed on a blue background.

This example demonstrates how to apply ring offset color utilities to ensure focus rings are always visible. The focus: variant is used to apply these styles only when the element is in a focused state.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Accessible Button with Ring Offset</title>
<!-- For demonstration, we use the Tailwind Play CDN. In a real project, set up Tailwind CSS with a build step. -->
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-slate-800 dark antialiased font-sans text-white flex items-center justify-center min-h-screen">
<div class="space-y-4 text-center">
<h1 class="text-2xl font-bold">Accessible Button with Ring Offset</h1>
<p class="text-slate-400">Click the button to see the focus ring with its offset.</p>
<button class="bg-blue-600 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded-lg transition-colors duration-200 focus:outline-none focus:ring-4 focus:ring-blue-400 focus:ring-offset-4 focus:ring-offset-slate-800">
Click Me
</button>
<p class="text-slate-500 text-sm max-w-md mx-auto pt-4">
The `focus:ring-offset-slate-800` class ensures the focus ring is visible against the dark page background, creating a clear separation.
</p>
</div>
</body>
</html>

A common best practice is to set the ring offset color to match the component’s background color. This creates a seamless effect where the ring appears to have a gap between it and the element. In the example above, focus:ring-offset-slate-800 matches the bg-slate-800 of the <body>.

Like most Tailwind utilities, ring offset color can be combined with state and responsive variants. For example, md:focus:ring-offset-red-500 would apply a red offset color on medium screens and larger when the element is focused.

You can add your own ring offset colors to your tailwind.config.js file by extending the ringOffsetColor theme object.

tailwind.config.js
module.exports = {
theme: {
extend: {
ringOffsetColor: {
'brand': '#FF6347', // Example: Tomato color
}
}
}
}

With this configuration, you can now use the class ring-offset-brand in your HTML.