Guide to CSS Borders

Posted on : 2023-10-24
Guide to CSS Borders

Share

One crucial aspect of CSS is the creation of borders, which define how elements are enclosed and separated from their surroundings. In this article, we will explore various border types in CSS and provide code examples to demonstrate their usage.

Basic Border Syntax

Before delving into specific border types, let's understand the basic CSS border syntax. Borders can be applied to various HTML elements, such as divs, paragraphs, or headings. The fundamental syntax for setting a border is as follows:

selector {
  border: border-width border-style border-color;
}
  • border-width determines the width of the border, which can be specified in various units like pixels, ems, or percentages.
  • border-style defines the type of border, which is the main focus of this article.
  • border-color specifies the color of the border.

The Border Types

We will explore the following CSS border styles:

.example-border {
  border: 2px dotted #FF5733; /* Dotted */
  border: 2px dashed #33FF57; /* Dashed */
  border: 2px solid #5733FF; /* Solid */
  border: 4px double #FF5733; /* Double */
  border: 2px groove #33FF57; /* Groove */
  border: 2px ridge #5733FF; /* Ridge */
  border: 2px inset #FF5733; /* Inset */
  border: 2px outset #33FF57; /* Outset */
  border: none; /* None */
  border: 2px hidden #5733FF; /* Hidden */
}

CSS Border Example

Let's see these border types in action by applying them to a set of HTML elements:

<!DOCTYPE html>
<html lang="en">
<head>
  <style>
    .dotted-border {
      border: 2px dotted #FF5733;
    }
    .dashed-border {
      border: 2px dashed #33FF57;
    }
    .solid-border {
      border: 2px solid #5733FF;
    }
    .double-border {
      border: 4px double #FF5733;
    }
    .groove-border {
      border: 2px groove #33FF57;
    }
    .ridge-border {
      border: 2px ridge #5733FF;
    }
    .inset-border {
      border: 2px inset #FF5733;
    }
    .outset-border {
      border: 2px outset #33FF57;
    }
    .none-border {
      border: none;
    }
    .hidden-border {
      border: 2px hidden #5733FF;
    }
    .border-example {
      margin: 10px;
      padding: 10px;
    }
  </style>
</head>
<body>
  <div class="border-example dotted-border">Dotted</div>
  <div class="border-example dashed-border">Dashed</div>
  <div class="border-example solid-border">Solid</div>
  <div class="border-example double-border">Double</div>
  <div class="border-example groove-border">Groove</div>
  <div class="border-example ridge-border">Ridge</div>
  <div class="border-example inset-border">Inset</div>
  <div class="border-example outset-border">Outset</div>
  <div class="border-example none-border">None</div>
  <div class="border-example hidden-border">Hidden</div>
</body>
</html>

In the HTML above, we have created ten div elements, each with a different border style applied using the corresponding CSS class. When you open this HTML file in a web browser, you'll see all the border styles in action, illustrating the visual differences between them.

Here is the output of the above code:css borders types

This guide should help you understand the various border types in CSS and how to apply them to your web design projects. CSS borders are a versatile tool for adding visual appeal and structure to your web page elements.