居中元素

时间:2014-10-27 19:15:53   收藏:0   阅读:163

如何垂直居中一个浮动元素?


// 方法一:已知元素的高宽
#div1{
   
    width:200px;
    height:200px;

    position: absolute;        //父元素需要相对定位
    top: 50%;
    left: 50%;
    margin-top:-100px ;   //二分之一的height,width
    margin-left: -100px;
    }

//方法二:未知元素的高宽

  #div1{
    width: 200px;
    height: 200px;
   

    margin:auto;
    position: absolute;        //父元素需要相对定位
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
    }
那么问题来了,如何垂直居中一个<img>?(用更简便的方法。)
#container     //<img>的容器设置如下
{
    display:table-cell;
    text-align:center;
    vertical-align:middle;
}

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!