zoukankan      html  css  js  c++  java
  • 居中问题(水平居中和垂直居中)

    1:图片的水平和垂直居中,如下

    <!DOCTYPE html>
    <html>
    <head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style>
    .container{
    300px;
    height:300px;
    border: 1px solid #000;
    /* 垂直居中 */
    display: table-cell;
    vertical-align:middle;
    /* 水平居中 */
    text-align: center;
    /* 针对IE的Hack */
    *display: block;
    *font-size: 262px;/*约为高度的0.873,200*0.873 约为175*/
    *font-family:Arial;/*防止非utf-8引起的hack失效问题,如gbk编码*/
    }
    img{border:1px solid red;}

    </style>
    </head>
    <body>
    <div class="container">
    <img align="center" src="img/iPhone1.jpg" alt=""/>
    </div>
    </body>
    </html>
    2,height line-height 是文本的垂直居中
    3,利用position:absolute 可以巧妙的实现居中

    <!DOCTYPE html>
    <html>
    <head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style>
    .container{
    80%;
    height: 500px;
    margin: 0 auto;
    border:1px solid #000;
    position: relative;
    }
    #wrap {
    200px;/*元素的宽度*/
    height:200px;/*元素的高度*/
    position: absolute;
    left: 50%;/*配合margin-left的负值实现水平居中*/
    margin-left: -100px;/*值的大小等于元素宽度的一半*/
    top:50%;/*配合margin-top的负值实现垂直居中*/
    margin-top: -100px;/*值的大小等于元素高度的一半*/
    border:1px solid #000;
    }

    </style>
    </head>
    <body>
    <div class="container">
    <span id="wrap">test</span>
    </div>

    </body>
    </html>
    给其父元素设置相对定位,就可以在父元素中实现水平和垂直方向的居中
  • 相关阅读:
    面向接口程序设计思想实践
    Block Chain Learning Notes
    ECMAScript 6.0
    Etcd Learning Notes
    Travis CI Build Continuous Integration
    Markdown Learning Notes
    SPRING MICROSERVICES IN ACTION
    Java Interview Questions Summary
    Node.js Learning Notes
    Apache Thrift Learning Notes
  • 原文地址:https://www.cnblogs.com/longailong/p/7325513.html
Copyright © 2011-2022 走看看