zoukankan      html  css  js  c++  java
  • css元素垂直居中

    一、css元素垂直居中初始状态

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>css元素垂直居中</title>
    <style type="text/css">
    .wrapper{
    background-color: #f24444;
    200px;
    height:200px;
    margin:0px auto;

    text-align:center;
    }
    /* .content{
    line-height:200px;
    }*/
    </style>
    </head>
    <body>
    <div class="wrapper">
    <div class="content">世上无难事,只怕有心人。</div>
    </div>
    </body>

    </html>

    二、1、line-height文本垂直居中

    .content{
    line-height:200px;
    }

    2、line-height图片垂直居中

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>css元素垂直居中</title>
    <style type="text/css">
    .wrapper{
    background-color: #f24444;
    200px;
    height:200px;
    margin:0px auto;
    text-align:center;
    }
    .content{
    line-height:200px;
    }
    .content img{
    vertical-align: middle;

    }
    </style>
    </head>
    <body>
    <div class="wrapper">
    <div class="content"><img src="s2.png"></div>
    </div>
    </body>

    </html>

    
    
    
    
    三、table方法垂直居中
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>css元素垂直居中</title>
    <style type="text/css">
    .wrapper{
    background-color: #f24444;
    200px;
    height:200px;
    margin:0px auto;
    text-align:center;
    display:table;
    }
    .content{
    display:table-cell;
    vertical-align:middle;
    }

    </style>
    </head>
    <body>
    <div class="wrapper">
    <div class="content">世上无难事,只怕有心人</div>
    </div>
    </body>

    </html>

    四、1、绝对定位与负位移垂直居中
    .wrapper{
    background-color: #f24444;
    200px;
    height:200px;
    margin:0px auto;
    text-align:center;
    position:relative;
    }
    .content{
    position:absolute;
    top:50%;
    left:50%;
    height:30%;
    50%;
    margin:-15% 0 0 -25%;
    }

    五、绝对定位与margin扩展

    .wrapper{
    background-color: #f24444;
    200px;
    height:200px;
    margin:0px auto;
    text-align:center;
    position:relative;
    }
    .content{
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    50%;
    height: 30%;
    margin: auto;
    }

    六、padding垂直居中
    .wrapper{
    background-color: #f24444;
    margin:0px auto;
    text-align:center;
    padding:5% 0;
    }
    .content{
    padding:10% 0;
    }

    
    
    七、浮动方法垂直居中

    .wrapper{
    background-color: #f24444;
    text-align:center;
    height:250px;
    }
    .floater{
    float:left;
    height:50%;
    100%;
    margin-bottom:-50px;
    }
    .content{
    clear:both;
    height:100px;
    }

    
    
  • 相关阅读:
    c# 抽象类(abstract)
    c# 虚方法(virtual)与 多态(Polymorphism)
    02.JavaScript基础上
    第 12 章 Ajax
    第 11 章 动画效果
    第 10 章 高级事件
    第 9 章 事件对象
    第 8 章 基础事件
    第 7 章 表单选择器
    第6章 DOM节点操作
  • 原文地址:https://www.cnblogs.com/wuliwuli/p/5170820.html
Copyright © 2011-2022 走看看