How to Add Border in an HTML Web Page – Simple Guide with Examples

Adding a border in an HTML web page is a common styling technique used to highlight content, separate sections, or enhance visual appeal. Borders can be applied to almost any HTML element using CSS.

In this guide, you’ll learn how to add borders to elements with practical code examples and styling tips.


🧱 What Is a Border in HTML?

In HTML, borders are not added directly with HTML tags—they are styled using CSS (Cascading Style Sheets). You can apply borders to any element such as a div, p, img, table, and more.


✅ Basic Syntax of CSS Border

selector {
  border: border-width border-style border-color;
}

Example:

p {
  border: 2px solid black;
}

This will add a 2px solid black border around all <p> elements.


📌 Step-by-Step Example

HTML:

<!DOCTYPE html>
<html>
<head>
  <style>
    .box {
      border: 2px solid blue;
      padding: 20px;
      width: 300px;
    }
  </style>
</head>
<body>

  <div class="box">
    This box has a blue border.
  </div>

</body>
</html>

🎨 Customize Border Properties

1. Border Width

border-width: 1px;   /* thin */
border-width: 5px;   /* thick */

2. Border Style

border-style: solid;   /* continuous line */
border-style: dashed;  /* dashed line */
border-style: dotted;  /* dotted line */
border-style: double;  /* double lines */

3. Border Color

border-color: red;
border-color: #333333;
border-color: rgb(0, 128, 0);

🧩 Add Borders to Specific Sides

You can also apply borders to only one side of an element.

border-top: 2px solid red;
border-right: 2px dashed green;
border-bottom: 2px dotted blue;
border-left: 2px double black;

🖼️ Example: Add Border to an Image

<img src="photo.jpg" style="border: 3px solid #ccc; border-radius: 8px;">

This adds a light gray border with slightly rounded corners to the image.


🗂️ Advanced: Rounded and Shadowed Borders

Rounded Border

border-radius: 10px;

Box Shadow (optional enhancement)

box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);

🧾 Quick Reference

PropertyDescription
borderShorthand to set all border values
border-widthThickness of the border
border-styleType of border line
border-colorColor of the border
border-radiusRounded corners
box-shadowShadow around the element

✅ Conclusion

Adding borders to your HTML elements using CSS is a simple but effective way to improve the layout, structure, and design of your web page. With a few lines of CSS, you can turn plain content into clean, styled blocks that stand out.

Start with basic solid borders and experiment with styles, colors, and radii to achieve the look you want.

Sharing Is Caring:

Leave a Comment