The Wayback Machine - https://web.archive.org/web/20241127033315/https://www.geeksforgeeks.org/how-to-add-border-on-only-one-side-in-shorter-way-in-css/
Open In App

How to Add Border on Only One Side in Shorter Way in CSS?

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

Borders are useful for defining and separating elements on your webpage, giving it a more organized look. You can easily add a border to just one side of an element using CSS. By using specific border properties, you can customize which side gets the border, helping to create a cleaner design.

Approach to Add Border on Only One Side

To add a border to just one side of an element in CSS, first, choose the HTML element you want to style, like a <div> or <p>. Then, use the specific property for the side you want to change; for example, if you want a border on the left, use border-left. You can set the border’s width, style (solid or dashed), and color. If you don’t want any borders on the other side, set them to none. For instance, you might write border-top: none; to remove the top border.

Syntax:

// With traditional CSS: CSS ruleset
border-{any side}: values;

Example: The code demonstrates how we can add a border to only one side using CSS and the border-{any side} property.

HTML
<!DOCTYPE html>
<html lang="en">

<head>
    <title>
        Is there a shorter way to write a border
        on only one side
    </title>
    <style>
        .container {
            display: flex;
        }

        .container-1 {
            border-left: 0.25rem solid green;
            background-color: gainsboro;
            width: 250px;
            height: 100px;
            margin: 5% 6% 0 10%;
        }

        .container-2 {
            border-bottom: 0.25rem solid green;
            background: gainsboro;
            margin-top: 5%;
            width: 250px;
            height: 100px;
        }

        .container-3 {
            border-right: 0.25rem solid green;

            background-color: gainsboro;
            width: 250px;
            height: 100px;
            margin: 5% 6% 0 10%;
        }

        .container-4 {
            border-top: 0.25rem solid green;
            background: gainsboro;
            width: 250px;
            height: 100px;
            margin-top: 5%;
        }

        p {
            margin-left: 18%;
        }
    </style>
</head>

<body>
    <h1 style="color: green; margin: 1rem">
    </h1>
    <h3 style="margin: 1rem; margin-top: -1rem">
        Is there a shorter way to write a border
        on only one side?
    </h3>
    <div class="container">
        <div class="container-1"></div>
        <div class="container-2"></div>
    </div>
    <div class="container">
        <p>with <code>only left border</code></p>
        <p>with <code>only bottom border</code></p>
    </div>
    <div class="container">
        <div class="container-3"></div>
        <div class="container-4"></div>
    </div>
    <div class="container">
        <p>with <code>only right border</code></p>
        <p>with <code>only top border</code></p>
    </div>
</body>

</html>

Output:

Output

Output



Previous Article
Next Article

Similar Reads

Server Side Rendering vs Client Side Rendering vs Server Side Generation
In the world of web development, there are several approaches to rendering web pages: server-side rendering, client-side rendering, and server-side generation. Each approach has its own advantages and disadvantages, and choosing the right one for your project depends on your specific needs and goals. In this blog, we’ll explore the differences betw
4 min read
Difference between One-way Binding and Two-way Binding
In this article, we will learn the concept of data binding in Angular. We will also explore its types & examine the differences between one-way binding and two-way binding in angular. Data binding is a way to synchronise the data between the model and view components automatically. AngularJS implements data-binding that treats the model as the
3 min read
What is the difference between one-way data flow and two-way data binding in vue.js?
Vue.js is an open-source front-end JavaScript framework for building user interfaces and single-page applications. It follows Model-View-ViewModel (MVVM) architecture pattern. Vue.js has many in-built directives for DOM manipulation such as v-bind, v-on, v-model, etc. In this article, we will learn about the one-way data binding & two-way data
7 min read
How to specify shorter version of the content in a header cell in HTML5 ?
In this article, we will learn how to specify a shorter version of the content in a header cell. This can be helpful for screen reader users as they do not need to read out larger headings or make it easy to understand known abbreviations. The <th> tag is used to specify the heading for an <table> element and the content of the heading
2 min read
How to specify a shorter label for an option in HTML5 ?
The HTML option label attribute specifies the text value that defines the option's shorted label. In the drop-down list, the shorter version will be shown. An object stored in a <select>, <optgroup>, or <datalist> element is defined by the HTML <option> element. As a result, <option> can be used to represent popup menu
2 min read
Which one should we use 'border: none' or 'border: 0 '?
In this article, we will learn the validity of CSS border properties. We can specify the no border property using CSS border: none, border-width : 0, border : 0 properties. Syntax: border: none | 0 Differences: border: noneborder : 0For this property, the browser does not render.For this property, the browser renders border-color and border-width p
1 min read
How to add a box-shadow on one side of an element using CSS?
The box-shadow property is used to set the box shadow on one side of element. Syntax: box-shadow: h-offset v-offset blur spread color; box-shadows values: h-offset: It is required and used to set the position of the shadow horizontally. The positive value is used to set the shadow on right side of the box and a negative value is used to set the sha
2 min read
How to place two div side-by-side of the same height using CSS?
The two or more different div of same height can be put side-by-side using CSS. Use CSS property to set the height and width of div and use display property to place div in side-by-side format. The used display property are listed below: display:table; This property is used for elements (div) which behaves like table.display:table-cell; This proper
2 min read
How To Place Tables Side by Side using HTML and CSS
In this article, we will learn how to place tables side by side using HTML and CSS. To place tables side by side, first, we create two tables using <table> tag and then apply some CSS styles to place both tables side by side. Place Tables Side by SideExample: Here, we place two Tables Side by Side using HTML and CSS. [GFGTABS] HTML <!DOCTY
2 min read
How to float three div side by side using CSS?
Floating three div elements side by side using CSS means aligning them horizontally in a row. This is done by applying float: left; to each div, which moves them to the left of the parent container, allowing them to sit next to each other. These are the following ways to solve this problem: Table of Content Using float propertyUsing display propert
3 min read
Create Side-by-Side Image Comparison In Tailwind CSS
CSSA side-by-side image comparison feature allows users to compare two images: showcasing "before" and "after" states. This technique is widely used in various fields such as web design, photography, e-commerce, etc. We can also use this technique to highlight differences or improvements between two images. PrerequisitesHTMLCSSTailwind CSSApproach
2 min read
How to Align Images Side By Side using CSS ?
Images side by side means placing multiple images in a single row next to each other. This arrangement shows images in a horizontal line, making it great for photo galleries, and comparing pictures. To align images side by side using CSS, we can use flexbox or grid for layout. Table of Content Using the float PropertyUsing FlexboxUsing CSS GridAlig
2 min read
Difference between Server-Side AJAX framework and Client-side AJAX framework ?
The article focuses on discussing the differences between the Server-side AJAX framework and the Client-side AJAX framework. The following topics will be discussed here: What is a Server-side AJAX framework?What is a Client-side AJAX framework?Server-side AJAX framework vs Client-side AJAX framework Let's start discussing each of these topics in de
3 min read
How to place SVG icons on a round circle side by side to another div using Bootstrap?
Bootstrap Icons are SVGs, so they scale quickly and easily and can be styled with CSS. Approach: We can place the SVG icons and give them the shape of a circle. This will make the SVG icons appear to be in a circular shape. This can be achieved using a Bootstrap class called "rounded-circle" Syntax: <img class = "rounded-circle" src = "..." alt
2 min read
Server side and Client side Programming
Server-side Programming : It is the program that runs on server dealing with the generation of content of web page. 1) Querying the database 2) Operations over databases 3) Access/Write a file on server. 4) Interact with other servers. 5) Structure web applications. 6) Process user input. For example if user input is a text in search box, run a sea
2 min read
How does SSR(Server-Side Rendering) differ from CSR(client-side rendering) ?
Server-Side Rendering (SSR) and Client-Side Rendering (CSR) are two different approaches used in web development to render web pages to users. Each approach has its own set of advantages and disadvantages. In this article, we will learn about the difference between SSR and CSR with examples and features. Table of Content Server-Side Rendering (SSR)
4 min read
How to apply bottom border with only Input text field using CSS ?
Adding a bottom border to the input field visually distinguishes it from surrounding elements on the page. Making input fields stand out on a website in a form for example is crucial. Styling these inputs with clear borders, appealing colors, and effects, can easily capture the user’s attention. Table of Content Using the outline and border-bottom
3 min read
How to add date and time picker using only one tag in HTML ?
The date picker in HTML is a convenient tool that enhances user experience by allowing users to select a date from a dropdown menu rather than typing it manually. By using the <input> element with the type attribute set to "date", you can generate an interactive date picker in your HTML document. This not only simplifies date entry but also e
2 min read
How to Add Border to <tr> Element in CSS ?
In this article, we will see the different techniques to include the border to the <tr> Table element using CSS. There are 2 ways to apply the bottom border in the table in HTML. By implementing HTML onlyBy using HTML & CSSWe will explore both approaches for including the border to the <tr> element, & will understand them throug
4 min read
How to add border-bottom to table row <tr> in CSS ?
A border is an important component of any table in HTML. It is used to define the boundaries of the table and its elements. The border-bottom property is used to add a border to the bottom of an HTML element. In this article, we will discuss how to add a border-bottom to table row <tr>. When applied to a table row, it creates a border that se
3 min read
How to add border in the clip-path: polygon() using CSS ?
Adding a border around shapes can substantially improve the visual attractiveness of your website when it comes to web design. Using the clip-path: polygon() Property is one great and inventive approach to accomplish this. This not only allows you to make unusual shapes but also allows you to add a border that fits your design. In this article, we
3 min read
How to Add an Iframe Border using CSS ?
This article will show you how to add a border to the iframe element using CSS. An inline frame is used to embed another document within the current HTML document. To add the iframe border, we will use the CSS border property. Syntax: <!-- CSS border to the iframe --><style> iframe { border: 5px solid green; }</style><!-- HTML
2 min read
How to add border to an element on mouse hover using CSS ?
Adding a border to an element on mouse hover using CSS involves changing the element's border style when the mouse pointer is over it. This effect enhances user interaction by visually highlighting the element, achieved using the :hover pseudo-class to modify border properties. Approach HTML Structure: Select the HTML element you want to apply the
1 min read
How to Add a Rounded Border with CSS ?
A rounded border in CSS gives elements smooth, curved corners instead of sharp ones. It enhances design aesthetics and is achieved using the border-radius property. This allows developers to control the curvature, creating visually appealing and modern web layouts easily. Using the border-radius property, you can easily add rounded borders to vario
2 min read
How to Add Border to an Image Using HTML and CSS?
Adding a border to an image means putting a line around the image to make it look better and more noticeable. To add a border to an image using HTML and CSS, we can use the <img> tag and apply CSS styles like border, border-width, and border color to customize the border's appearance. Syntax .img-class { border: 1px solid black;}Adding a Bord
1 min read
How to Add Border Around Text using CSS?
The CSS border property is used to add a border around text by wrapping the text in an HTML element like <span> or <p>. Syntax border: "borderWidth borderStyle colorName;"Example 1: Adding a border around the text content using CSS. [GFGTABS] HTML <p> The following text has a border <span style="border: 2px solid green; pa
1 min read
Vue.js Prop One-Way Data Flow
In VueJS, all the props are bonded between the child and properties is one-way-down. When we update the prop in the parent component, it is updated to all the child components, but the reverse does not happen and VueJS warns the use. This is used as protection from altering the state of a parent-by-child component. Different Cases where the Child C
4 min read
Best Way to Add Bootstrap 4 to Project
New developer generally face problems while implementing or adding bootstrap to their projects. The problem mainly occurs to implement the bootstrap all over the project (that is on all the required files) without adding the stylesheet and script on every page of the project. In this article, we have provided a simplified way to the same. Example w
5 min read
What is the best way to add an event in JavaScript ?
Javascript has events to provide a dynamic interface to a webpage. These events are hooked to elements in the Document Object Model(DOM). There are three ways to assign an event handler:  HTML event handler attributeHTML DOM propertyHTML DOM addEventListener() methodThe best way to add an event in JavaScript script is addEventListener(). Let's dive
2 min read
How to Add a Border or Shadow to Elements using Bootstrap 5 Classes ?
To add a border in Bootstrap 5, we can use classes like 'border' for a full border or border-top, border-end, border-bottom, border-start for specific sides. For shadows, apply classes like 'shadow' for a default shadow, shadow-sm for a small shadow, or shadow-lg for a large shadow. Table of Content Shadow classesBorder ClassesShadow classesBootstr
2 min read
three90RightbarBannerImg