3 Ways To Create Vertical Lines In HTML CSS

Welcome to a tutorial on how to create vertical lines in HTML and CSS. Want to add a vertical line beside a paragraph or section of the page?

One of the easiest ways to create a vertical line in HTML CSS is to add a CSS border.

  • <div style="boder-left: 5px solid red">Left Vertical Line</div>
  • <div style="boder-right: 5px solid blue">Right Vertical Line</div>

That covers the quick basics, but read on for more examples!

ⓘ I have included a zip file with all the source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

 

 

QUICK SLIDES

[web_stories_embed url=”https://code-boxx.com/web-stories/vertical-lines-html-css/” title=”Vertical Lines With HTML CSS” poster=”https://code-boxx.com/wp-content/uploads/2022/03/STORY-HTML-20230505.webp” width=”360″ height=”600″ align=”center”]

 

TABLE OF CONTENTS

Download & Notes Vertical Lines Useful Bits & Links
The End

 

DOWNLOAD & NOTES

Firstly, here is the download link to the example code as promised.

 

QUICK NOTES

If you spot a bug, feel free to comment below. I try to answer short questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

 

EXAMPLE CODE DOWNLOAD

Click here to download all the example source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

 

HTML CSS VERTICAL LINES

All right, let us now get into the examples of creating vertical lines in HTML and CSS.

 

1) VERTICAL LINE WITH CSS BORDER

1A) THE RESULT

Left border
Right border

 

1B) HTML & CSS

1-border.html
<!-- (A) VERTICAL LINE ON LEFT -->
<div class="border-l">Left border</div>

<!-- (B) VERTICAL LINE ON RIGHT -->
<div class="border-r">Right border</div>
1-border.css
/* (A) LEFT BORDER */
.border-l {
  /* (A1) VERTICAL LINE ON LEFT */
  /* BORDER : THICKNESS STYLE COLOR */
  border-left: 5px solid red;
 
  /* (A2) COSMETICS */
  padding: 20px;
  background: #ffeded;
}
 
/* (B) RIGHT BORDER */
.border-r {
  /* (B1) VERTICAL LINE ON RIGHT */
  /* SOLID | DOTTED | INSET | DASHED | DOUBLE */
  border-right: 5px dashed blue;
 
  /* (B2) COSMETICS */
  padding: 20px;
  background: #f0edff;
}

As in the introduction, the easiest way to create a vertical line is to add border-left or border-right. But take note of how we can control the lines – border: THICKNESS STYLE COLOR. If you are new to HTML/CSS, just take a moment to walk through all the measuring units and color systems. That will lessen your pains, links below.

 

 

2) VERTICAL LINE IN THE CENTER

2A) THE RESULT

Left
Right

 

2B) HTML & CSS

2-middle.html
<div class="wrapper">
  <div class="left-side">Left</div>
  <div class="right-side">Right</div>
</div>
2-middle.css
/* (A) FLEX WRAPPER */
.wrapper {
  display: flex;
  background: #f2f2f2;
}
 
/* (B) LEFT-RIGHT DIMENSIONS */
.left-side { width: 50%; }
.right-side { flex-grow: 1; } /* auto use all remaining space */
.left-side, .right-side { padding: 10px; }
 
/* (C) VERTICAL LINE */
.left-side { border-right: 5px dotted red; }

How do we place a line in the middle (or somewhere in the middle) then?

  • Create a <div class="wrapper">, then sandwich <div class="left-side"> <div class="right-side"> inside.
  • Set .wrapper { display: flex } to “split” it into a left-right layout.
  • Set the dimensions of left-side and right-side.
  • Use the same old border to create the vertical line.

 

 

3) HORIZONTAL RULE

3A) THE RESULT


 

3B) THE HTML

3-hr.html
<hr width="1" size="100">

This is another common alternative floating all over the Internet – Use a horizontal rule <hr> to create a vertical line… Funky. While it is simple, I cannot recommend this method – It is not very customizable.

 

USEFUL BITS & LINKS

That’s all for the tutorial, and here is a small section on some extras and links that may be useful to you.

 

LINKS & REFERENCES

 

INFOGRAPHIC CHEAT SHEET

Vertical Lines In HTML CSS (Click To Enlarge)

 

THE END

Thank you for reading, and we have come to the end. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

Leave a Comment

Your email address will not be published. Required fields are marked *