What are borders and outlines?
Borders and outlines are CSS properties that draw lines around elements. They look similar but have important differences:
- Borders are part of the box model โ they take up space and affect the element's layout. You can style each side independently and use
border-radiusto round corners. - Outlines are drawn outside the border and don't affect layout. They're often used for focus states on buttons and form inputs โ especially important for accessibility.
Additionally, box-shadow lets you create drop shadows, inset shadows, and layered effects that make elements appear elevated or embedded.
Border styles
The border-style property accepts nine values. Let's see all eight common styles:
In practice, solid and dashed are most common. The 3D styles like groove, ridge, inset, and outset are rarely used in modern design.
Border width, color, and shorthand
You can control three aspects of a border independently, or use the shorthand:
The shorthand border: width style color is the fastest way to add a border. Use individual properties like border-top, border-right, border-bottom, and border-left when you need different styles on each side.
Try it yourself: Border styles gallery
Edit the code below to create a gallery of cards with different border styles and colors. Click Run to see the changes.
Rounded corners with border-radius
border-radius rounds the corners of any element. You can round all corners the same, or specify each corner individually.
When border-radius: 50% is applied to a square element, it becomes a perfect circle. For pill-shaped buttons, set the horizontal padding and use a large border-radius.
Try it yourself: Circles, pills, and rounded cards
Experiment with border-radius to create avatars, buttons, and cards. Change the values to see how corners round.
Outlines and focus states
An outline is similar to a border but doesn't affect layout or spacing. Outlines are essential for focus states โ when a user tabs to a button or input field, the outline shows which element is focused.
Important: Never remove focus outlines without replacing them. Screen readers and keyboard users rely on visible focus indicators to navigate. Always provide a custom outline or other visual indicator.
:focus or :focus-visible) are critical for keyboard navigation. Always ensure your buttons and inputs have visible focus indicators.
Box-shadow for depth and effects
box-shadow creates drop shadows, inset shadows, and layered effects. The syntax is: box-shadow: horizontal vertical blur spread color;
The four shadow values are:
horizontal(X offset): positive = right, negative = leftvertical(Y offset): positive = down, negative = upblur: larger = softer shadowspread(optional): enlarges or shrinks the shadowcolor: shadow color (often semi-transparent black)inset(optional): creates inner shadow instead of drop shadow
Try it yourself: Box-shadow effects
Experiment with different shadow values. See how blur and spread change the effect. Try inset shadows and layered effects.
Task: Create a professional profile card with:
- A circular avatar (using
border-radius: 50%) with a border - A rounded card container with a drop shadow
- A pill-shaped "Follow" button with hover effect
- A decorative colored bottom border on the name
- Multiple layered shadows for depth
Use the editor above to build it โ experiment with different border-radius values and shadow effects!
Hint: Start with a container div. Inside, place an img with border-radius: 50% and fixed width/height. Add a heading with border-bottom and a button with border-radius: 50px. Use box-shadow: 0 4px 12px rgba(0,0,0,0.15) on the main card.
Key takeaways
- Borders are part of the box model and take up space. Outlines don't affect layout and are used for focus states.
border-styleincludes: solid, dashed, dotted, double, groove, ridge, inset, outset, and none.- Use the shorthand
border: width style colorfor quick styling, or individual properties for per-side control. border-radius: 50%on a square creates a circle; use it for avatars and icons.border-radius: 50pxon a button creates a pill shape โ great for CTAs.box-shadowcreates depth without changing the DOM. Use it sparingly for visual hierarchy.- Always provide visible focus indicators for accessibility โ don't remove outlines without replacing them.
Test your knowledge
You just mastered CSS borders.
Imagine what you'll design next.
Join the Best AI Programs โ three 5-month courses, fully online, with placement assistance. Week 1 is free.
Explore our AI-era courses โ