h2 标签的标题未垂直居中

Caption of h2 tag not centering vertically

提问人:turner228 提问时间:10/3/2023 最后编辑:imvain2turner228 更新时间:10/3/2023 访问量:52

问:

我有这个代码,我希望元素中的“0”居中。无论我对班级做什么,“0”都停留在顶部中心,不会从顶部移动。<h2>.scoreSee image of problem

body {
    margin: 0;
    background-color: #0369A1;
    text-align: center;
}

@font-face {
    font-family: CursedTimerUlil-Aznm;
    src: url(fonts/CursedTimerUlil-Aznm.ttf);
}

h3 {
    font-family: Verdana, Geneva, Tahoma, sans-serif;
    color: #EEE;
    font-size: 40px;
    font-weight: 700;
    margin: 21px;
}

.score {
    font-family: CursedTimerUlil-Aznm;
    border: 3px solid #BE123C;
    border-radius: 4px;
    background-color: #FB7185;
    width: 200px;
    font-weight: 400;
    font-size: 90px;
    margin: 28px 0;
}

.container {
    display: flex;
    justify-content: space-around;
}

.counters {
    display: flex;
    justify-content: space-between;
    margin: 0;
}

.counter-btn {
    font-family: CursedTimerUlil-Aznm;
    font-weight: 400;
    font-size: 18px;
    width: 48px;
    height: 48px;
    border: 2px solid #EEE;
    border-radius: 3px;
    color: #EEE;
    background-color: #0369A1;
}
<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" href="styles.css">
    </head>
    <body>
        <div class="container">
            <div class="card">
                <h3>HOME</h3>
                <h2 class="score">0</h2>
                <div class="counters">
                    <button class="counter-btn">+1</button>
                    <button class="counter-btn">+2</button>
                    <button class="counter-btn">+3</button>
                </div>
            </div>
            <div class="card">
                <h3>GUEST</h3>
                <h2 class="score">0</h2>
                <div class="counters">
                    <button class="counter-btn">+1</button>
                    <button class="counter-btn">+2</button>
                    <button class="counter-btn">+3</button>
                </div>
            </div>
        </div>
    </body>
</html>

我有另一段代码,其中“0”完美地居中,但我无法弄清楚第一段代码有什么问题,以及两段代码之间有什么区别。Working code image.

我的工作HTML代码:

body {
    margin: 0;
    text-align: center;
}

.score {
    background: #0a0001;
    font-size: 90px;
    color: #F94F6D;
    width: 120px;
    margin: 20px;
}

.container {
    display: flex;
    justify-content: space-around;
}

button {
    background: none;
}
<html>
    <head>
        <link rel="stylesheet" href="index.css">
    </head>
    <body>
        <div class="container">
            <div class="column">
                <h3>HOME</h3>
                <h2 class="score">0</h2>
                <div>
                    <button>+1</button>
                    <button>+2</button>
                    <button>+3</button>
                </div>
            </div>
            <div class="column">
                <h3>GUEST</h3>
            </div>
        </div>
    </body>
</html>

HTML CSS 居中

评论

0赞 turner228 10/3/2023
我发现 h2 元素的这种显示问题是我使用的字体 (CursedTimerUlil-Aznm)。使用任何其他字体都可以正常工作

答:

0赞 David Fox 10/3/2023 #1

与第二个代码相比,第一个代码段将更多应用于 .score 的 CSS 属性,但下面的更改应该会得到您想要的结果。“应该”..

.score {
    font-family: CursedTimerUlil-Aznm;
    border: 3px solid #BE123C;
    border-radius: 4px;
    background-color: #FB7185;
    width: 200px;
    font-weight: 400;
    font-size: 90px;
    margin: 28px 0;
    display: flex;           /* Added this line */
    justify-content: center; /* Added this line */
    align-items: center;     /* Added this line */
}

0赞 Johannes 10/3/2023 #2

这仅仅取决于字体,字体在字符的上方和下方保留空间——通常在下方为延伸到基线以下的 y、p q 等字符(称为“降序”)保留更多空间。单个字符不会仅仅因为它们没有降序而区别对待——所有字符都必须适合保留的空间。

解决方案:要么使用另一种字体,要么为其父元素创建一些字体。padding-top