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 linedottedโ a row of dotsdashedโ a row of dashesdoubleโ two parallel linesgrooveandridgeโ 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.

โ Frequently Asked Questions
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.”
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.
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.
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.
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.
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.
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
-
-
-
1.3. HTML Tag Preview
-
-
-
2.2. The HTML Framework
-
-
2.4. HTML Headings
-
-
-
-
-
-
3.01. Introduction to CSS
-
3.02. How CSS Cascades
-
-
-
-
3.05. CSS Colors
-
3.06. RGBA Colors
-
-
3.08. Font and Text Properties
-
3.09. Classes and IDs
-
-
-
-
-
4.3. HTML Embedded Images
-
-
-
-
-
-
5.1. CSS Borders
-
5.2. CSS Border Radius
-
5.3. CSS Gradients
-
5.4. CSS Box Shadows
-
5.5. CSS Text Shadows
-
7.1. The Display Property
-
-
-
7.4. The Box Model
-
-
-
-
-
-
9.1. Website Tables
-
9.2. HTML Forms
-
-
9.4. Special Form Fields
-
9.5. Embedding Multimedia
-
-
9.7. Keyboard Shortcuts
-
10.1. What Is a Framework?
-
10.2. Font Awesome Icons
-
-
10.4. Bootstrap Grid System
-
-
10.6. Bootstrap Navigation
-
-
11.2. Purchasing a Domain Name
-
11.3. Website Hosting
-
11.4. Nameservers and DNS
-
11.5. FTP
-
11.6. GitHub Pages
