How To Add Border In HTML?
Last Updated :
04 Sep, 2024
Adding Borders to HTML elements is a common way to enhance the presentation and layout of web pages. Borders can be added to divs, paragraphs, images, and tables to separate or highlight content visually. CSS can provide several properties to customize the borders, such as color, style, and width.
There are multiple ways to add the borders in the HTML using CSS:
1. Using the border Property
The border property in CSS is the shorthand way to set the border width, style, and color in one line. This property can be applied to most HTML elements and is used to quickly define the borders of the element. The border will wrap around the content of the element, providing a visual boundary.
Syntax:
border: width style color;
Example : In this example, the div element is given the blue border that is 2 pixels thick and solid. The border can completely surround the content within the div.
HTML
<!DOCTYPE html>
<html>
<head>
<style>
.box {
border: 2px solid blue;
}
</style>
</head>
<body>
<h1 style="color: green">GeeksforGeeks</h1>
<div class="box">This div has a solid blue border.</div>
</body>
</html>
Output:
Using the border Property2. Using the Individual Border Properties
CSS can allows you to specify the each aspect of the border individually: width, style, and color. This method can provides the more flexibility as you can set the different widths, styles, or colors independently for the each property. This approach can be particularly useful if you want different attributes for the different sides of the border.
Syntax:
border-width: width;
border-style: style;
border-color: color;
Example : In this example, the div element can be given the red color. The width, style and color are set separately using the 3 pixels for the width, a dashed style, and the red color.
HTML
<!DOCTYPE html>
<html>
<head>
<style>
.box {
border-width: 3px;
border-style: dashed;
border-color: red;
}
</style>
</head>
<body>
<h1 style="color: green;">GeeksforGeeks</h1>
<div class="box">This div has a red dashed border.</div>
</body>
</html>
Output:
Using the Individual Border Properties3. Adding the Borders to Specific Sides
Instead of applying the border to all sides, we can apply the border only to specific sides of the element using the border-top, border-bottom, border-right, and border-left, It can be useful for the styling elements where only certain sides need to be highlighted or separated.
Syntax:
border-top: width style color;
border-right: width style color;
border-bottom: width style color;
border-left: width style color;
Example: In this example, the div styled to have the green solid border on the top side and the purple dotted border on the right side. It will highlighted only the specified sides of the div.
HTML
<!DOCTYPE html>
<html>
<head>
<style>
.box {
border-top: 5px solid green;
border-right: 5px dotted purple;
}
</style>
</head>
<body>
<h1 style="color: green;">GeeksforGeeks</h1>
<div class="box">This div has borders on the top and right sides.</div>
</body>
</html>
Output:
Adding the Borders to Specific Sides4. Using the outline Property
The outline property can be similar to the border property but differ in that it does not take up space or affect the layout of the element. It can be drawn outside the elements border and is often used for the accessibility purposes (ex: highlighting the elements on focus).
Syntax:
outline: width style color;
Example: In this example, the div element is given the orange dashed outline that is 3 pixels wide. This outline will not alter the layout but will visually highlight the element.
HTML
<!DOCTYPE html>
<html>
<head>
<style>
.box {
outline: 3px dashed orange;
}
</style>
</head>
<body>
<h1 style="color: green;">GeeksforGeeks</h1>
<div class="box">This div has an orange dashed outline.</div>
</body>
</html>
Output:
Using the outline Property5. Using the Classes for Reusable Borders
Defining the CSS class with a specific border style allow you to apply the same border to the multiple elements by just assigning the class to the each element. This approach can promotes the consistency and reusability across the different parts of the webpage.
Syntax:
.border-style {
border: width style color;
}
Example: In this example, the class named .border-style can be defined with the double black border. This class can be applied to the both the div and p element to determine the reusability.
HTML
<!DOCTYPE html>
<html>
<head>
<style>
.border-style {
border: 2px double black;
}
</style>
</head>
<body>
<h1 style="color: green;">GeeksforGeeks</h1>
<div class="border-style">First div with border.</div>
<p class="border-style">Paragraph with the same border.</p>
</body>
</html>
Output
Using the Classes for Reusable Borders6. Applying the Border through Inline CSS
We can apply the border directly to the HTML element using the style attribute within the tag itself. This method is simple but not recommended for the extensive use, as it leads to the inline styles that can clutter HTML and make maintenance harder.
Syntax:
<element style="border: width style color;">
Example: In this example, the div element is given a 1-pixel solid gray border directly through the style attribute in the HTML tag.
HTML
<!DOCTYPE html>
<html>
<body>
<h1 style="color: green;">GeeksforGeeks</h1>
<div style="border: 1px solid gray;">
This div has a gray border.
</div>
</body>
</html>
Output
Applying the Border through Inline CSS
Similar Reads
How To Create A Box in HTML?
In HTML, you can create a "box" using several techniques, such as using <div> elements and styling them with CSS. These boxes can be used for various purposes, such as creating layouts, buttons, or sections within a webpage.Method 1. Div Element with CSSThe <div> element is a block-level
3 min read
How to Add Space Around Table Border in HTML ?
Crafting visually appealing tables often involves adjusting the spacing between the content and the border. This guide dives into effective methods using HTML and CSS to achieve the desired spacing around your table's border, enhancing both readability and aesthetics. Below are the approaches to add
2 min read
How to add horizontal line in HTML ?
Creating a visually appealing and well-structured webpage often involves the use of horizontal lines. These lines help separate different sections of content, making it easier for users to read and understand the information presented. In this guide, weâll explore two effective methods to add horizo
2 min read
How to Create Table Border in HTML ?
In HTML, we can represent the data in the tabular format by using the <table> tag. We can customize the table by creating a border with different widths and colors. Below are the approaches to create a Table Border in HTML: Table of Content Using HTML Table Tag AttributesUsing HTML Inline Styl
2 min read
How to Create a Collapsed Border in HTML ?
The border-collapse is a Property in CSS that is used to set the table borders. It should collapse into a single border or be separated from its border in HTML. Syntax:border-collapse: separate|collapse|initial|inherit;Properties:separate:Â This property is used to set a separate border of a cell. Th
2 min read
How to Set a Border for an HTML Div Tag ?
Borders help us make things look good on websites, this plays an important role in the external view of a website. In HTML and CSS, we have different ways to add borders, making it easier for developers to create attractive web pages that look neat and organized. Table of Content Inline StylesIntern
4 min read
How to Add Borders to Pagination in CSS ?
Add borders to Pagination is when you have many pages of content and must move between them. Making these page buttons look better can make navigating easier and more enjoyable for users. One way to do this is by adding borders to the page buttons using CSS. These are the following approaches: Table
3 min read
How to Create Table in HTML?
HTML tables are used for organizing and displaying data in a structured format on web pages. Tables are commonly used for product information, presenting data analytics, or designing a pricing comparison chart. Elements of HTML TableTable ElementsDescription<table>The <table> element def
3 min read
How to add space between elements in HTML?
Spaces in HTML are essential for improving layout and readability. To add space between elements, such as rows in a table, you can use CSS properties like margin, padding, and border. Simple elements like <br> (line break) or <hr> (horizontal line) can also create space between sections.
4 min read
HTML | <img> border Attribute
The <img> border attribute is used to specify the border width around the image. The default value of <img> border attribute is 0. It is not supported by HTML 5. Use CSS instead of this attribute. Syntax: <img border="pixels"> Attribute Values: It contains single value pixels which
1 min read