HTML Border

What is an HTML Border?

An HTML border is a feature in web development that allows designers to add visual separation to elements on a webpage. Borders can be applied to various HTML elements like images, tables, div containers, and more. They enhance the structure of a webpage by providing a clear visual outline. Borders can be customized in terms of color, thickness, style, and radius, allowing for unique and visually appealing layouts.

Why are Borders Important in Web Design?

Borders are key in web design because they enhance the visual appeal and readability of content. They help organize layout elements and guide users through the website. Borders define and separate different sections, preventing clutter and making important content more noticeable.

Using CSS, adding and styling borders is straightforward. Designers can specify the color, width, style, and position, often with just a few lines of code. The shorthand border property enables setting multiple border attributes at once, making coding efficient.

Basic CSS Border Properties

Using the border Property

The border property is used to add borders to HTML elements like tables, headers (th), and table cells (td). You can specify border-width for thickness and border-style for the type of border (e.g., solid, dotted, dashed).

Example:

table, th, td {
  border: 1px solid black;
}

This sets a 1-pixel solid black border for the table and its cells.

Setting Border Color with border-color

To define the color of a border, use the border-color property. Colors can be defined by name, hexadecimal code, or RGB values.

Examples:

border-color: red;
border-color: #ff0000;
border-color: rgb(255, 0, 0);

For different border colors on each side, use:

  • border-top-color
  • border-right-color
  • border-bottom-color
  • border-left-color

Alternatively, use classes from frameworks like W3.CSS (e.g., w3-border-blue).

Choosing a Border Style with border-style

The border-style property defines how a border looks. Common styles include:

  1. Solid - A plain solid line.
  2. Dotted - A series of dots.
  3. Dashed - Long dashes.
  4. Double - Two parallel lines.
  5. Groove - A carved groove effect.
  6. Ridge - A raised ridge appearance.
  7. Inset - An indented effect.
  8. Outset - A raised effect.

Specifying Border Width with border-width

The border-width property sets the thickness of borders. You can use pixels (e.g., 2px), or relative values (thin, medium, thick). For different sides:

border-width: 1px 2px 3px 4px;

This sets top, right, bottom, and left border widths respectively.

Advanced CSS Border Properties

Creating Double Borders

To add a double border, set the border-style to double. Example:

border: 3px double red;

This creates a 3-pixel double red border.

Customizing the Left Border of an Element

CSS provides properties to customize a specific side of a border. For the left border:

  • border-left: Shorthand to set width, style, and color.
  • border-left-color: Sets only the color.
  • border-left-style: Sets the style (e.g., solid, dotted).
  • border-left-width: Sets the width.

Example:

.element {
  border-left: 2px solid red;
}

Using Shorthand for Multiple Border Properties

The shorthand border property allows setting width, style, and color in one line:

border: 2px solid black;

This applies a 2-pixel solid black border to all sides.

Different Types of Borders

Dotted Borders

To create a dotted border:

.secondary-header {
  border-style: dotted;
  border-width: 2px;
  border-color: red;
}

This applies a 2-pixel red dotted border to elements with the class secondary-header.

Grooved Borders

Grooved borders create a 3D effect. Use the border-style property with a value of groove:

.element {
  border-style: groove;
}

Hidden Borders

Hidden borders are borders styled to be invisible but still take up space in the layout. They can be created by setting the border style to hidden or using transparent colors:

.element {
  border-style: hidden;
  border-color: transparent;
}

Create a free account to access the full topic

“It has all the necessary theory, lots of practice, and projects of different levels. I haven't skipped any of the 3000+ coding exercises.”
Andrei Maftei
Hyperskill Graduate