CSS让内容居中的方法总结

内容水平居中 

分行内元素与块级元素两种情况;其中块级元素又分定宽与不定宽两种情况;
行内元素

首先看它的父元素是不是块级元素,如果是,则直接给父元素设置 text-align: center; 

<style>
    #father {
        width: 300px;
        height: 100px;
        background-color: skyblue;
        text-align: center;
       }
</style>

<div id="father">
    <span id="son">我是行内元素</span>
</div>

如果不是,则先将其父元素设置为块级元素,再给父元素设置 text-align: center;

<style>
    #father {
        display: block;
        width: 500px;
        height: 300px;
        background-color: skyblue;
        text-align: center;
       }
</style>

<span id="father">
    <span id="son">我是行内元素</span>
</span>
块级元素

定宽度:需要谁居中,给其设置 margin: 0 auto; (作用:使盒子自己居中)

<style>
    #father {
        width: 500px;
        height: 300px;
        background-color: skyblue;
    }

    #son {
        width: 100px;
        height: 100px;
        background-color: green;
        margin: 0 auto;
    }
</style>

<div id="father">
    <div id="son">我是块级元素</div>
</div>

 不定宽度:默认子元素的宽度和父元素一样,这时需要设置子元素为display: inline-block; 或 display: inline;即将其转换成行内块级/行内元素,给父元素设置 text-align: center; 

<style>
    #father {
        width: 500px;
        height: 300px;
        background-color: skyblue;
        text-align: center;
    }

    #son {
        background-color: green;
        display: inline;
    }
</style>

<div id="father">
    <div id="son">我是块级元素</div>
</div>
使用定位属性

首先设置父元素为相对定位,再设置子元素为绝对定位,设置子元素的left:50%,即让子元素的左上角水平居中;

定宽度:设置绝对子元素的 margin-left: -元素宽度的一半px; 或者设置transform: translateX(-50%);

<style>
    #father {
        width: 500px;
        height: 300px;
        background-color: skyblue;
        position: relative;
}

    #son {
        width: 100px;
        height: 100px;
        background-color: green;
        position: absolute;
        left: 50%;
        margin-left: -50px;
}
</style>

<div id="father">
    <div id="son">我是块级元素</div>
</div>

不定宽度:利用css3新增属性transform: translateX(-50%);

<style>
    #father {
        width: 500px;
        height: 300px;
        background-color: skyblue;
        position: relative;
}

    #son {
        height: 100px;
        background-color: green;
        position: absolute;
        left: 50%;
        transform: translateX(-50%);
}
</style>

<div id="father">
    <div id="son">我是块级元素</div>
</div>
使用flexbox布局实现(宽度定不定都可以

使用flexbox布局,只需要给待处理的块状元素的父元素添加属性 display: flex; justify-content: center;

<style>
    #father {
        width: 500px;
        height: 300px;
        background-color: skyblue;
        display: flex;
        justify-content: center;
    }

    #son {
        width: 100px;
        height: 100px;
        background-color: green;
    }
</style>

<div id="father">
    <div id="son">我是块级元素</div>
</div>

垂直居中

a.行内元素(单行或多行)b.块级元素(两种方案)
单行的行内元素

只需要设置单行行内元素的”行高等于盒子的高”即可;

<style>
    #father {
        width: 500px;
        height: 300px;
        background-color: skyblue;
    }

    #son {
        background-color: green;
        height: 300px;
    }
</style>

<div id="father">
    <span id="son">我是单行的行内元素</span>
</div>
多行的行内元素

使用给父元素设置display:table-cell;和vertical-align: middle;属即可;

<style>
    #father {
        width: 500px;
        height: 300px;
        background-color: skyblue;
        display: table-cell;
        vertical-align:middle;
    }

    #son {
        background-color: green;
    }
</style>

<div id="father">
    <span id="son">我是多行的行内元素我是多行的行内元素我是多行的行内元素我是多行的行内元素我是多行的行内元素我是多行的行内元素我是多行的行内元素我是多行的行内元素</span>
</div>
块级元素

使用定位: 首先设置父元素为相对定位,再设置子元素为绝对定位,设置子元素的top: 50%,即让子元素的左上角垂直居中;

定高度:设置绝对子元素的 margin-top: -元素高度的一半px; 或者设置transform: translateY(-50%);

<style>
    #father {
        width: 500px;
        height: 300px;
        background-color: skyblue;
        position: relative;
}

    #son {
        height: 100px;
        background-color: green;
        position: absolute;
        top: 50%;
        margin-top: -50px;
}
</style>

<div id="father">
    <div id="son">我是块级元素</div>
</div>

不定高度:利用css3新增属性transform: translateY(-50%);

<style>
    #father {
        width: 500px;
        height: 300px;
        background-color: skyblue;
        position: relative;
}

    #son {
        width: 100px;
        background-color: green;
        position: absolute;
        left: 50%;
        transform: translateY(-50%);
}
</style>

<div id="father">
    <div id="son">我是块级元素</div>
</div>
使用flexbox布局实现(高度定不定都可以)

使用flexbox布局,只需要给待处理的块状元素的父元素添加属性 display: flex; align-items: center;

<style>
    #father {
        width: 500px;
        height: 300px;
        background-color: skyblue;
        display: flex;
        align-items: center;
    }

    #son {
        width: 100px;
        height: 100px;
        background-color: green;
    }
</style>

<div id="father">
    <div id="son">我是块级元素</div>
</div>

水平垂直居中

根据宽高是否确定而定方案
元素宽高已定

设置父元素为相对定位,给子元素设置绝对定位,top: 0; right: 0; bottom: 0; left: 0; margin: auto;

<style>
    #father {
        width: 500px;
        height: 300px;
        background-color: skyblue;
        position: relative;
}

    #son {
        width: 100px;
        height: 100px;
        background-color: green;
        position: absolute;
        top: 0;
        right: 0;
        bottom: 0;
        left: 0;
        margin: auto;
}
</style>

<div id="father">
    <div id="son">我是块级元素</div>
</div>

设置父元素为相对定位,给子元素设置绝对定位,left: 50%; top: 50%; margin-left: –-元素宽度的一半px; margin-top: –-元素高度的一半px;

<style>
    #father {
        width: 500px;
        height: 300px;
        background-color: skyblue;
        position: relative;
}

    #son {
        width: 100px;
        height: 100px;
        background-color: green;
        position: absolute;
        left: 50%;
        top: 50%;
        margin-left: -50px;
        margin-top: -50px;
}
</style>

<div id="father">
    <div id="son">我是块级元素</div>
</div>
元素宽高未定

使用定位属性: 设置父元素为相对定位,给子元素设置绝对定位,left: 50%; top: 50%; transform: translateX(-50%) translateY(-50%);

<style>
    #father {
        width: 500px;
        height: 300px;
        background-color: skyblue;
        position: relative;
}

    #son {
        background-color: green;
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translateX(-50%) translateY(-50%);
}
</style>

<div id="father">
    <div id="son">我是块级元素</div>
</div>

使用flex布局实现: 设置父元素为flex定位,justify-content: center; align-items: center;

<style>
    #father {
        width: 500px;
        height: 300px;
        background-color: skyblue;
        display: flex;
        justify-content: center;
        align-items: center;
}

    #son {
        background-color: green;
}
</style>

<div id="father">
    <div id="son">我是块级元素</div>
</div>
(1)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
ZEROZERO
上一篇 2019年6月18日
下一篇 2019年6月29日

相关推荐

  • CSS布局之圣杯与双飞翼布局

    所谓圣杯布局和双飞翼布局其实解决的问题是相同的,都是解决左右两栏固定宽度,中间部分自适应,其中某部分内容比其他内容高的时候,保证三者元素等高。他俩的区别就是:圣杯用padding。…

    2019年6月18日
    1.8K
  • px、em和rem实战经验

    在自适应布局或者移动端网页开发时,我们经常会用到em和rem两个长度单位。接下来我们讨论一下这两个单位和px之间的区别,以及他们的使用场景等。 px px,像素(计算机屏幕上的一个…

    2018年9月11日
    2.0K
  • CSS多列等高布局

    在项目开发中,经常遇到需要多列等高布局的需求。解决这种的需求的方法有很多,各有利弊,现总结如下。 方法一:使用flex布局 优点:实现方便,还可以方便实现各种比例 ; 缺点: IE…

    2019年6月11日
    2.1K
  • 网页布局之三栏网页宽度自适应布局

    在工作中经常遇到网页布局错乱的问题,往往引发的这种问题都是因为不同设备不同分辨率而导致。归根结底,是因为前端工程师经验不足,代码写得不够完好。以下是我总结及从网络搜集的一些网页布局…

    2018年10月8日
    2.9K
  • css布局基础总结

    前端css布局知识繁杂,实现方式多种多样。想写出高效、合理的布局,必须以深厚的css基础为前提。为了方便记忆和复习,将css布局要点记录如下。内容较多,应用方面说的不太详细,但都是…

    2018年9月13日
    2.3K
  • css如何利用transparent属性设置透明度?transparent属性绘制各种三角形

    想到用css设置元素透明度,大家的第一反应会是:用Opacity属性来设置透明度,其实在css中还有其他设置透明度的方法。本章给大家介绍用transparent属性设置透明度,以及…

    2021年2月25日
    1.3K
  • css晦涩难懂的点都在这啦

    CSS大家肯定都是会的但是每个人所撑握的情况都不一样,特别是已经工作几年的前辈(这里指的是我司)很多CSS玩法都不知道,可能他们已经习惯了用组件, 但是面试的时候又不可避免问,所以…

    2021年1月20日
    1.3K
  • CSS卡片堆栈

    在浏览各种APP及网站,往往会发现很多酷炫的布局及样式。搜集一下,补充自己的技能库,借鉴学习一下。 HTML CSS

    2019年12月31日
    2.0K
  • 两栏布局之左侧固定,右侧自适应的实现方法

    实现左侧固定,右侧自适应的两栏布局的方法有很多。其中经常用到的有float方法、BFC方法、CSS3的flex布局及grid布局。并非所有的布局都会在开发中使用,但是其中也会涉及一些知识点。

    2018年10月13日
    2.5K

发表回复

登录后才能评论