Border in CSS: How to Add a Border in CSS like a pro
Border in CSS: Ever wondered how to create stylish borders around elements in CSS? either a fancy gradient border, simple border or even a borderless button, CSS will get the job done for you. This article will help you to understand borders in CSS better, from the basics to advanced techniques.
Table of contents
What is the Border Property in CSS?
The border
property in CSS allows you to define the style, width, and color of an element’s border. You can apply it to divs, buttons, images, or any other HTML elements.
Here’s a simple example:
div {
border: 2px solid black;
}
This adds a 2-pixel solid black border around a <div>
element.
How to Add a Border in CSS
There are multiple ways to add a border in CSS. Let’s break it down.
1. Using the border
Shorthand Property
Instead of setting border properties separately, you can define everything in one line using the border
shorthand.
p {
border: 3px dashed blue;
}
This applies a 3-pixel blue dashed border to a paragraph (<p>
).
2. Using Individual Border Properties
You can also define each border property separately:
h1 {
border-width: 4px;
border-style: double;
border-color: red;
}
This applies a 4px red double border around an <h1>
element.
How to Make a Border in CSS
CSS provides different styles for borders. Here are some commonly used ones:
Border Style | Description | Example |
---|---|---|
solid | A single, solid line | border: 2px solid black; |
dashed | A dashed line | border: 2px dashed red; |
dotted | A dotted line | border: 2px dotted blue; |
double | Two solid lines | border: 4px double green; |
groove | A 3D groove effect | border: 5px groove gray; |
ridge | A 3D ridge effect | border: 5px ridge orange; |
inset | Gives an inset effect | border: 5px inset purple; |
outset | Opposite of inset, appears raised | border: 5px outset pink; |
Here’s how you can use them:
.box {
border: 5px ridge orange;
}
This applies a 5-pixel ridge border to the .box
class.
How to Remove Border from a Button in CSS
By default, buttons have a border. If you want a clean, modern button without a border, simply use border: none;
button {
border: none;
background-color: blue;
color: white;
padding: 10px 20px;
font-size: 16px;
}
This removes the border and styles the button with a blue background and white text.
If you want to remove the outline that appears when clicking the button, add:
button:focus {
outline: none;
}
How to Create a Gradient Border in CSS
Gradient borders can make your design look stylish. However, CSS doesn’t support gradient colors directly in the border
property. The trick is to use background-clip
with a pseudo-element.
Method 1: Using Background-Clip
.gradient-border {
border: 5px solid transparent;
border-image: linear-gradient(to right, red, blue);
border-image-slice: 1;
}
This creates a red-to-blue gradient border around the element.
Method 2: Using a Pseudo-Element
For more control, you can use ::before
to create a fancy border effect.
.gradient-box {
position: relative;
padding: 20px;
background-color: white;
}
.gradient-box::before {
content: "";
position: absolute;
top: -5px;
left: -5px;
right: -5px;
bottom: -5px;
background: linear-gradient(to right, pink, purple);
z-index: -1;
}
This method creates a glowing gradient border effect.
Border Radius: Making Rounded Borders
If you want a smooth, rounded border, use border-radius
.
.circle {
width: 100px;
height: 100px;
border-radius: 50%;
border: 3px solid red;
}
This creates a perfect circle.
Border Animation in CSS
Want an animated border? Try this effect using @keyframes
.
@keyframes border-animation {
0% { border-color: red; }
50% { border-color: blue; }
100% { border-color: red; }
}
.animated-border {
border: 5px solid red;
animation: border-animation 2s infinite;
}
This changes the border color every 2 seconds.