The Wayback Machine - https://web.archive.org/web/20240917110633/https://www.geeksforgeeks.org/css-borders/
Open In App

CSS Borders

Last Updated : 06 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

A CSS border is a line that surrounds an HTML element, providing a visual separation between the element and its surroundings. Borders can be styled in various ways, including adjusting their width, color, and style. The basic properties used to define borders in CSS are border-width, border-style, and border-color.

This Borders in CSS article will teach us about CSS borders, covering styling options, practical use cases, and best practices.

Basic Syntax

The simplest way to add a border to an element is by using the border shorthand property. Here’s the basic syntax:

element {
border: 1px solid black;
}

In this example, the border is defined as 1 pixel wide, solid in style, and black in color.

Border Properties

CSS provides several properties to customize borders:

PropertyDescription
border-styleDetermines the type of border (e.g., solid, dashed, dotted).
border-widthSets the width of the border (in pixels, points, or other units).
border-colorSpecifies the border color.
border-radiusCreates rounded corners for elements.

Ways to Style Border in CSS

The CSS border property enables the styling of an element’s border by setting its width, style, and color, allowing for customizable visual boundaries in web design.

1. Border Style

2. Border Width

3. Border Color

4. Border individual sides

5. Border radius property

Common Border Styles

The border-style property specifies the type of border. None of the other border properties will work without setting the border style. 

Following are the types of borders:

Border StyleDescription
DottedCreates a series of dots.
DashedForms a dashed line.
SolidProduces a continuous line.
DoubleRenders two parallel lines.
GrooveCreates 3D grooved effect.
RidgeCreates 3D ridged effect.
InsetAdds 3D inset border.
OutsetAdds 3D outset border.
NoneRemoves the border.
HiddenHides the border.

Example : In this example Paragraphs feature varied border styles (dotted, dashed, solid, double) via CSS classes (.dotted, .dashed, .solid, .double). This enhances visual presentation, demonstrating distinct styles for each paragraph.

HTML
<!DOCTYPE html>
<html>

<head>
    <style>
        p.dotted {
            border-style: dotted;
        }

        p.dashed {
            border-style: dashed;
        }

        p.solid {
            border-style: solid;
        }

        p.double {
            border-style: double;
        }
    </style>
</head>

<body>
    <h2>The border-style Property</h2>

    <p>Geeksforgeeks</p>


    <p class="dotted">A dotted border.</p>

    <p class="dashed">A dashed border.</p>

    <p class="solid">A solid border.</p>

    <p class="double">A double border.</p>

</body>

</html>

Output: 

CSS border-style Example Output

CSS Border Width

Border width sets the width of the border. The width of the border can be in px, pt, cm or thin, medium, and thick.

Example : In this example Two paragraphs have an 8-pixel solid border defined by CSS. This border enhances visual separation, and its thickness can be adjusted using the `border-width` property for a desired effect.

HTML
<!DOCTYPE html>
<html>

<head>
    <style>
        p {
            border-style: solid;
            border-width: 8px;
        }
    </style>
</head>

<body>
    <p>
        Geeksforgeeks
    </p>
    <p>
        Border properties
    </p>
</body>

</html>

Output: 

CSS Border Width Example Output

CSS Border Color

This property is used to set the color of the border. Color can be set using the color name, hex value, or RGB value. If the color is not specified border inherits the color of the element itself.

Example : In this example Two paragraphs have a solid red border set with the `border-color` property, enhancing visual distinction. The border color can be customized by specifying different color values.

HTML
<!DOCTYPE html>
<html>

<head>
    <style>
        p {
            border-style: solid;
            border-color: red
        }
    </style>
</head>

<body>
    <p>
        Geeksforgeeks
    </p>
    <p>
        Border properties:color
    </p>
</body>

</html>

Output: 

CSS Border Color Example Output

CSS Border individual sides:

Using border property, we can provide width, style, and color to all the borders separately for that we have to give some values to all sides of the border.

Syntax: 

border-top-style : dotted;
border-bottom-width: thick;
border-right-color: green;

Example: In this example, A single <h2> heading features a dotted top border set with the `border-top-style` property. CSS allows various border styles to enhance element appearance.

HTML
<!DOCTYPE html>
<html>

<head>
    <style>
        h2 {
            border-top-style: dotted;
        }
    </style>
</head>

<body>
    <h2>Welcome to GeeksforGeeks</h2>
</body>

</html>

Output:

CSS Border individual sides Example Output

Border radius property

The CSS border-radius property rounds the corners of an element’s border, creating smoother edges, with values specifying the curvature radius.

Syntax:

border-radius: value;

Example : In this example A single <h1> heading has a solid border, centered text (using `text-align`), a green background, and rounded corners (using `border-radius`), creating a visually appealing header element.

HTML
<!DOCTYPE html>
<html>

<head>
    <style>
        h1 {
            border-style: solid;
            text-align: center;
            background: green;
            border-radius: 20px;
        }
    </style>
</head>

<body>
    <h1>GeeksforGeeks</h1>
</body>

</html>

Output:

Border radius property Example Output

Practical Use Cases

CSS borders find application in various scenarios:

  • Styling Buttons: Borders enhance button appearance.
  • Creating Dividers: Use borders to separate content sections.
  • Customizing Images: Add borders to image thumbnails.
  • Designing Navigation Menus: Borders define menu items.

CSS Borders Use Cases:

1. How to define the color of the border using CSS ?

to define the color of border we use the border-color property in CSS to define the color of the border.

2. How to specify the double border using CSS ?

Here we Use the border-style property set to double and adjust the border-width property to control the thickness of the double border.

3.How to Add Border Around Text using CSS ?

Here we use the border property with values for style, width, and color to add a border around text in CSS.

4. How to create a Border around an HTML Element using CSS ?

To create a border around an HTML element using CSS, use the border property to specify style, width, and color.

5.How to create a hidden border using CSS properties ?

To create a hidden border using CSS properties, set the border-style property to hidden or none to hide the border.

CSS Borders Supported Browsers:

CSS Borders – FAQs

What are CSS borders?

CSS borders are used to create lines around HTML elements, enhancing visual separation and styling.

How to set a border in CSS?

Use the border property to set the border width, style, and color. For example, border: 1px solid black;.

What are the different border styles available in CSS?

Common border styles include solid, dashed, dotted, double, groove, ridge, inset, outset, and none.

How to set the width of a border?

Use the border-width property to set the border’s thickness. For example, border-width: 2px;.

How to set the color of a border?

Use the border-color property to set the border’s color. For example, border-color: red;.

How to set individual border sides?

Use border-top, border-right, border-bottom, and border-left properties to style individual sides. For example, border-top: 1px solid black;.

What is the shorthand property for borders?

The shorthand border property sets width, style, and color in one declaration. For example, border: 2px dashed blue;.



Previous Article
Next Article

Similar Reads

Pure CSS Table with Horizontal Borders
A table is an arrangement of data in rows and columns, or possibly in a more complex structure. Tables are widely used in communication, research, and data analysis. In Pure CSS, we will add a "pure-table" class to add styles to the table. This class adds padding and borders to table elements and emphasizes the header. A table with Horizontal Borde
2 min read
Tailwind CSS Borders Complete Reference
Tailwind CSS is a highly customizable, low-level CSS framework that gives you all of the buildings blocks that you need. Tailwind CSS Border utility settings the radius, width, color, opacity, etc., of element's borders. all the properties covered as in this class form. Tailwind CSS Class Description Example Border RadiusIt is used to set the borde
2 min read
Primer CSS Responsive Borders
Primer CSS is a free open-source CSS framework that is built upon systems that create the foundation of the basic style elements such as spacing, typography, and color. This systematic method makes sure our patterns are steady and interoperable with every other. Its approach to CSS is influenced by Object-Oriented CSS principles, functional CSS, an
3 min read
Primer CSS Borders
Primer CSS is a free and open-source CSS framework. It is built upon the systems that create the foundation of the basic style elements such as spacing, typography, and color. Created with GitHub’s design system, it is highly reusable and flexible. Primer CSS Borders is a utility class that applies borders to a div container of HTML by simply apply
3 min read
Pure CSS applying Padding and Borders to Grid Units
Pure CSS is a free and open-source CSS framework. It is a set of instruments for developing responsive websites and online applications. This was created by Yahoo and is used to create websites that are quicker, more aesthetically beautiful, and more user-friendly. It effectively replaces Bootstrap. The responsive design was considered when creatin
3 min read
How to add Borders and Shadows to Elements in CSS ?
Apply box shadows to elements to create a 3D effect, whereas adding borders to containers or boxes can enhance the structural clarity of a layout. In this article, we will learn how to add borders and shadows to the elements in CSS. We will explore two different approaches to adding borders and shadows. Table of Content Using the border and box-sha
2 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 of ContentUsing Border PropertyUsing Border-Style
3 min read
Gradient borders in CSS
Creating visually appealing borders can significantly enhance the user experience on your website. One such technique is the use of gradient borders. Although CSS does not directly support gradient borders, there are two effective methods to achieve this: Try It: .custom-item { border: 1px solid gray; font-family: 'Gill Sans', 'Gill Sans MT', Calib
4 min read
How to remove borders of a canvas-type text using Fabric.js ?
In this article, we are going to see how to remove the borders of a canvas-like text using FabricJS. The canvas means text written is movable and can be stretched according to requirement. Further, the text itself cannot be edited like a textbox.Approach: To make it possible we are going to use a JavaScript library called FabricJS. After importing
2 min read
How to set the collapsing borders model for a table ?
CSS describes the way HTML elements should be displayed. There are various CSS properties that can add magic to the basic HTML document. This article is focused on setting the collapsing borders model for the table element of HTML. HTML tables allow us to arrange our data using rows and columns. The &lt;table&gt; tag defines an HTML table. Each tab
3 min read
Borders in bootstrap with examples
Borders: Borders are generally used to display an outline around a box or table cell or any other HTML element. In Bootstrap, there are different classes available to add or remove borders. The classes that are used to add borders are referred as Additive classes and those that are used to remove borders are referred as subtractive classes. Additiv
4 min read
How to remove controlling borders of a canvas circle using Fabric.js ?
In this article, we are going to see how to remove the controlling borders of a canvas circle using FabricJS. The canvas means the circle is movable and can be stretched according to requirement. Further, the circle can be customized when it comes to initial stroke color, fill color, stroke width, or radius. Approach: To make it possible, we are go
2 min read
How to change Input box borders after filling the box using JavaScript ?
In this article, we will change the input border after filling in the text on the input text field. The onchange event attribute works when the value of the element changes and selects the new value from the list. Approach: Whenever the input value is changed with some value by the user, the onchange event attribute is triggered. After the event is
2 min read
React MUI Borders
Material UI is an open-source design framework that showcases different components made in react. It is developed and maintained by Google since 2014. What is Borders API offered by Material UI? The Borders API, provided by MUI, allows us to control the border and border-radius of DOM elements that have border utilities. passed to Borders API props
3 min read
Tachyons Theming Borders
CSS border properties allow us to set the style, color, and width of the border. Tachyon is a toolkit that is used to create a responsive website. Used Classes: Border: The "ba" class is used to create a simple border.Border Top: The "bt" class is used to create a border from the top.Border Right: The "br" class is used to create a border from the
4 min read
Bootstrap 5 Borders Sizes
Bootstrap 5 Border utility is used to set the style of the border element. The border styles can be of border-color, border-width, border-radius, etc. The Border Sizes are used to set the border-radius rounded corners larger or smaller. The range of border sizes varies from 0 to 3, where 0 represents smaller-size rounded corners and 3 represents la
2 min read
Bootstrap 5 Table borders
Bootstrap 5 Table borders classes are used to set or unset the border of the table. With bootstrap 5 table borders are easy to customize with border or no border. Bordered tables: This is used to set the border on each side of the table.Tables without borders: This is used to make the table borderless all over the table like there will be no row an
2 min read
Bootstrap 5 Borders Subtractive
Bootstrap5 Borders Subtractive is used to remove borders from the box by defining which side the border is to be removed or we can remove the border from all sides at once. Bootstrap 5 Borders Subtractive Classes: border-0: This class is used to remove borders from all around the elementborder-top-0: This class is used to remove the border from the
3 min read
Bootstrap 5 Borders Additive
Bootstrap5 Borders Additive classes are mainly used to display an outline around a box and it provides style, color, and radius of an element border on the side we want. For eg: if we want the border only on the top of the box we can have it by using the border-top additive class. Bootstrap 5 Borders Additive classes: border: This class is used to
2 min read
Bootstrap 5 Tables without borders
Bootstrap5 Tables without borders is used to create a table in borderless form. It is mostly used when we do not want to show the separation in the table content. Tables without Borders Class: table-borderless: This class is used to create borderless tables Syntax: &lt;table class="table table-borderless"&gt; ... &lt;/table&gt; Example 1: In this e
2 min read
HTML Table Borders
HTML Table Borders define the visible lines around cells and the overall table. They help in creating clear distinctions between various sections of the table, making it easier to understand and show information clearly. This attribute allows you to control the border's style, color, and width, for the visual presentation of your table. Syntaxtable
5 min read
Chakra UI Borders
Chakra UI is a popular React component library that simplifies the process of building user interfaces with a set of customizable and accessible components. Among those feature it also provide support for styling and layout including the management of borders. It provides various properties to customize borders. In this article, we will learn about
2 min read
Bootstrap 5 Borders
Bootstrap 5 Borders offer predefined border styles to elements. Classes like .border, .border-0, .border-top, .border-bottom, .border-left, and .border-right provide easy customization for borders, including color, width, and radius, enhancing UI design. iframe { width: 100%; height: 450px;} @media (max-width: 1145px) {.article--viewer .a-wrapper .
2 min read
Difference between bootstrap.css and bootstrap-theme.css
Bootstrap is an HTML, CSS and JS front-end-framework used for developing responsive and mobile-first projects on the web. It is the framework that comes up with predefined terms, thus giving you the ability to use these codes instead of having to create the codes from scratch. It is a collection of HTML, CSS and JS code that is designed to help bui
5 min read
Difference between CSS Grid and CSS Flexbox
Grid:CSS Grid Layout, is a two-dimensional grid-based layout system with rows and columns, making it easier to design web pages without having to use floats and positioning. Like tables, grid layout allow us to align elements into columns and rows. To get started you have to define a container element as a grid with display: grid, set the column an
4 min read
Which CSS framework is better Tailwind CSS or Bootstrap ?
In this article, we will see which CSS framework is better tailwind CSS or bootstrap. Both are widely used CSS frameworks for styling the front end. We will understand each one of them in detail. Both frameworks have their own pros and cons so it depends upon the type of application you are going to build and accordingly you can choose the best fra
5 min read
CSS Cheat Sheet - A Basic Guide to CSS
What is CSS? CSS i.e. Cascading Style Sheets is a stylesheet language used to describe the presentation of a document written in a markup language such as HTML, XML, etc. CSS enhances the look and feel of the webpage by describing how elements should be rendered on screen or in other media. What is a CSS Cheat Sheet? CSS Cheat Sheet provides you wi
13 min read
How to identify unused CSS definitions from multiple CSS files in a project ?
In this article, we will learn how to identify unused CSS definitions from multiple CSS files in a project. What are unused CSS definitions? Unused CSS definitions are the stylesheets that are completely useless for rendering and loading the page. If you remove these unused CSS definitions then nothing will happen to your webpage. Why you should re
2 min read
Create a CSS Fireflies background using HTML/CSS
In this article, we will see how to create the CSS Fireflies background using HTML &amp; CSS, along with understanding its implementation through the illustration. The CSS fireflies effect can be created by utilizing the CSS animation and we will implement 3 animations to achieve the fireflies effect. To create randomness, we used different animati
7 min read
Using Primer CSS - A Simple and Transparent CSS Library for Web Development
Cascading Style Sheets (CSS) is a style sheet language used to describe the presentation of a document written in HTML or XML. CSS helps you to control the layout and appearance of web pages. In this article, we will discuss the concept of Primer CSS, a CSS library that provides a simple and easy-to-use set of styles for web development. Obvious an
3 min read