You can imagine borders as the outlines that go around different parts of a webpage, such as boxes, images, buttons, or text areas. CSS Borders can be used by users and developers to highlight important information and separate one section from other sections, or simply make your website look better.
By using the CSS border property, you can easily add outlines to the HTML elements, choose the thickness of the border using the border-width property, define the line style it should have, like solid, dashed, or dotted, and also the color of the border. In this blog, you will learn how to style borders using CSS properties with real-world use cases and examples.
Table of Contents:
What is CSS?
CSS stands for Cascading Style Sheets. It is a stylesheet language that is used to style web pages on the internet. It controls the look of the HTML elements on the website, such as its colors, sizes, spacing, fonts, and borders.
CSS Border Properties
A CSS border is like an outline that you can put around an HTML element, such as a box, image, or button. It can separate an HTML element from other content on the webpage. By using CSS properties, you can easily change this border according to your needs. Here are some important CSS properties that help you to style the borders.
CSS border-width Property
The CSS border-width property is used to control the width of the border, which means how thick or thin a border appears all around an element. You can set the border width by using units like pixels (px) or em, or you can also use predefined keywords like thin, medium, or thick to set the CSS border-width.
Syntax:
Selector {
border-width: 3px;
}
You can also set the border-width individually for each side of the elements by using CSS properties like the border-top-width, border-right-width, border-bottom-width, and border-left-width.
Become a Full-Stack Web Developer
Enroll Now
CSS border-style Property
This defines the appearance of the border line. This border-style property is very helpful because it tells the browser what kind of line the border should be. Without setting a border style, the border is not shown on screen, even if you set its width and color. Here is the list of values that you can use to set the border style in CSS:
Border Style |
Description |
Syntax |
Dotted |
A border made from small dots. |
border-style: dotted; |
Dashed |
It is the border made from short dashes. |
border-style: dashed; |
Solid |
A simple straight line. |
border-style: solid; |
Double |
Two solid lines next to each other. |
border-style: double; |
Groove |
Looks like the border is carved into the page, giving a 3D effect. |
border-style: groove; |
Inset |
Pushes the border into elements, creating a 3D effect. |
border-style: inset; |
Outset |
Makes the border appear as if it’s popping out from the element. |
border-style: outset; |
Ridge |
Looks like the border is raised from the page (3D effect). |
border-style: ridge; |
None |
No border is displayed. |
border-style: none; |
Hidden |
The border is not shown, similar to “none”. |
border-style: hidden; |
You can also set the border style for each side individually by using CSS properties like border-top-style, border-bottom-style, border-left-style, and border-right-style.
CSS border-color Property
The CSS border-color property allows users to change the color of the border. For changing the color, you can use the color names like red or blue, or other options like HEX codes, like #ff0000, and RGB values like rgb(255, 0, 0).
Syntax:
Selector {
border-color: purple;
}
You can use CSS properties like border-top-color, border-right-color, border-bottom-color, and border-left-color to set the colors individually for each side.
CSS border-radius Property
The border-radius property is used to round the corners of the border. Instead of having square edges, the border-radius property makes the corners of the elements smooth and curved. It will give a modern look to your design.
Syntax:
Selector {
border-radius: 10px;
}
Example: Combining All Border Properties
Output:
CSS Border Shorthand Property
Instead of writing different lines for the border width, border style, and border color, you can use the CSS border shorthand property to set all of these properties only in one line. This CSS border shorthand property helps keep your code short and easy to read.
Syntax:
Selector {
border: 2px solid blue;
}
Here, you can set everything, including border width, border style, and border color, in a single line. The border-width is 2px, the border-style is solid, and the border-color is blue.
How to Add a CSS Border on a Specific Side?
Sometimes, you may want to add a border only on one side of an element instead of all four sides. To do this, you can use any CSS property like the border-top, border-bottom, border-left, and border-right to add a border according to you.
Example: A black box with a green border only at the top.
Output:
Practical Use Cases of CSS Borders
The CSS border properties are not only used for decorating HTML elements. They play an important role in enhancing the structure and complete design of your website. Here are some popular use cases of the CSS border in web development projects:
- You can use CSS border-style and CSS border-color properties to mark the important sections in a website, like warnings, headings, or call-to-action buttons.
- You can style your buttons by using the CSS border property and background-color property.
- You can use CSS border-top, CSS border-bottom, or borders for individual sides to style navigation menus.
- CSS borders are also used to create boxes or divide different sections of the webpage.
Get 100% Hike!
Master Most in Demand Skills Now!
Conclusion
Borders in CSS are an easy and important tool to design and manage your website content. You can use borders to build structure, mark messages, or style buttons. By understanding properties like CSS border-radius, CSS border-style, CSS border-width, and CSS border-color, you can make your web pages look better. You can also use CSS border-top and CSS border-bottom to add a custom border to web pages.
CSS Borders – FAQs
Q1. What is a border in CSS?
A border in CSS basically refers to the line around an element that helps users to separate content from one another.
Q2. What is CSS border-color?
It is the property in CSS that is used to change the color of a border. It uses different values, like color names, HEX, RGB, or HSL values.
Q3. How many border types are in CSS?
CSS has different types of border styles like solid, dashed, dotted, double, groove, ridge, inset, and outset.
Q4. How to align text in CSS?
Use the text-align property in CSS to change the alignment of the text.
Q5. How to set font size in CSS?
You can use the font-size property in CSS to change the font size. It uses various units like px, em, or %.