LESSON 5.1

CSS Borders

Frame and define your elements with style

Learn how to add borders to any HTML element using CSS, controlling the width, style, and color to create dividers, outlined boxes, and decorative effects.

๐Ÿ’ก The Concept

The CSS border property lets you add a visible outline around any HTML element, with control over its width, style, and color. Borders do a lot of quiet work on a webpage. They create visual separation between sections, draw attention to important elements like buttons and callout boxes, and give structure to layouts that would otherwise feel like a wall of content. Even a subtle border can make a design feel more intentional and polished.

For a border to be displayed, you need three CSS properties: border-width, border-style, and border-color. If any one of the three is missing, the border will not display.

img {
   border-width: 12px;
   border-style: solid;
   border-color: #a51d01;
}

CSS Shorthand Properties

With CSS shorthand you can combine all three border properties into a single line โ€” width, style, and color separated by spaces. It is called shorthand because you get three properties for the price of one.

img {
   border: 12px solid #a51d01;
}

Note that you need to have all three values separated by a space for your border to work.

Border Style Options

The border-style value controls the appearance of the line. Common options include:

  • solid โ€” a plain, unbroken line
  • dotted โ€” a row of dots
  • dashed โ€” a row of dashes
  • double โ€” two parallel lines
  • groove and ridge โ€” 3D carved effects

Single Side Borders

You can add a border to one side only using the individual side properties: border-top, border-right, border-bottom, border-left. A common use is adding a border-bottom to headings as a visual separator:

h2 {
   border-bottom: 5px solid #F6D23D;
}

๐ŸŽฌ Video Demonstration

In this video I demonstrate how to use borders in JSFiddle.

Exercise: Try It Yourself

๐Ÿ“Œ Tools of the Trade: Use the Border Generator to experiment with border styles, widths, and colors and copy the ready-to-use CSS. Bookmark the Tools of the Trade page for quick access to all course tools.

Step 1. Open JSFiddle and paste this HTML into the HTML panel:

<h1>Border Play</h1>
<img src="https://webdevstudents.com/wp-content/uploads/2025/09/guinea-pig.png" alt="A cute guinea pig" width="100" height="100">

Step 2. In the CSS panel, add a border to the image. If you need help choosing values, use the Border Generator to design your border and copy the CSS.

Step 3. Add a border to the <h1>.

Step 4. Try border styles other than solid โ€” for example dotted or groove.

Step 5. Add a border to a single side only.

Step 6. Open the Border Generator, design a border you like, and copy the CSS into JSFiddle.

JSFiddle demonstrating CSS border properties

โ“ Frequently Asked Questions

Why are the three shorthand values separated by spaces instead of commas?

In CSS, commas are used to separate lists of values that are all the same type — like multiple background images or multiple fonts in a font stack. When a shorthand property takes different types of values, spaces are used instead. The browser can tell 12px from solid from #a51d01 because each value is a different kind of thing. Spaces say “these three things together describe one property,” while commas would say “here are three separate items.”

Does the order of the three values matter in the shorthand border?

Technically no — the browser can figure out which value is which because they are different types (a number with a unit, a keyword, and a color). However, the conventional order is width, style, then color: border: 2px solid red;. Following the convention makes your code easier for others to read and matches what you will see in documentation and examples across the web.

What happens if I leave out one of the three values in the shorthand?

The border may not display at all. The border-style is the most critical — without it, no border will appear regardless of what width or color you set. If you omit the width, the browser uses a default (usually 3px). If you omit the color, it inherits the element’s text color. But forgetting the style is the most common reason a border mysteriously refuses to show up.

Can I have different borders on each side of an element?

Yes. You can use border-top, border-right, border-bottom, and border-left independently, each with their own width, style, and color. A common design technique is to use only border-bottom on a heading to create a clean underline effect, or to combine a thick left border with no other borders to create an accent stripe on a card or blockquote.

Can I use a border on any HTML element, or just divs?

You can add a border to virtually any HTML element — headings, paragraphs, images, lists, buttons, inputs, and more. The border wraps around the element’s box, so it works wherever a box exists. Some elements like inline text behave a little differently, but for most block-level elements like div, p, h1, and img, borders work exactly as you would expect.

Why isn’t my border showing up?

The most common reason is a missing border-style. A border will not display without it, even if you have set a width and color. Check that your shorthand includes all three values, or that you have a separate border-style declaration. Other things to check: a typo in the property name, a conflicting rule overriding your border, or accidentally applying the border to the wrong selector.

What is the difference between border and outline?

Both create a visible line around an element, but they behave differently. A border is part of the element’s box model — it takes up space and affects the layout around it. An outline is drawn outside the border and does not affect layout at all; it does not push other elements around. You will most often see outline in browser default styles, such as the blue focus ring that appears around form fields and links when you tab to them. Developers sometimes write outline: none to remove it, though this can hurt accessibility for keyboard users.

โ˜… Key Takeaways

Borders can frame content, separate sections, and add visual structure to a page. This lesson covered the CSS border properties, including style, color, and width. Next up is Block and Inline Elements, where you will learn how different HTML elements behave by default. That knowledge will help you control layout more precisely.

Google Ads

The costs of this website are partially offset by revenue from Google Ads.

Tools of the Trade

HTML/CSS Lessons