CSS技巧 -- 水平居中和垂直居中

10 篇文章 0 订阅

CSS技巧

水平居中

文本的水平居中

  • 单行文本的水平居中

    • 对元素设置text-align: center;,text-align对文本、inline元素、inline-block元素有效,对block元素无效。

元素的水平居中

  • 块元素

    • 我们可以对块元素设置width,然后为块元素设置margin-left: auto; margin-right: auto;。

      • 等价写法是margin: 0 auto;
      • 注意必须对块元素设置width,这个方法才有效,如果不设置width,块元素会默认占满整行,这样就不能设置居中了。
<!DOCTYPE html>
<html>
    <head>
        <meta charset = "utf-8"/>
        <link type = "text/css" rel = "stylesheet" href = "a.css"/>
        <style type = "text/css">
            .wrapper{
                border: 0.1em solid red;
                font-size: 10px;
                width: 10em;
                height: 10em;
                margin-top: 20em;
            }
            div{
                width: 5em;
                height: 5em;
                border: 0.1em solid red;
                margin: 0 auto;
            }
        </style>
    </head>
    <body>
        <div class = "wrapper">
            <div></div>
        </div>
    </body>
</html>
  • 行内元素或inline-block、inline-flex等元素

    • 我们可以使用text-align: center;来设置水平居中。

垂直居中

文本的垂直居中

  • 对于单行文本

    • 我们可以设置line-height值和父元素height值相等,就可以设置单行文本在父元素中垂直居中了。
  • 对于多行文本

    • 使用span元素把多行文本包含起来,再对span元素设置display: inline-block;。

      • 这样就转换为了inline-block元素在父元素中垂直居中的问题。

元素的垂直居中

  • 对块元素设置垂直居中

    • 对于已经设置width和height的元素(父元素也已经设置width和height),我们可以使用position属性。

      • 我们对父元素设置position: relative;,对子元素设置position: absolute;,再对子元素设置top: 50%; left: 50%; margin-top设置为子元素height一半的负值,margin-left设置为子元素width一半的负值。

        • 这样我们实现了块元素在父元素中的水平垂直居中。
        • 如果只想实现垂直居中,就把margin-left属性和left属性去掉。
<!DOCTYPE html>
<html>
    <head>
        <meta charset = "utf-8"/>
        <link type = "text/css" rel = "stylesheet" href = "a.css"/>
        <style type = "text/css">
            .wrapper{
                border: 0.1em solid red;
                font-size: 10px;
                width: 10em;
                height: 10em;
                position: relative;
                margin-top: 20em;
            }
            div{
                position: absolute;
                width: 5em;
                height: 5em;
                border: 0.1em solid red;
                top: 50%;
                left: 50%;
                margin-left: -2.5em;
                margin-top: -2.5em;
            }
        </style>
    </head>
    <body>
        <div class = "wrapper">
            <div></div>
        </div>
    </body>
</html>
  • 对inline-block元素垂直居中

    • 对于inline-block元素,我们可以使用display: table-cell;和veritical-align: middle;实现垂直居中。
<!DOCTYPE html>
<html>
    <head>
        <meta charset = "utf-8"/>
        <link type = "text/css" rel = "stylesheet" href = "a.css"/>
        <style type = "text/css">
            .wrapper{
                border: 0.1em solid red;
                font-size: 10px;
                width: 10em;
                height: 10em;
                margin-top: 20em;
                display: table-cell;
                vertical-align: middle;
            }
            div{
                width: 5em;
                height: 5em;
                border: 0.1em solid red;
                display: inline-block;
            }
        </style>
    </head>
    <body>
        <div class = "wrapper">
            <div></div>
        </div>
    </body>
</html>

XMind: ZEN - Trial Version

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值