块元素水平垂直居中

2019-02-24  本文已影响0人  2764906e4d3d

块元素水平垂直居中

  1. 使用flex布局将子元素水平垂直居中
<style>
            #fa{
                width: 300px;
                height: 300px;
                background-color: #00FFFF;
                display: flex;
                justify-content: center;
                align-items: center;
            }
            .center{
                width: 100px;
                height: 100px;
                background-color: rosybrown;
            }
</style>
  1. 使用position定位
#div{
  position:absolute;
  left:50%;
  top:50%;
  transform: translate(-50%,-50%);
}

div{
  width: 200px;
  height: 150px;
  border: 1px solid aqua;
  position: absolute;
  margin: auto;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
            }
上一篇 下一篇

猜你喜欢

热点阅读