欢迎来到DIVCSS5查找CSS资料与学习DIV CSS布局技术!
往常我们画一条横线直接用标签<hr>即可,当画一条竖线的时候发现找不到标签。在网上查找了一下资料,大致推荐用js来做。小弟比较偏执想用纯css来做,最终找到了解决方法,下面我就来分享一下我的做法。
 
在两个子p中加多一个p,并设置左(右)边框为可见,并且利用利用padding-bottom|margin-bottom正负值相抵消的原理。例如设置 padding-bottom:1600px; margin-bottom:-1600px;我们可以理解为:运用的是padding可以撑开外层标签而margin不用来撑开外层标签。即当padding-bottom时撑开外层标签的高度,外层标签用overflow:hidden;隐藏掉多余的高,这样可以让高度与最高的那一栏对齐;而margin关乎模块布局,margin可以抵消掉padding撑开的盒子使布局能够从内容部分开始。
 
以下是代码:
 
 
 
 
 
body{  
 
    margin-top:100px;  
 
    margin-left:200px;  
 
}  
 
.mainp{  
 
    width:900px;  
 
    padding:10px;  
 
    overflow:hidden; /*关键*/ 
 
    border:1px solid black;  
 
}  
 
.leftp{  
 
    float:left;  
 
    width:400px;  
 
    background-color:#CC6633;  
 
}  
 
.rightp{  
 
    float:right;  
 
    width:400px;  
 
    background-color:#CC66FF;  
 
}  
 
.centerp{  
 
    float:left;  
 
    width:50px;  
 
    border-right: 1px dashed black;  
 
    padding-bottom:1600px;  /*关键*/ 
 
    margin-bottom:-1600px;  /*关键*/ 
 
}  
 
 
 
<!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>竖线画法</title>  
 
<link href="../css/demo.css" rel="stylesheet" type="text/css" />  
 
</head>  
 
<body>  
 
    <p class="mainp">  
 
        <p class="leftp"><br><br><br><br><br><br></p>  
 
        <p class="centerp"></p>  
 
        <p class="rightp"><br><br><br><br><br><br><br></p>  
 
    </p>  
 
</body>  
 
</html>
 
效果图:
 
顺便写一下js的思路和关键代码
 
比较两个子p的高度哪一高。选择把高的那个p的相邻边框设为可见也可达到目的。
 
以下是js的代码
 
function myfun(){  
 
  var p1=document.getElementById("content");  
 
  var p2=document.getElementById("side");  
 
  var h1=p1.offsetHeight;  
 
  var h2=p2.offsetHeight;  
 
    if(h1>h2){  
 
        p1.style.borderRight="1px dashed #B6AEA3";  
 
    }else{  
 
        p2.style.borderLeft="1px dashed #B6AEA3";  
 
  }  
 
}









本文转载自励志网
 

如需转载,请注明文章出处和来源网址:http://www.divcss5.com/html/h57990.shtml