In CSS, borders are used to create visible boundaries around elements. Borders can be applied to various HTML elements, such as divs, paragraphs, images, and more. You can control the border's width, style, and color to achieve different visual effects. The border property is used to define the border around an element, and it has three main components: border-width, border-style, and border-color.

Here's the syntax of the border property:

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

 

Now let's break down each part of the border property and provide examples for each:

1) Border Width: The border-width property specifies the width of the border. It can take values like thin, medium, thick, or specific length values (e.g., 1px, 2px, etc.).

/* Example using border-width */
.box {
  border-width: 2px;
}

 

Above example sets the border width of the elements with class "box" to 2 pixels.

2) Border Style: The border-style property defines the style of the border. It can take values like solid, dashed, dotted, double, groove, ridge, inset, outset, and more.

/* Example using border-style */
.element {
  border-style: dashed;
}

 

Above example defines a dashed border style for elements with class "element."

3) Border Color: The border-color property sets the color of the border. It can be specified using named colors, hexadecimal notation, RGB values, or HSL values.

/* Example using border-color */
.container {
  border-color: #f0f0f0;
}

 

Above example sets the border color of elements with class "container" to the specified color.

4) Using border shorthand: Similar to the background property, you can use the border shorthand property to combine all border-related properties into a single line.

/* Example using border shorthand */
.image-frame {
  border: 2px solid #ccc;
}

 

Above example combines border width, style, and color using the border shorthand property for elements with class "image-frame."

You can mix and match these properties to create various border effects for your elements. Additionally, you can apply borders to specific sides of an element (e.g., border-top, border-right, etc.) if you need more fine-grained control over the border styling.

Here's an example that demonstrates using borders on an HTML element:

<!DOCTYPE html>
<html>
<head>
  <title>Border Example</title>
  <style>
    /* CSS styles */
    .box {
      width: 200px;
      height: 100px;
      border: 2px solid #f0f0f0;
      border-radius: 10px;
      padding: 20px;
    }
  </style>
</head>
<body>
  <!-- HTML content -->
  <div class="box">
    <p>This is a box with a border.</p>
  </div>
</body>
</html>

 

In this example, we have a div element with the class "box." The CSS styles set a solid border of 2 pixels width and a light gray color (#f0f0f0). We also add some padding and a border-radius to create rounded corners. The result is a box with a visible border around its content.

Latest Tutorial

Newly published tutorials

Laravel 10 - Send mail using AWS SES

Learn to send Email using Laravel 10 with AWS SES service.

How to Make an AWS S3 Bucket Public

Learn how we can make an AWS S3 bucket public

Laravel 10 - Tutorial to create CRUD Application

Learn to create crud application using laravel 10. Step by step guidance will help to understand the concept of crud.