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-radius to 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.

Key point:
Borders affect layout; outlines don't. Box-shadow creates visual depth without changing the DOM.

Border styles

The border-style property accepts nine values. Let's see all eight common styles:

border-styles.css All eight border style values

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:

borders.css Border properties and 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.

โœ๏ธ Try It Yourself โ€” Edit & Run
Preview

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.

border-radius.css Rounded corners examples

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.

โœ๏ธ Try It Yourself โ€” Border Radius
Preview

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.

outline.css Outline and focus states

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.

Accessibility tip:
Focus states (with :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;

box-shadow.css Box-shadow examples

The four shadow values are:

  • horizontal (X offset): positive = right, negative = left
  • vertical (Y offset): positive = down, negative = up
  • blur: larger = softer shadow
  • spread (optional): enlarges or shrinks the shadow
  • color: 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.

โœ๏ธ Try It Yourself โ€” Box-Shadow Playground
Preview
Mini Challenge: Build a Profile Card

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-style includes: solid, dashed, dotted, double, groove, ridge, inset, outset, and none.
  • Use the shorthand border: width style color for 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: 50px on a button creates a pill shape โ€” great for CTAs.
  • box-shadow creates 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

Question 1 of 5
What is the correct shorthand for a 2px solid blue border?
Question 2 of 5
How do you create a perfect circle with CSS?
Question 3 of 5
What is the key difference between border and outline?
Question 4 of 5
Which value in box-shadow creates an inner shadow?
Question 5 of 5
How do you round only the top two corners of an element?
0
/5
Great work! You understand CSS borders and shadows.
Africa & India's Best AI Academy
๐Ÿค– AI-first ๐ŸŽ“ Placement Assistance

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 โ†’