Home 〉 CSS Tutorials 〉 CSS Outline Tutorial: Learn outline, outline-color, offset & style Guide
CSS Outline Tutorial: Learn outline, outline-color, offset & style Guide !
What's the difference between a border and an outline in CSS?
The `outline` property lets you highlight elements (like buttons and inputs) without affecting the layout. Learn how to control outline color, width, style, and even the space between the element and the outline with `outline-offset`. This is especially useful for accessibility and focus indicators. Here is the blog post on CSS Outline Property.
The 'outline' property in CSS lets you draw a line around an element — outside the border — without affecting the element's size or layout. It's often used for accessibility, focus styles, and visual highlights.
In this tutorial, you'll learn how to use 'outline', 'outline-color', 'outline-style', 'outline-width', and 'outline-offset' with real examples.
A CSS outline is a line drawn around an element, outside the border. It doesn't take up space in the layout (unlike margins or borders).
Syntax ✍
outline: <outline-width> <outline-style> <outline-color>;
All parts are optional, and the order doesn't matter.
Feature | Border | Outline |
---|---|---|
Affects layout | Yes (adds to size) | No (drawn outside) |
Individual sides? | Yes | No (applies to all) |
Offset possible? | No | Yes ('outline-offset') |
<!DOCTYPE html>
<html>
<head>
<style>
.outline-box {
padding: 20px;
outline: 2px solid red;
}
</style>
</head>
<body>
<div class="outline-box">This box has a red outline.</div>
</body>
</html>
output 📌
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
Adds a 2px solid red line outside the element.
Example 📄
outline-style: dashed;
<!DOCTYPE html>
<html>
<head>
<style>
.styled-outline {
outline-width: 3px;
outline-style: dashed;
outline-color: blue;
}
</style>
</head>
<body>
<p class="styled-outline">Dashed blue outline.</p>
</body>
</html>
output 📌
Dashed blue outline.
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
Sets custom width, style, and color for the outline.
The 'outline-offset' property creates space between the outline and the edge of the element.
Example 📄
outline-offset: 5px;
<!DOCTYPE html>
<html>
<head>
<style>
.offset-box {
border: 2px solid gray;
outline: 3px solid green;
outline-offset: 10px;
padding: 10px;
}
</style>
</head>
<body>
<div class="offset-box">Outline is offset from the border.</div>
</body>
</html>
output 📌
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
The green outline is drawn 10px away from the border.
Example 📄
outline: 4px dotted orange;
This is equal to:
<!DOCTYPE html>
<html>
<head>
<style>
input:focus {
outline: 2px solid #007BFF;
}
</style>
</head>
<body>
<input type="text" placeholder="Focus me">
</body>
</html>
output 📌
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
Property | Example Value |
---|---|
'outline-style' | 'solid', 'dashed' |
'outline-color' | 'red', '#333', 'rgba()' |
'outline-width' | '2px', 'thick' |
'outline-offset' | '5px', '10px' |
CSS 'outline' is a powerful but underused property. It's perfect for focus states, highlights, and accessible interfaces. Unlike borders, outlines don't affect layout, making them ideal for clean, lightweight UI elements.
Experiment with different colors, styles, and offsets to match your design and accessibility needs.
An outline does not affect element size and applies to all sides equally. A border adds to the layout and can be set on individual sides.
Yes, using 'transition', though results may vary across browsers.
Browsers use outlines to indicate keyboard focus. It's essential for accessibility.
Use 'outline: none;', but make sure to provide another visual indicator for focus.