The outline
property in CSS is used to draw a line around an element, outside the border edge. It is similar to the border
property but differs in a few key ways:
The outline
property is a shorthand for setting the following individual properties:
outline-color
outline-style
outline-width
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Outline Property Example - Codersmile.com</title>
</head>
<body>
<div class="box">
This box has an outline.
</div>
</body>
</html>
<style>
/* styles.css */
body {
font-family: Arial, sans-serif;
}
.box {
width: 200px;
padding: 20px;
border: 2px solid #000;
margin: 10px;
background-color: #f0f0f0;
outline: 2px dashed #ff0000; /* Outline: 2px width, dashed style, red color */
}
</style>
.box
element has a solid black border and a dashed red outline.The outline-offset
property allows you to control the space between the outline and the border edge. This can be useful for creating more visually appealing designs.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Outline Offset Property Example - Codersmile.com</title>
</head>
<body>
<div class="box">
This box has an outline-offset.
</div>
</body>
</html>
<style>
body {
font-family: Arial, sans-serif;
}
.box {
width: 200px;
padding: 20px;
border: 2px solid #000;
margin: 10px;
background-color: #f0f0f0;
outline: 2px dashed #ff0000; /* Outline: 2px width, dashed style, red color */
outline-offset: 10px; /* Space between border and outline */
}
</style>
outline-offset
property adds a 10px space between the border and the outline, making the outline appear further from the box.