Borders and outlines
Borders define component edges; outlines are often used to show focus without changing layout. They look similar but serve different purposes.
✓ What you will learn in this topic
- Explain the key idea: Borders shape components; outlines can identify focus without shifting layout.
- Apply the concept: The demo receives a rounded gray edge; keyboard-focused actions in the card gain a separated amber outline.
- Avoid this common mistake:
Concept explanation
A border has width, style, and color and participates in the box model. border-radius rounds corners. An outline is drawn outside the border and does not take up layout space, making it useful for focus indication. outline-offset separates the indicator from the element.
Do not remove outlines globally. A custom focus style should remain easy to see against both the component and the surrounding surface.
Syntax
A CSS rule has a selector followed by declarations. Each declaration is a property and a value; the braces group the declarations that apply to matching elements.
.demo {
border: 2px solid #94a3b8;
border-radius: 0.75rem;
}Example 1: a small page element
Use this HTML as the target and place the CSS in a stylesheet linked to the page, or inside a <style> element while experimenting.
<main class="demo card-grid course-grid dashboard-panel course-card-list dashboard-grid card-layout"><h2>Learning card</h2><p>Clear styles make this information easier to scan.</p><nav class="site-nav"><a href="/learn">Courses</a></nav><label class="form-field"><span>Email</span><input type="email" aria-invalid="true"></label><article class="card course-card dashboard-card theme-dark"><span class="badge status">New</span><div class="course-card__image"></div><h2 class="course-card__title card__title">Course update</h2><p class="status">New lessons are available this week.</p><a href="/learn" aria-current="page">Read the details</a><div class="actions course-card__actions dashboard-panel__actions"><button type="button">View lessons</button><button type="button" disabled>Unavailable</button></div></article></main>.demo {
border: 2px solid #94a3b8;
border-radius: 0.75rem;
}Expected result: The demo receives a rounded gray edge; keyboard-focused actions in the card gain a separated amber outline.
Example 2: a practical interface card
The same idea can style a reusable card on a course page. The class selector keeps the rule attached to this component rather than changing every element of that type.
<section class="demo card-grid course-grid dashboard-panel course-card-list dashboard-grid card-layout"><h2>Learning card</h2><p>Clear styles make this information easier to scan.</p><nav class="site-nav"><a href="/learn">Courses</a></nav><label class="form-field"><span>Email</span><input type="email" aria-invalid="true"></label><article class="card course-card dashboard-card theme-dark"><span class="badge status">New</span><div class="course-card__image"></div><h2 class="course-card__title card__title">Course update</h2><p class="status">New lessons are available this week.</p><a href="/learn" aria-current="page">Read the details</a><div class="actions course-card__actions dashboard-panel__actions"><button type="button">View lessons</button><button type="button" disabled>Unavailable</button></div></article></section>.card a:focus-visible,
.card button:focus-visible {
outline: 3px solid #f59e0b;
outline-offset: 3px;
}Expected result: The course update reads as a distinct interface card, and its content follows the spacing, color, and layout choices in the rule.
Common mistake
Removing a browser focus outline without providing a clear replacement makes keyboard navigation hard to follow.
Best practice
Keep a visible focus indicator, use shadows sparingly, and do not use an outline as a substitute for a layout border.
Practice exercise
Style a card with a subtle border and shadow, then style its keyboard focus state with a high-contrast outline. Apply it to the example above and inspect the result at a narrow and a wide viewport.
Section summary
- Borders shape components; outlines can identify focus without shifting layout.
- Keep a visible focus indicator, use shadows sparingly, and do not use an outline as a substitute for a layout border.
Knowledge check
Why is an outline useful for keyboard focus?
Reveal the explanation
Unlike a border, an outline does not change box dimensions and is commonly useful for focus states.
Finish the module lessons, then take the server-scored module quiz to check your understanding.
Practical example
css.demo {
border: 2px solid #94a3b8;
border-radius: 0.75rem;
}
Expected result:
The demo receives a rounded gray edge; keyboard-focused actions in the card gain a separated amber outline.
Hands-on practice task
Style a card with a subtle border and shadow, then style its keyboard focus state with a high-contrast outline.
In this section
Other lessons in Borders, Outlines, and Shadows
Broaden your knowledge
Related learning topics
HTML
Learn to structure accessible, meaningful web pages with a complete HTML course from first document to professional application patterns.
JavaScript
Build strong JavaScript foundations with browser examples, focused practice, beginner projects, and module quizzes.