CSS实现不定宽高垂直水平居中问题

2020-03-18  本文已影响0人  喜剧之王爱创作

这个也是一个很古老的问题了,下面总结几种最常见的方式

<div class="content">
 <div class="item"></div>
</div>
flex方式
.content {
    height: 800px;
    display: flex;
    justify-content: center;
    align-items: center;
    border: 1px solid #090;
}
.item {
   width: 200px;
   height: 200px;
   background: #f00;
}
定位方式
.content {
    height: 800px;
    border: 1px solid #090;
    position: relative;
}
.item {
   width: 200px;
   height: 200px;
   background: #f00;
   position: absolute;
   left: 50%;
   top: 50%;
   transform: translate(-50%, -50%);
}

以上总结两种,其他方式请自行百度,个人认为这两种方式最好理解,更便利

上一篇 下一篇

猜你喜欢

热点阅读