【问题标题】:How do I vertically center align every <p> with flex display?如何垂直居中对齐每个 <p> 与 flex 显示?
【发布时间】:2021-04-19 23:03:13
【问题描述】:

我在这里有几个嵌套元素,我的目标是垂直居中对齐每个元素,p 标签也是如此,我不知道我缺少 p 内的标签987654323@s 不垂直对齐。可能有什么解决办法?

main .content-slider {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 80px;
    background: #f5f5f5;
    border: 1px solid red;
}

main .content-slider .wrapper {
    display: flex;
    align-items: center;
    justify-content: center;
    border: 1px solid blue;
}

main .content-slider .wrapper div {
    display: flex;
    align-items: center;
    justify-content: center;
    flex-wrap: wrap;
    border: 1px solid greenyellow;
}

main .content-slider .wrapper div p {
    display: flex;
    align-items: center;
    justify-content: center;
}

.line-break {
    width: 100%;
}
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Nike Official Site. Nike DE</title>
</head>
<!--<link rel="stylesheet" href="node_modules/bootstrap/dist/css/bootstrap.css">-->
<!--<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">-->
<link crossorigin="anonymous" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" rel="stylesheet">
<link href="css/style.css" rel="stylesheet">
<link href="images/nike-icon.jpg" rel="shortcut icon">
<meta content="width=device-width, initial-scale=1.0" name="viewport">

<body>
    <main>
        <div class="content-slider">
            <div class="wrapper">
                <div class="item1">
                    <p>up to 30% off</p>
                    <div class="line-break"></div>
                    <p>it's here: the end of season sale. <a href="#"><span>shop</span></a></p>
                </div>
                <div class="item2">
                    <p></p>
                </div>
                <div class="item3">
                    <p></p>
                </div>
            </div>
        </div>

    </main>
    <script src="node_modules/jquery/dist/jquery.js"></script>
    <script src="node_modules/bootstrap/dist/js/bootstrap.js"></script>
</body>

</html>

【问题讨论】:

  • 你那里似乎有一些底部边距。否则我看不到问题。
  • 是的,淘气的引导程序! Tnx。

标签: css flexbox center-align


【解决方案1】:

main .content-slider {
    display: flex;
    align-items: center;
    justify-content: center;
    height: auto;
    background: #f5f5f5;
    border: 1px solid red;
}

main .content-slider .wrapper {
    display: flex;
    align-items: center;
    justify-content: center;
    border: 1px solid blue;
    flex-direction: column;
}

main .content-slider .wrapper div {
    display: flex;
    align-items: center;
    justify-content: center;
    flex-wrap: wrap;
    border: 1px solid greenyellow;
}

main .content-slider .wrapper div p {
    display: flex;
    align-items: center;
    justify-content: center;
}

.line-break {
    width: 100%;
}
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Nike Official Site. Nike DE</title>
</head>
<!--<link rel="stylesheet" href="node_modules/bootstrap/dist/css/bootstrap.css">-->
<!--<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">-->
<link crossorigin="anonymous" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" rel="stylesheet">
<link href="css/style.css" rel="stylesheet">
<link href="images/nike-icon.jpg" rel="shortcut icon">
<meta content="width=device-width, initial-scale=1.0" name="viewport">

<body>
    <main>
        <div class="content-slider">
            <div class="wrapper">
                <div class="item1">
                    <p>up to 30% off</p>
                    <div class="line-break"></div>
                    <p>it's here: the end of season sale. <a href="#"><span>shop</span></a></p>
                </div>
                <div class="item2">
                    <p></p>
                </div>
                <div class="item3">
                    <p></p>
                </div>
            </div>
        </div>

    </main>
    <script src="node_modules/jquery/dist/jquery.js"></script>
    <script src="node_modules/bootstrap/dist/js/bootstrap.js"></script>
</body>

</html>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-13
    • 2016-07-23
    • 2016-10-19
    • 1970-01-01
    • 2018-02-20
    相关资源
    最近更新 更多