Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Set border width with CSS
The border-width property allows you to set the width of element borders. The value of this property could be either a length in px, pt or cm or it should be set to thin, medium or thick.
Syntax
border-width: value; /* Individual sides */ border-top-width: value; border-right-width: value; border-bottom-width: value; border-left-width: value; /* Shorthand for all sides */ border-width: top right bottom left;
Values
The border-width property accepts the following values:
- Length units: px, pt, em, rem, cm, mm
- Keywords: thin, medium, thick
- Global values: initial, inherit, unset
Example: Different Border Widths
<html>
<head>
<style>
.border-px {
border-width: 4px;
border-style: solid;
border-color: #333;
margin: 10px 0;
padding: 10px;
}
.border-pt {
border-width: 4pt;
border-style: dashed;
border-color: #666;
margin: 10px 0;
padding: 10px;
}
.border-keywords {
border-style: solid;
border-color: #999;
margin: 10px 0;
padding: 10px;
}
.thin { border-width: thin; }
.medium { border-width: medium; }
.thick { border-width: thick; }
</style>
</head>
<body>
<p class="border-px">
Border width: 4px with solid style
</p>
<p class="border-pt">
Border width: 4pt with dashed style
</p>
<p class="border-keywords thin">
Border width: thin
</p>
<p class="border-keywords medium">
Border width: medium
</p>
<p class="border-keywords thick">
Border width: thick
</p>
</body>
</html>
Individual Side Properties
You can set different widths for each side of the border:
<html>
<head>
<style>
.individual-sides {
border-top-width: 2px;
border-right-width: 4px;
border-bottom-width: 6px;
border-left-width: 8px;
border-style: solid;
border-color: #e74c3c;
padding: 15px;
margin: 20px 0;
}
.shorthand {
border-width: 2px 4px 6px 8px;
border-style: solid;
border-color: #3498db;
padding: 15px;
margin: 20px 0;
}
</style>
</head>
<body>
<p class="individual-sides">
Individual side properties: top(2px), right(4px), bottom(6px), left(8px)
</p>
<p class="shorthand">
Shorthand syntax: border-width: 2px 4px 6px 8px
</p>
</body>
</html>
Comparison of Units
| Unit | Description | Best Use Case |
|---|---|---|
| px | Pixels (absolute) | Precise control, web layouts |
| pt | Points (1pt = 1/72 inch) | Print stylesheets |
| em | Relative to font size | Scalable designs |
| thin/medium/thick | Browser-defined keywords | Quick prototyping |
Key Points
- Border width requires
border-styleto be visible - Default border width is
medium(usually 3px) - Shorthand accepts 1-4 values (top, right, bottom, left)
- Negative values are not allowed
Conclusion
The border-width property provides flexible control over border thickness using length units or keywords. Remember to set border-style for the border to be visible.
Advertisements
