css—使脱离文档流的元素水平居中

在静态页面设计中,当需要对绝对定位的元素进行布局时,其父元素通常设置为相对定位,导致容器脱离文档流。如何使这种情况下容器水平居中成为一个挑战。通过移除容器的`flex-direction: row`并添加`justify-content: center`,成功解决了这个问题,实现了理想的水平居中效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在设计静态页面时,有一部分布局需要使用绝对定位,那么它的父元素就要使用相对定位,这样一来就使承载绝对定位元素的容器脱离了文档流,现在让容器水平居中难倒了我这小菜鸡。它就一直这个样子:

 

 

这是源码: 

 

<!doctype html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        *,*:before,*:after {
            box-sizing: border-box;
        }
        .container3 {
            display: flex;
            flex-direction: row;
            align-items: center;
        }

        .left-black-box,.right-black-box {
            text-align: center;
            vertical-align: middle;
            background-color: #222222;
            float: left;
            padding: 30px 40px;
            position: relative;
            display: inline-block;
            left: 0;
            right: 0;
        }
        .right-black-box {
            margin-left: 20px;
        }
        .container3 .inner-word {
            width: 80%;
            display: table-cell;
            vertical-align: middle;
            padding: 22px;
            border-top:1px solid rgba(160, 160, 160, 0.82);
            border-bottom: 1px solid rgba(160, 160, 160, 0.82);
        }
        .container3 .inner-word span:nth-of-type(1) {
            color: #fff;
            display: block;
            margin-bottom: 1rem;
        }
        .container3 .inner-word span:nth-of-type(2) {
            color: #bebebe;
            display: block;
        }
        .red-circle-container {
            position: absolute;
            display: table;
            width: 50px;
            height: 50px;
            padding:10px;
            margin-left: 50%;
            border-radius: 50%;
            transform: translate(-125%,-50%);
            background-color: #222222;
        }
        .red-circle {
            display: table-cell;
            vertical-align: middle;
            background-color: #8b291b;
            border-radius: 50%;
        }
    </style>
</head>
<body>
    <div class="container3">
        <div class="left-black-box">
            <div class="red-circle-container">
                <div class="red-circle"></div>
            </div>
            <div class="inner-word">
                <span>FREE SHIPPING</span>
                <span><i>on orders of $50 or more</i></span>
            </div>
        </div>

        <div class="right-black-box">
            <div class="red-circle-container">
                <div class="red-circle"></div>
            </div>
            <div class="inner-word">
                <span>EASY RETURNS</span>
                <span><i>Shipping is paid by us</i></span>
            </div>
        </div>
    </div>
</body>
<ml>

自己实在不行了,开始积极找大佬请教,去了一行,又加了一行,完美搞定

就是去掉容器的flex-direction: row,加上了justify-content: center

.container3 {
            overflow: hidden;
            display: flex;
            justify-content: center;
            align-items: center;
        }

以上是改后的效果图,

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值