Back to Web Development
2026-02-015 min read

Borders (Web Development)

Learn Borders (Web Development) step by step with clear examples and exercises.

Title: Borders in Web Development: A full guide

Why This Matters

Borders are essential in web development as they help separate and organize elements on a webpage, making it visually appealing and easy to navigate. Understanding borders is crucial for creating professional-looking websites and ensuring that your content stands out.

Prerequisites

To follow this tutorial, you should have a basic understanding of HTML and CSS. If you're new to web development, we recommend checking out our HTML Introduction and CSS Introduction lessons first.

Core Concept

Understanding Borders

In CSS, you can add borders to HTML elements using the border property. This property has four subproperties that allow you to control different aspects of the border: border-width, border-style, border-color, and border-radius.

Border-Width

The border-width property sets the thickness of the border. You can specify a single value for all sides, or separate values for each side using the following shorthand syntax:

border-width: 2px solid; /* Sets the border width to 2 pixels and the border style to solid */
border-width: 1px 3px 5px 7px; /* Sets the top border width to 1 pixel, right border width to 3 pixels, bottom border width to 5 pixels, and left border width to 7 pixels */

Border-Style

The border-style property sets the type of line that makes up the border. Common values include solid, dashed, dotted, and double. Here's an example:

border-style: solid; /* Sets the border style to a solid line */
border-style: dashed; /* Sets the border style to a dashed line */

Border-Color

The border-color property sets the color of the border. You can specify a single color for all sides, or separate colors for each side using the following shorthand syntax:

border-color: red; /* Sets the border color to red */
border-color: red blue green yellow; /* Sets the top border color to red, right border color to blue, bottom border color to green, and left border color to yellow */

Border-Radius

The border-radius property sets the rounded corners of an element. You can specify a single radius for all corners or separate radii for each corner using the following shorthand syntax:

border-radius: 10px; /* Sets the border radius to 10 pixels for all corners */
border-top-left-radius: 20px; /* Sets the top left corner radius to 20 pixels */
border-bottom-right-radius: 30px; /* Sets the bottom right corner radius to 30 pixels */

Box Model and Borders

In CSS, every HTML element has a box model that consists of content area, padding, border, and margin. When you set a border for an element, it adds to the total width and height of the element's box model. Keep this in mind when designing your web pages to ensure elements don't overlap or become too large.

Worked Example

Let's create a simple HTML page with a container that has a border:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Borders Example</title>
<style>
.container {
width: 300px;
height: 200px;
border: 5px solid red;
border-radius: 10px;
padding: 20px;
}
</style>
</head>
<body>
<div class="container">
<h1>Welcome to my website!</h1>
<p>This is an example of a container with a border.</p>
</div>
</body>
</html>

Save this code as borders_example.html, open it in your web browser, and you'll see a red-bordered container with rounded corners and some padding inside.

Common Mistakes

  1. Forgetting to close the border-width, border-style, or border-color shorthand syntax. Remember to always close these properties with a semicolon (;).
/* Incorrect */
border: 2px solid red;

/* Correct */
border: 2px solid red; /* Always close the shorthand syntax with a semicolon */
  1. Setting border-radius for only some corners. If you want to set different radii for each corner, make sure to use all four properties (border-top-left-radius, border-top-right-radius, border-bottom-left-radius, and border-bottom-right-radius).
/* Incorrect */
border-radius: 20px 30px; /* This only sets the left and right corners */

/* Correct */
border-top-left-radius: 20px;
border-top-right-radius: 30px;

Practice Questions

  1. Create a webpage with a container that has a dashed border of 3 pixels and a radius of 15 pixels on all corners. The container should have a width of 400 pixels, a height of 250 pixels, and some padding inside.
  1. Given the following HTML and CSS code, what will be the final appearance of the .box element?
<div class="box">Hello World</div>

<style>
.box {
width: 300px;
height: 200px;
border: 5px solid blue;
border-radius: 10px;
padding: 20px;
}
</style>

FAQ

Q: Can I set different border colors for each side of an element?

A: Yes, you can specify separate colors for each side using the shorthand syntax for border-color.

Q: How do I remove borders from an HTML element in CSS?

A: To remove borders, set all border properties (border-width, border-style, and border-color) to 0.

.no-borders {
border: 0;
}

Q: How can I create a border that only appears on hover?

A: To create a border that only appears on hover, use the :hover pseudo-class in your CSS:

.box:hover {
border: 5px solid blue;
}

Q: How do I create a border that extends beyond the element's content area?

A: To create a border that extends beyond the element's content area, increase the padding property or decrease the content-area (width and height) of the element. Keep in mind that this will also affect the total width and height of the box model.

.extended-border {
width: 250px; /* Decrease content area */
padding: 30px; /* Increase padding to extend border */
border: 5px solid blue;
}
Borders (Web Development) | Web Development | XQA Learn