How to create a vertical line with CSS?


We can easily create a vertical line on a web page using the border-left property. The border width, style and color needs to be also set for the line to be visible on the web page.

Create a div

First, create a container for the line to appear −

<div class="vLine"></div>

Create a line

To let the line to appear, use the border-left property −

.vLine {
   border-left: 6px solid rgb(128, 0, 128);
   height: 500px;
   margin-left: 5%;
}

Create a vertical line

To create a vertical line with CSS, the code is as follows −

<!DOCTYPE html>
<html>
<head>
   <style>
      .vLine {
         border-left: 6px solid rgb(128, 0, 128);
         height: 500px;
         margin-left: 5%;
      }
   </style>
</head>
<body>
   <h1>Vertical Line Example<h1>
   <div class="vLine"></div>
</body>
</html>

Create a centered vertical line

To center the vertical line, set the position property to absolute. With that the left property is set to 50% −

left: 50%;
position: absolute;

Let us see an example to create a vertical line and center it −

<!DOCTYPE html>
<html>
<head>
   <style>
      .vLine {
         border-left: 6px solid red;
         height: 500px;
         left: 50%;
         position: absolute;
      }
   </style>
</head>
<body>
   <h1>Vertical Line Example<h1>
   <div class="vLine"></div>
</body>
</html>

Updated on: 14-Dec-2023

587 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements