Technology Encyclopedia Home >How to set border style with CSS?

How to set border style with CSS?

To set the border style of an HTML element using CSS, you can use the border property or its individual components: border-width, border-style, and border-color. Here's a brief explanation and example:

Explanation:

  1. Using the border shorthand property: You can set the width, style, and color of the border in a single line using the border property.

Example:

p {
  border: 2px solid black;
}

In this example, the paragraph element (<p>) will have a 2-pixel wide, solid black border.

  1. Using individual border properties: You can also set each aspect of the border separately using border-width, border-style, and border-color.

Example:

p {
  border-width: 2px;
  border-style: solid;
  border-color: black;
}

This will produce the same result as the previous example.

CSS Border Styles:

There are several border styles you can use with the border-style property, such as:

  • solid: A single solid line.
  • dashed: A series of square-ended dashes.
  • dotted: A series of round dots.
  • double: Two parallel solid lines.
  • groove: A groove effect.
  • ridge: A ridge effect.
  • inset: An inset effect.
  • outset: An outset effect.

Example with different border styles:

p {
  border-width: 2px;
  border-style: dashed;
  border-color: blue;
}

In this example, the paragraph element will have a 2-pixel wide, dashed blue border.

If you're working on a project that requires scalable and reliable hosting, consider using Tencent Cloud's services, which offer a variety of solutions for web development and hosting.