如何垂直居中一个浮动元素(未知元素的宽高)

Posted qihunliupo

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何垂直居中一个浮动元素(未知元素的宽高)相关的知识,希望对你有一定的参考价值。

<style>
  #div{
    width: 800px;
    height: 500px;
    border: 1px solid gold;
    position: relative;
  }
  #div1{
    width: 200px;
    height: 200px;
    background-color: cyan;
    position: absolute; /*父元素需要相对定位*/
    margin: auto;
    top: 0;
    left: 0;
    bottom: 0;
    right: 0;
   }
</style>

<body>
  <div id="div">
    <div id="div1"></div>
  </div>
</body>

以上是关于如何垂直居中一个浮动元素(未知元素的宽高)的主要内容,如果未能解决你的问题,请参考以下文章