【问题标题】:How do I center an h4 that includes both regular text and links, without losing the spaces between them?如何将包含常规文本和链接的 h4 居中,而不会丢失它们之间的空格?
【发布时间】:2021-04-04 16:25:32
【问题描述】:

我有这个<h4> 标签:

<h4>
    A <a href="https://openjdk.java.net/">Java</a> and <a href="https://kotl.in">Kotlin</a> coder.
</h4>

应用了这个 CSS(继承自 body):

body {
    font-family: jetbrains-mono;
    background-color: #444;
    color: #EEEEEE;
    font-size: 18px;
    max-width: 650px;
    line-height: 1.2;
    display: grid;
    align-items: center;
    margin: auto auto;
}

结果如下:

仅使用 align-content: center 会产生相同的结果。

问题是,如果我使用弹性框而不是网格来使文本居中,如下所示:

display: flex;
align-items: center;
justify-content: center;

这就是发生的事情:

我该如何解决这个问题? 完整的 html 文件在这里:https://github.com/TheOnlyTails/theonlytails.com/blob/main/index.html 完整的 css 文件在这里:https://github.com/TheOnlyTails/theonlytails.com/blob/main/style.css

【问题讨论】:

    标签: html css centering


    【解决方案1】:

    要使其成为一列,您需要flex-direction: column; 属性。将其添加到您添加 display: flex 属性的容器中,如下所示:

       display: flex;
       align-items: center;
       justify-content: center;
       flex-direction: column;
    

    我希望这能解决您的问题。

    【讨论】:

    • 它似乎可以正常工作,但现在两行之间的空间更大了。
    • 可以发截图吗?
    • 由于它们是标题,您可以在 CSS 中减少它们的边距。顺便说一句,我认为它们之间的间距相同,但由于它们居中,它们看起来间距更大。
    猜你喜欢
    • 2016-01-08
    • 1970-01-01
    • 1970-01-01
    • 2014-10-08
    • 2021-10-11
    • 1970-01-01
    • 1970-01-01
    • 2011-02-02
    • 2014-02-26
    相关资源
    最近更新 更多