How to add a horizontal line in HTML

problem

You’re learning HTML and wish to add a horizontal line in your page.

SOLUTION

Using the <hr> tag. The thematic break (horizontal rule) element.

For example we have two paragraphs of text and we wish to place a line between them.

hr.html
				
					<p>First paragraph</p>
<hr/>
<p>Second paragraph</p>
				
			
output
Change the color

By default the line color is black. We can change it by setting the color attribute. For example let’s make it green

hr.html
				
					<p>First paragraph</p>
<hr color="green"/>
<p>Second paragraph</p>
				
			
output
Change the size

We can change the size (height), how thick it is by setting the size attribute. For example:

hr.html
				
					<p>First paragraph</p>
<hr color="green" size="5px"/>
<p>Second paragraph</p>

				
			
output

For the size attribute we use pixels.

Change the width

We can change the width of the line by setting the width attribute. This attribute accepts both pixels and a percentage as value. The percentage corresponds to the current screen’s width. For example, let’s make the line span 50% of the screen.

hr.html
				
					<p>First paragraph</p>
<hr color="green" size="5px" width="50%"/>
<p>Second paragraph</p>
				
			
output

As we can see the line is 50% width and it appears in the center.

Aligning the line

By default the line is aligned to the left of the page. We can change it by setting the align attribute and a value of “left, right, center”. In this case we can set it to the right. For example:

hr.html
				
					<p>First paragraph</p>
<hr color="green" size="5px" width="50%" align="right"/>
<p>Second paragraph</p>
				
			
output
Change the alignment with JavaScript (advanced)

We can change the alignment dynamically with Javascript code. In combination with a timer we can keep changing the alignment from left to center, from center to right, from right to left and repeat. For example:

hr.html
				
					<script type="text/javascript">
	function realign(pos){
    	let line = document.getElementsByTagName("hr")[0];
    	if(pos=="center"){
        	line.setAttribute("align","right");    
        	setTimeout(function(){
            	realign('right');
        	},1500);
    	}
    	if(pos=="right"){
        	line.setAttribute("align","left");    
        	setTimeout(function(){
            	realign('left');
        	},1500);
    	}
    	if(pos=="left"){
        	line.setAttribute("align","center");    
        	setTimeout(function(){
            	realign('center');
        	},1500);
    	}
	}
</script>

				
			
output

Just for fun.

The complete code
hr.html
				
					<html>
	<body onload="realign('center')">
		<p>First paragraph</p>
		<hr color=”green” size='5px' width='50%' />
		<p>Second paragraph</p>
	</body>

	<script type="text/javascript">
		function realign(pos){
			let line = document.getElementsByTagName("hr")[0];
			if(pos=="center"){
				line.setAttribute("align","right");	
				setTimeout(function(){
					realign('right');
				},1500);
			}
			if(pos=="right"){
				line.setAttribute("align","left");	
				setTimeout(function(){
					realign('left');
				},1500);
			}
			if(pos=="left"){
				line.setAttribute("align","center");	
				setTimeout(function(){
					realign('center');
				},1500);
			}
		}
	</script>
</html>
				
			

conclusion

In this post we saw how to use the <hr> tag and change its color, size, width and alignment. We also saw a piece of javascript that interacts with the line and changes its alignment.

References

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/hr

Share it!

Facebook
Twitter
LinkedIn
Reddit
Picture of Ellion

Ellion

Professional IT consultant, writer, programmer enthusiast interested in all sorts of coding.
Eats all cookies 🍪

4.5 4 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

Strictly Necessary Cookies

Strictly Necessary Cookie should be enabled at all times so that we can save your preferences for cookie settings.

Google Analytics Cookies

This website uses Google Analytics to collect anonymous information such as the number of visitors to the site, and the most popular pages.

Keeping this cookie enabled helps us to improve our website.

HotJar Cookies

We use Hotjar in order to better understand our users’ needs and to optimize this service and experience. Hotjar is a technology service that helps us better understand our users’ experience (e.g. how much time they spend on which pages, which links they choose to click, what users do and don’t like, etc.) and this enables us to build and maintain our service with user feedback. Hotjar uses cookies and other technologies to collect data on our users’ behavior and their devices. This includes a device's IP address (processed during your session and stored in a de-identified form), device screen size, device type (unique device identifiers), browser information, geographic location (country only), and the preferred language used to display our website. Hotjar stores this information on our behalf in a pseudonymized user profile. Hotjar is contractually forbidden to sell any of the data collected on our behalf.

For further details, please see the ‘about Hotjar’ section of Hotjar’s support site.