border-width
Definition
The border-width property in CSS sets the width of an element's border. The width can be set using a length value (such as pixels or ems), or using one of the following keywords: thin, medium, or thick. The border-width property can also be set for individual sides of an element using the border-top-width, border-right-width, border-bottom-width, and border-left-width properties.
Examples
To set the width of all four borders of an element to 3 pixels:
div {
border-width: 3px;
}
To set the width of the top and bottom borders to 2 pixels and the width of the right and left borders to 1 pixel:
div {
border-top-width: 2px;
border-right-width: 1px;
border-bottom-width: 2px;
border-left-width: 1px;
}
To set the width of an element's border to the keyword "medium":
div {
border-width: medium;
}
To set the width of an element's border to the value of a variable:
:root {
--border-width: 5px;
}
div {
border-width: var(--border-width);
}
Values
Value | Description |
---|---|
thin | Thin border |
medium | Medium border |
thick | Thick border |
Note: The border-width property in CSS can also take a specific value in pixels (e.g. 10px) or other length units (e.g. 1em, 2vw).
Best Practices
- Be consistent: Use the same border width for all elements that are meant to have the same visual appearance.
- Use relative units: Use relative units like em or rem instead of pixels, so that the border width is responsive and adjusts based on the font size.
- Consider accessibility: Keep in mind that some users may have difficulty distinguishing elements with thin borders. Use a thicker border or other visual cues to make it easier for these users to navigate your website.
- Use appropriate values: The values 'thin', 'medium' and 'thick' are appropriate for simple designs and rough borders, but for more precise designs, it's best to use pixel values.
- Avoid overuse: Avoid using too many borders on a single page as it can make the design appear cluttered and overwhelming.
- Test on different devices and screen sizes: Test your design on different devices and screen sizes to ensure that the border widths look good and are easy to see on different screens.
- Use the shorthand property: Remember that the border-width property can be set using the shorthand property 'border', which allows you to set all four sides of the border at once.
Browser Compatibility
Chrome | Firefox | Safari | Internet Explorer | Microsoft Edge | Opera |
---|---|---|---|---|---|
Yes | Yes | Yes | Yes | Yes | Yes |