使用css使元素居中的9种方法

实现元素垂直居中的方式

1.将line-height设置为和height一样的值即可(只适用于单行文本)

line-height:100px;
height:100px;
<style>
        .box1{
            width: 100px;
            height: 100px;
            line-height: 100px;
            background-color: yellowgreen;
            position: absolute;
        }
    </style>
</head>
<body>
    <div class="box1">
        hello<br>
        world
    </div>
</body>
</html>

实现效果:只有第一行的文字才能实现垂直居中
在这里插入图片描述
2.将父元素的display设置为table-cell,同时vertical-align设置为middle

//父元素
display:table-cel;//将元素设置为单元格
vertical-align:middle;
   <style>
        .outer{
            width: 300px;
            height: 300px;
            background-color: tomato;
            display: table-cell;
            vertical-align: middle;
        }
        .box1{
            width: 100px;
            height: 100px;
            background-color: yellowgreen;
        }
    </style>
</head>
<body>
<div class="outer">
    <div class="box1">
    </div>
</div>
</body>
</html>

实现的效果:
在这里插入图片描述
3.子元素使用绝对定位方式, 以及height:固定值;top:0;bottom:0;margin: auto 0;父元素开启定位

// 子元素
height:100px
top:0;
bottom:0;
margin: auto 0;
position:absolute;
//父元素
position:relative;
<style>
        .outer{
            width: 300px;
            height: 300px;
            background-color: tomato;
            position: relative;
        }
        .box1{
            width: 100px;
            height: 100px;
            top: 0;
            bottom:0;
            margin:auto 0;
            position: absolute;
            background-color: yellowgreen;
        }
    </style>
</head>
<body>
<div class="outer">
    <div class="box1">
    </div>
</div>
</body>
</html>

实现效果同上。
4.子元素开启决定定位, top:50%,transform:translateY(-50%),父元素开启定位

// 子元素
position:absolute;
top:50%;
transform:translateY(-50%);
//父元素
position:relative;
<style>
        .outer{
            width: 300px;
            height: 300px;
            background-color: tomato;
            position: relative;
        }
        .box1{
            width: 100px;
            height: 100px;
            background-color: yellowgreen;
            position:absolute;
            top: 50%;
            transform: translateY(-50%);
        }
    </style>
</head>
<body>
<div class="outer">
    <div class="box1">
    </div>
</div>
</body>
</html>

实现效果同上。

5.父元素开启flex 并设置align-items:center;

//父元素
display:flex;
align-items:center;
<style>
        .outer{
            width: 300px;
            height: 300px;
            background-color: tomato;
            display: flex;
            align-items: center;
        }
        .box1{
            width: 100px;
            height: 100px;
            background-color: yellowgreen;

        }
    </style>
</head>
<body>
<div class="outer">
    <div class="box1">
    </div>
</div>
</body>
</html>

实现效果同上。

实现元素水平居中的方式

1.设置父元素的text-align为center(只对文本有效)

// 父元素
text-align:center;
//
<style>
        .outer{
            width: 300px;
            height: 300px;
            background-color: tomato;
            text-align: center;
        }
        .box1{
            width: 100px;
            height: 100px;
            background-color: yellowgreen;
        }
    </style>
</head>
<body>
<div class="outer">
    <div class="box1">
        hello<br>
        world
    </div>
</div>
</body>
</html>

实现的效果:只有文本实现了居中的效果
在这里插入图片描述
2.子元素宽度为固定值,margin:auto 0;

//子元素
width:100px;
marigin:auto 0;

<style>
        .outer{
            width: 300px;
            height: 300px;
            background-color: tomato;
        }
        .box1{
            width: 100px;
            height: 100px;
            background-color: yellowgreen;
            margin: 0 auto;
        }
    </style>
</head>
<body>
<div class="outer">
    <div class="box1">
    </div>
</div>
</body>
</html>

实现效果:
在这里插入图片描述
3.子元素使用绝对定位方式, 以及width:固定;left:0;right:0;margin:0 auto;父元素开启定位

// 子元素
width: 100px;
left:0;
right:0;
margin: 0 auto;
position:absolute;
//父元素
position:relative;
 <style>
        .outer{
            width: 300px;
            height: 300px;
            background-color: tomato;
            position: relative;
        }
        .box1{
            width: 100px;
            height: 100px;
            background-color: yellowgreen;
            position: absolute;
            left: 0;
            right: 0;
            margin: 0 auto;

        }
    </style>
</head>
<body>
<div class="outer">
    <div class="box1">
    </div>
</div>
</body>
</html>

实现效果同上。
4.子元素开启定位,left:50%,transform:translateX(-50%)

// 子元素
position:absolute;
left:50%;
transform:translateX(-50%);
//父元素
position:relative;

<style>
        .outer{
            width: 300px;
            height: 300px;
            background-color: tomato;
            position: relative;
        }
        .box1{
            width: 100px;
            height: 100px;
            background-color: yellowgreen;
            position: absolute;
            left: 50%;
            transform: translateX(-50%);
        }
    </style>
</head>
<body>
<div class="outer">
    <div class="box1">
    </div>
</div>
</body>
</html>

实现效果同上。
5.父元素开启flex 并设置justify-content:center;

// 父元素
display:flex;
justify-content:center;
<style>
        .outer{
            width: 300px;
            height: 300px;
            background-color: tomato;
            display: flex;
            justify-content: center;
        }
        .box1{
            width: 100px;
            height: 100px;
            background-color: yellowgreen;
        }
    </style>
</head>
<body>
<div class="outer">
    <div class="box1">
    </div>
</div>
</body>
</html>

实现效果同上。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值