How to Implement Different Borders in CSS?

Last updated on Jun 19,2023 1.8K Views

How to Implement Different Borders in CSS?

edureka.co

Borders are used in HTML pages to demarcate and highlight content. When there is plenty of information on a page and you wish to draw the user’s attention to specific parts, then use borders around that content. In this article on Borders in CSS I’ll discuss the following topics:

 

When to use Borders

The standard practice is to use CSS border tags to define borders in HTML pages for:

You might want to read up on the CSS basics before getting on with learning about CSS borders.

For a comprehensive CSS tutorial, visit Edureka CSS Tutorial For Beginners. You will get an excellent heads-up on the way CSS is to be used to augment HTML web design.

 

Borders in CSS

CSS Borders can be assigned to different sections of the HTML page, whether it is to enclose headings or paragraphs. Let’s start with an example. Here we define a border around the heading and another border around the paragraph text.


<!DOCTYPE html>
<html>
<head>
<style>
body {
background-color: lightblue;
}

h1 {
text-align: center;
border-style: solid;
}

p {
font-family: verdana;
font-size: 20px;
border-style: dotted;
}

</style>
</head>
<body>

<h1>My First Border in the heading</h1>
<p>Border around the paragraph too!</p>

</body>
</html>

 

Attributes of CSS Border

CSS borders have 3 major attributes to their

In the above example, we saw that only one border attribute is defined, that is its style. The other attributes when not defined take on default values. There is another attribute called radius, which is less frequently used. It is used to make the edges of the border rounded.

StyleDescription
border-style: solid
border-style: double
border-style: groove
border-style: ridge
border-style: inset
border-style: outset
border-style: none
border-style: hidden
border-style: dotted
border-style: dashed

 

You would notice that there is a ‘no border’ and ‘hidden border’ option as well. A developer can simply avoid defining a border, why explicitly define it as a ‘hidden border’? This is done for space usage and alignment purposes with other elements in the page.

Border styles can be mixed within an element too. For this purpose, the 4 individual border sides can be separately defined with different styles. This can be done in 2 ways. Either define all the 4 sides in a single tag. In this case, the count starts from the top line and then moves clockwise. Alternatively, each of the 4 borderlines can be defined in individual tags too. Both cases are shown in the next Example.

StyleDescription
border-style: dotted dashed solid double

border-top-style: dotted;

border-right-style: dashed;

border-bottom-style: solid;

border-left-style: double;

 

The color attribute for a border can be set in multiple ways. This is similar to setting color for other elements. The colors can be set by one of the following methods:

 

The width property, as the name suggests, defines the thickness of the 4 border sides. If one value is defined, it is for all sides, otherwise individual width values can also be set.

The width can be specified in any of the standard unit such as pixels(‘px’), points(‘pt’) or in centimeters(‘cm’). You can also specify the width using pre-defined values of ‘thick’, ‘thin’ and ‘medium’.

StyleDescription
border-width: 10px
border-width: 0.1cm
border-width: medium

 

The purpose of defining radius is to get rounded corners for the border. The radius factor defines the extent of roundedness. Larger the value, more curved the corners become. As a standard practice, radius values are kept between 1-3 times the border width.

The following code will generate:

border-width: 10px;
border-radius: 30px;

 

Default Values for Border Attributes

Build stunning cross-platform mobile apps with our comprehensive Flutter Training.

 

Define Borders in Shorthand

Some developers prefer to define border attributes in a concise one-line tag. This shorthand format helps in minimizing the lines of code and expert developers prefer this format. Using the shorthand format is recommended when the border definition is simple and fairly standardized. However, if you need to highlight each side of the border in a different style, then you need to stick to the format of defining individual tags.

The Following code is used:

border-style: dashed;
border-color: green;
border-width: 5px;
border: dashed green 5px;

 

Points to keep in mind while Designing Borders in CSS

Hope you found the information you were looking for on CSS borders, and with this, we come to an end of this Borders in CSS article.

Check out our Full Stack Web Developer Masters Program which comes with instructor-led live training and real-life project experience. This training makes you proficient in skills to work with back-end and front-end web technologies. It includes training on Web Development, jQuery, Angular, NodeJS, ExpressJS, and MongoDB.

Got a question for us? Please mention it in the comments section of “Border in CSS” blog and we will get back to you.

Upcoming Batches For Full Stack Developer Course
Course NameDateDetails
Full Stack Developer Course

Class Starts on 18th May,2024

18th May

SAT&SUN (Weekend Batch)
View Details
BROWSE COURSES
REGISTER FOR FREE WEBINAR UiPath Selectors Tutorial