饥人谷技术博客

css综合(重点:垂直居中)

2017-09-07  本文已影响0人  Zuo_鸣

编码规范


垂直居中的实现方法

(1)再没有加vertical-align:middle时,是以底部为基准对齐了


(2)在所有子元素中加了vertical-align:middle时,以span中线为基准,对齐了

(3)最后,把span的边框去掉,就能实现
效果预览

实现以下效果

效果预览

 <!DOCTYPE html>
 <html>
 <head>
 <meta charset="utf-8">
 <title>JS Bin</title>
 </head>
 <body>
    <div class="box">
        <div class="tr1"></div>
        <div class="con"></div>
    </div>
    <div class="box">
        <div class="tr2"></div>
        <div class="con"></div>
    </div>
    <div class="box">
        <div class="tr3"></div>
        <div class="con"></div>
    </div>  
</body>
</html>
 .box {
    margin-bottom:50px;
    position: relative;
    margin-top:20px;
}
.con {
    width:200px;
    height:100px;
    border:1px solid #ccc;
    margin-top:10px;
}
.tr1 {
    position:absolute;
    top:-10px;
    left:10px;
    border-left: 10px solid transparent;
    border-right: 10px solid transparent;
    border-bottom: 10px solid #ccc;
}
.tr2 {
    position: absolute;
    top:0px;
    left:182px;
    border-top: 10px solid red;
    border-right: 10px solid red;
    border-left: 10px solid transparent;
    border-bottom: 10px solid transparent;  
}
.tr3 {
    position:absolute;
    top:-9px;
    left:10px;
    border-left:10px solid transparent;
    border-right:10px solid transparent;
    border-bottom:10px solid #ccc; 
}
.tr3:after {
    content:'';
    position:absolute;
    top:1px;
    left:-10px;
    border-left:10px solid transparent;
    border-right:10px solid transparent;
    border-bottom:10px solid #fff;
}
上一篇下一篇

猜你喜欢

热点阅读