🎨 Borders & Outlines in CSS
Borders and outlines are two important tools in CSS that help give structure, shape, and emphasis to HTML elements. Just like colors make your website vibrant and fonts make your text expressive, borders and outlines make your content stand out and give it visual definition.
Borders are drawn inside an element’s box (around its content and padding).
Outlines are drawn outside the element’s border and don’t take up space in the layout.
Together, they allow you to style boxes, highlight sections, and improve accessibility by showing focus indicators.
✍️ What are Borders & Outlines?
Border → A border is the line that goes around an element’s content and padding. You can control its thickness, style, color, and even make it rounded with border-radius
.
Outline → An outline is very similar to a border, but it is drawn outside the border edge and does not affect the element’s size or layout.
In short: Borders are for permanent design, outlines are for temporary highlighting (like when a user focuses on an input box).
🔑 Common Border & Outline Properties
border-style
→ defines how the border looks (solid, dashed, dotted, etc.)border-width
→ sets the thickness of the borderborder-color
→ changes the border colorborder-radius
→ makes rounded cornersoutline-style
→ defines how the outline looksoutline-width
→ sets the thickness of the outlineoutline-color
→ changes the outline coloroutline-offset
→ adds space between border and outline
🌟 Why Use Borders & Outlines?
Borders are perfect for structuring layouts, making boxes visible, or designing buttons and cards.
Outlines are best for accessibility (e.g., highlighting form fields when focused).
You don’t always need both, but when combined, they can create interactive and professional designs.
📝 CSS Borders & Outlines – Detailed Examples
1. Border Style (border-style
)
What it does: Controls how the border line looks.
Values: solid, dashed, dotted, double, groove, ridge, inset, outset, none
<p style="border-style: dashed;">This paragraph has a dashed border.</p>
2. Border Width (border-width
)
What it does: Sets the thickness of the border.
<div style="border-style: solid; border-width: 5px;">
This box has a thick border.
</div>
3. Border Color (border-color
)
<p style="border: 2px solid red;">
This text has a red border.
</p>
4. Border Radius (border-radius
)
<div style="border: 2px solid blue; border-radius: 15px; padding: 10px;">
Rounded corners example
</div>
<div style="border: 2px solid red; border-radius: 50%; width: 100px; height: 100px;">
Circle shape using border-radius
</div>
5. Outline Style & Offset (outline-style
, outline-offset
)
What it does: Highlights elements without affecting layout.
<button style="border: 2px solid blue; outline: 3px solid red; outline-offset: 5px;">
Click Me
</button>
6. Creative Use Case: Form Focus
<input type="text" placeholder="Click here">
<style>
input {
border: 2px solid gray;
padding: 8px;
}
input:focus {
outline: 3px solid green;
outline-offset: 2px;
}
</style>
✅ Open VS Code, create a borders.html
file, paste these examples inside, and run it in your browser to see borders and outlines in action.