html - 如何对齐不同字体大小的文本部分

标签 html css

我正在尝试键入一些居中对齐的文本。每个短语都应该有不同的文本大小,尝试与前一个短语具有完全相同的视觉宽度,等等。

这是它必须如何的一个例子:http://soluzionipersuasive.com/img/text.png

我在 CSS 部分使用这段代码:

.a1 {
    font-family: Alfa Slab One;
    color: blue;
    font-size:35px;
    line-height: 1.3em;
    }

.b2 {
    font-family: Alfa Slab One;
    color: #0585c0;
    font-size:40px;
    line-height: 1.3em;
    }

.c3 {
    font-family: Alfa Slab One;
    color: blue;
    font-size:40px;
    line-height: 1.3em;
    }

...以及 html 区域中的另一个:

<span class="a1">Hello <span class="b2">How are you? <span class="c3">All good!</span>

但不幸的是,代码无法正常工作。它只提供正确的文本颜色。每个文本的文本字体和字体大小与第一个相同。不明白为什么。

有什么建议吗?

最佳答案

您可以使用 display flex 并对齐元素中心。它还有助于关闭跨度。

.flex {
  display: flex;
  align-items: center;
}

.a1 {
    font-family: Alfa Slab One;
    color: blue;
    font-size:35px;
    line-height: 1.3em;
    }

.b2 {
    font-family: Alfa Slab One;
    color: #0585c0;
    font-size:40px;
    line-height: 1.3em;
    }

.c3 {
    font-family: Alfa Slab One;
    color: blue;
    font-size:40px;
    line-height: 1.3em;
    }
<div class="flex">
  <span class="a1">Hello</span> 
  <span class="b2">How are you? </span>
  <span class="c3">All good!</span>
</div>

不确定您到底想要什么 - 这也可能是它:

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

.a1 {
    font-family: Alfa Slab One;
    color: blue;
    font-size:35px;
    line-height: 1.3em;
    }

.b2 {
    font-family: Alfa Slab One;
    color: #0585c0;
    font-size:40px;
    line-height: 1.3em;
    }

.c3 {
    font-family: Alfa Slab One;
    color: blue;
    font-size:40px;
    line-height: 1.3em;
    }
<div class="flex">
  <span class="a1">Hello</span> 
  <span class="b2">How are you? </span>
  <span class="c3">All good!</span>
</div>

关于html - 如何对齐不同字体大小的文本部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58247726/

相关文章:

javascript - 使用 onClick 事件触发 CSS3 转换

html - 如何使用 img 标签和 css 和类而不是 src 属性显示图像

css - 如何在 backbone js 中编写页面特定的 CSS

python - 使用 Python 刷新本地网页

html - 将图像和文本放置在 div 的底部

javascript - 如何判断表单是通过 Select onChange 提交还是通过 Submit 按钮提交

html - 垂直对齐可滚动的旋转文本

html - 如何在 Pagedown 编辑器中使 Markdown 到 HTML 的转换对 SEO 更友好

css - 如何模糊 div CSS 的一部分

html - 居中 UL 但保持文本左对齐