flex 2----居中

发布时间:2023年12月26日

居中

方法1:父容器开启flex 布局,随后使用justify-content 和align-items 实现水平垂直居中

方法2:父容器开启flex 布局,随后子元素margin: auto

vs 使用不使用flex的方法

块元素

? ? ? ? 水平 :margin:0 auto ;

? ? ? ? 垂直:margin-top(父元素H-子元素H)/2)px

行内,行内块:

? ? ? ? 水平:text-align:center;

? ? ? ? 垂直:height = line-height,? vertical-align:middle;若想绝对居中的话,父元素font-size:0,再重置子元素的font-size

水平,垂直居中-CSDN博客


方法1:父容器开启flex 布局,随后使用justify-content 和align-items 实现水平垂直居中

 .outer1{
            width: 800px;
            height: 500px;
            background-color: antiquewhite;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        .inner1{
            width: 300px;
            height: 150px;
            background-color: aqua;
        }

  <div class="outer1">
        <div class="inner1">
            haha
        </div>
    </div>

方法2:父容器开启flex 布局,随后子元素margin: auto

.outer1{
            width: 800px;
            height: 500px;
            background-color: antiquewhite;
            display: flex;
        }
        .inner1{
            width: 300px;
            height: 150px;
            background-color: aqua;
            margin:auto;
        }

文章来源:https://blog.csdn.net/u014378108/article/details/135213400
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。