Recently, when I was drawing an interface, I encountered a requirement: draw a vertical line in the interface, and this vertical line needs to automatically fill the entire parent div in height (that is, the height of this vertical line is the same as the higher of the two divs). Normally, we can draw a horizontal line directly using the tag <hr>, but when we try to draw a vertical line, we find that we can't find the tag. I searched for information online and generally recommended using js. I was a bit paranoid and wanted to use pure CSS to do it. Finally I found a solution. Let me share my approach below. Add another div between the two child divs, set the left (right) border to be visible, and use the principle of offsetting the positive and negative values of padding-bottom | margin-bottom. For example, setting Here is the code: body{ margin-top:100px; margin-left:200px; } .maindiv{ width:900px; padding:10px; overflow:hidden; /*Key*/ border:1px solid black; } .leftdiv{ float:left; width:400px; background-color:#CC6633; } .rightdiv{ float:right; width:400px; background-color:#CC66FF; } .centerdiv{ float:left; width:50px; border-right: 1px dashed black; padding-bottom:1600px; /*Key*/ margin-bottom:-1600px; /*Key*/ } <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>Vertical line drawing</title> <link href="../css/demo.css" rel="stylesheet" type="text/css" /> </head> <body> <div class="maindiv"> <div class="leftdiv"><br><br><br><br><br><br><br></div> <div class="centerdiv"></div> <div class="rightdiv"><br><br><br><br><br><br><br><br></div> </div> </body> </html> Effect picture: By the way, write some ideas and key codes of js Compare the heights of the two child divs to see which one is taller. You can also achieve this by setting the adjacent border of the taller div to be visible. The following is the js code function myfun(){ var div1 = document.getElementById("content"); var div2 = document.getElementById("side"); var h1=div1.offsetHeight; var h2=div2.offsetHeight; if(h1>h2){ div1.style.borderRight="1px dashed #B6AEA3"; }else{ div2.style.borderLeft="1px dashed #B6AEA3"; } } Summarize The above is what I introduced to you about how to draw a vertical line between two div tags in HTML. I hope it will be helpful to you. If you have any questions, please leave me a message and I will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website! |
<<: Use SQL statement to determine whether the record already exists before insert
Forms in HTML can be used to collect various type...
1. What is the use of slow query? It can record a...
Written in advance: In the following steps, you n...
HTML is a hybrid language used for publishing on ...
<br />In the previous article, I introduced ...
This article example shares the specific code of ...
motivation Due to learning needs, I purchased a v...
Empty link: That is, there is no link with a targ...
1. Install MySQL # Download mysql in docker docke...
Table of contents 1. Scene layout 2. Add a handle...
The purpose of setting up MySQL query cache is: C...
There are many commands used in the system, so ho...
Table of contents Environment Setup Overview 1.Wh...
In normal development, we usually use convex roun...
Let me briefly describe some common basic graphic...