关于css:如何垂直居中一个浮动元素

11次阅读

共计 317 个字符,预计需要花费 1 分钟才能阅读完成。

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

1. 已知元素的高宽

  #div1{
    background-color:#6699FF; 
    width:200px; 
    height:200px; 
    position: absolute; // 父元素须要绝对定位 
    top: 50%; 
    left: 50%; 
    margin-top:-100px ; // 二分之一的 height,width 
    margin-left: -100px; 
    } 

2. 未知元素的高宽

    #div1{ 
      width: 200px; 
      height: 200px; 
      background-color: #6699FF; 
      margin:auto; 
      position: absolute; // 父元素须要绝对定位 
      left: 0; 
      top: 0; 
      right: 0; 
      bottom: 0; 
      } 
正文完
 0