HTML关于居中的问题

1: margin:0 auto;方式–>设置当前层在父级层中居中

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <style type="text/css">
            body{
                text-align: center;
            }
            #box{
                width: 720px;
                height: 250px;
                margin: 0 auto;
                border: 1px solid #FF0000;
            }
        </style>
    </head>
    <body>
        <div id="box"></div>
    </body>
</html>

2: 定位方式

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <style type="text/css">
            #box{
                width: 720px;
                height: 250px;
                position: relative;
                left: 50%;
                margin-left: -360px;
                border: 1px solid #FF0000;
            }
        </style>
    </head>
    <body>
        <div id="box"></div>
    </body>
</html>

3: 水平居中

.test1 ul {
    margin: 0 auto;
}

4: 垂直居中

.test2 {
    position: relative;
}
.test2 ul {
    position: absolute;
    left: 0;
    top: 50%;
    transform: translate(0, -50%);
}

5: 水平垂直居中

.test3 {
    position: relative;
}

.test3 ul {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
}

6: 文字居中text-align、line-height

文字在div中左右居中:

text-align:center

文字在div中垂直居中:

line-height值为外边框的高度

7: 图片居中vertical-align

可设置图片

vertical-align:middle

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值