border-width is a CSS property that sets the width of an element’s border.
It can be applied to any element that supports borders, such as div, p, img, etc.
The property accepts various values, including:
- Keywords:
thin,medium,thick(relative to the element’s font size) - Numeric values: Pixels (px), ems (em), rems (rem), percentages (%), etc.
- Multiple values: Separate values for top, right, bottom, and left borders (e.g.,
1px 2px 3px 4px)
Using border-width
- Target the element:
- Use a CSS selector to identify the element you want to style.
- For example,
divtargets all<div>elements.
- Set the
border-widthproperty:
element {
border-width: value;
}
Replace element with the target selector and value with the desired border width.
Examples
- Set a 2-pixel border around all
<div>elements:
div {
border-width: 2px;
}
- Set a 10-pixel thick border on the top and bottom of a
<p>element:
p {
border-width: 10px 0 10px 0;
}
- Set a border that scales with the element’s font size:
h1 {
border-width: 0.2em;
}
Additional notes
- To set the border style (solid, dashed, dotted, etc.), use the
border-styleproperty. - To set the border color, use the
border-colorproperty.You can combine these properties to create custom borders.
Best practices
Be mindful of browser compatibility when using relative units (em, rem).
Consider using a CSS preprocessor (like Sass or Less) for more complex border styles and calculations.
Test your styles in different browsers and devices to ensure consistency.