CSS居中的几种方式

1.水平居中的 margin: 0 auto;

这个用于子元素上的,前提是不受float影响。

2.水平居中的 text-align: center;

img的display:inline-block;类似一样在不受float影响下进行
是在父元素上添加效果让它进行水平居中

3.水平垂直居中(一)定位和需要定位的元素的margin减去宽高的一半

这种方法的局限性在于需要知道需要垂直居中的宽高才能实现,经常使用这种方法

    <style>
        *{
            padding: 0;
            margin: 0;
        }
        .box{
            width: 300px;
            height: 300px;
            background:#e9dfc7; 
            border:1px solid red;
            position: relative;
        }
        img{
            width: 100px;
            height: 150px;
            position: absolute;
            top: 50%;
            left: 50%;
            margin-top: -75px;
            margin-left: -50px;
        }
    </style>
<!--html -->
<body>
    <div class="box" >
        ![](../img1.png)
    </div>
</body>

4.水平垂直居中(二)定位和margin:auto;

这个方法也很实用,不用受到宽高的限制,也很好用

<style>
        *{
            padding: 0;
            margin: 0;
        }
        .box{
            width: 300px;
            height: 300px;
            background:#e9dfc7; 
            border:1px solid red;
            position: relative;

        }
        img{
            width: 100px;
            height: 100px;
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            margin: auto;
        }
    </style>
<!--html -->
<body>
    <div class="box" >
        ![](../img1.png)
    </div>
</body>

5.水平垂直居中(三)绝对定位和transfrom

这个是不需要知道居中元素的宽高就可以使用的.面试中大部分人会问如果不知道宽高该如何居中,答这个

<style>
    *{
        padding: 0;
        margin: 0;
    }
    .box{
        width: 300px;
        height: 300px;
        background:#e9dfc7; 
        border:1px solid red;
        position: relative;

    }
    img{
        width: 100px;
        height: 100px;
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%,-50%);
    }
</style>
<!--html -->
<body>
    <div class="box" >
        ![](../img1.png)
    </div>
</body>

6.水平垂直居中(四)diplay:table-cell

其实这个就是把其变成表格样式,再利用表格的样式来进行居中,很方便

<style>
    .box{
            width: 300px;
            height: 300px;
            background:#e9dfc7; 
            border:1px solid red;
            display: table-cell;
            vertical-align: middle;
            text-align: center;
        }
        img{
            width: 100px;
            height: 150px;
            /*margin: 0 auto;*/  这个也行
        }
</style>
<!--html -->
<body>
    <div class="box" >
        ![](../img1.png)
    </div>
</body>

7.水平垂直居中(五)flexBox居中

这个用了C3新特性flex,非常方便快捷,在移动端使用完美,pc端有兼容性问题,以后会成为主流的

<style>
    .box{
            width: 300px;
            height: 300px;
            background:#e9dfc7; 
            border:1px solid red;
            display: flex;
            justify-content: center;
            align-items:center;
        }
        img{
            width: 150px;
            height: 100px;
        }
</style>
<!--html -->
<body>
    <div class="box" >
        ![](../img1.png)
    </div>
</body>

8.水平垂直居中(六)利用vertical-align:middle;

这个方法关键要有一个和容器一样高的元素作为居中的一个参照就像b元素一样

<style>
    .wrap{
            width:300px;
            height:300px; 
            background:rgba(0,0,0,0.5);
            text-align:center;
            font-size:0;
            }
    .vamb{
        display:inline-block; 
        width:0px;
        height:100%;
        vertical-align:middle;
        }
    .test{
        display:inline-block;
        vertical-align:middle;
        font-size:16px;
        text-align:left;
        background:red;
        }
</style>
<body>
    <div class="wrap">
        <b class="vamb"></b>
        <div class="test">
        宽高不定<br>
        垂直水平居中
        </div>
    </div>
</body>
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 14,355评论 1赞 92
  • 水平居中方式 1.margig:0 auto;(不能受到float影响) 2.text-align: center...
    MGd阅读 635评论 0赞 1
  • 本文主要总结几种常见的CSS居中方式,下面我准备分为三个方向来写,分别是水平居中,垂直居中,水平垂直居中。水平居中...
    IrisLong阅读 770评论 0赞 2
  • 1.水平居中的 margin:0 auto; 2.水平居中 text-align:center; 3.水平垂直居中...
    被遗忘的传说阅读 1,280评论 0赞 0
  • 上课了。 所有人抽出了物理必修二除了我,我抽出来的东西会更大一些,更软一些,放在桌子上大小正好,但略显空旷。感觉若...
    贝龙阅读 975评论 3赞 5

友情链接更多精彩内容