两个内联块级 div 在它们具有不同字体大小时如何垂直对齐?

huangapple go评论86阅读模式
英文:

How two inline block div are vertical aligned when they have differnt font size?

问题

以下是翻译好的部分:

有一个包含两个子 div 的 div,并将它们设置为 inline-block。这两个子 div 具有不同的字体大小,因此它们的高度相差很大。

浏览器基于什么来垂直放置左侧的子元素?为什么它们不都从顶部开始?

以下是代码示例:

<div>
    <h4>有一些想法吗?</h4>
    <div style="display: inline-block; width: 300px; background-color: rgb(80, 133, 130); font-size: 16px;">
        <span>AAA</span>
    </div>
    <div style="display: inline-block; width: 300px; background-color: rgb(154, 16, 99); font-size: 88px;">
        <span>BBB</span>
    </div>
</div>

以下是结果。谢谢。

两个内联块级 div 在它们具有不同字体大小时如何垂直对齐?

英文:

There is one div with two child div, and make them as inline-block. The two child div have different font size. So, their height are quite different.

What is browser based on to put left child in this position vertiaclly? Why don't they all starting from the very top?

Here is the code.

&lt;div&gt;
    &lt;h4&gt;Got Some Ideas for us?&lt;/h4&gt;
    &lt;div style=&quot;display: inline-block; width: 300px;background-color: rgb(80, 133, 130);font-size: 16px;&quot;&gt;
        &lt;span&gt;AAA&lt;/span&gt;
        
    &lt;/div&gt;
    &lt;div style=&quot;display: inline-block;width: 300px;background-color: rgb(154, 16, 99);font-size: 88px;&quot;&gt;
        
        &lt;span&gt;BBB&lt;/span&gt;
    &lt;/div&gt;
&lt;/div&gt;

Here is the result. Thanks.

两个内联块级 div 在它们具有不同字体大小时如何垂直对齐?

答案1

得分: 1

同一行的文本共享相同的基线。有两种方法可以使两个文本项顶部对齐。

首先,您可以为每个 div 应用 vertical-align: top;。这是因为这些项的 display 属性设置为 inline-block;

.small {
  display: inline-block;
  width: 300px;
  background-color: rgb(80, 133, 130);
  font-size: 16px;
  vertical-align: top;
}

.large {
  display: inline-block;
  width: 300px;
  background-color: rgb(154, 16, 99);
  font-size: 88px;
  vertical-align: top;
}
<div>
  <h4>Got Some Ideas for us?</h4>
  <div class="small">
    <span>AAA</span>
  </div>
  <div class="large">
    <span>BBB</span>
  </div>
</div>

另一种方法是使用 flexbox,并使用 align-items: flex-start;。这可能是我会选择的方式,但一切都取决于最终目标。Flexbox 非常强大。

.container {
  display: flex;
  align-items: flex-start;
}

.small {
  display: inline-block;
  width: 300px;
  background-color: rgb(80, 133, 130);
  font-size: 16px;
  vertical-align: top;
}

.large {
  display: inline-block;
  width: 300px;
  background-color: rgb(154, 16, 99);
  font-size: 88px;
  vertical-align: top;
}
<div>
  <h4>Got Some Ideas for us?</h4>
  <div class="container">
    <div class="small">
      <span>AAA</span>
    </div>
    <div class="large">
      <span>BBB</span>
    </div>
  </div>
</div>
英文:

Text on the same line share the same baseline. There are two ways that you could handle aligning the two text items to the top.

First, you can apply vertical-align: top; to each of the divs. This works because the items are display: inline-block;.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.small {
  display: inline-block;
  width: 300px;
  background-color: rgb(80, 133, 130);
  font-size: 16px;
  vertical-align: top;
}

.large {
  display: inline-block;
  width: 300px;
  background-color: rgb(154, 16, 99);
  font-size: 88px;
  vertical-align: top;
}

<!-- language: lang-html -->

&lt;div&gt;
  &lt;h4&gt;Got Some Ideas for us?&lt;/h4&gt;
    &lt;div class=&quot;small&quot;&gt;
      &lt;span&gt;AAA&lt;/span&gt;

    &lt;/div&gt;
    &lt;div class=&quot;large&quot;&gt;

      &lt;span&gt;BBB&lt;/span&gt;
    &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

The other way would be to use flexbox, and align-items: flex-start;. This is probably the way that I would do it, but it all depends on the end goal. Flexbox is really powerful.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.container {
  display: flex;
  align-items: flex-start;
}

.small {
  display: inline-block;
  width: 300px;
  background-color: rgb(80, 133, 130);
  font-size: 16px;
  vertical-align: top;
}

.large {
  display: inline-block;
  width: 300px;
  background-color: rgb(154, 16, 99);
  font-size: 88px;
  vertical-align: top;
}

<!-- language: lang-html -->

&lt;div&gt;
  &lt;h4&gt;Got Some Ideas for us?&lt;/h4&gt;
  &lt;div class=&quot;container&quot;&gt;
    &lt;div class=&quot;small&quot;&gt;
      &lt;span&gt;AAA&lt;/span&gt;

    &lt;/div&gt;
    &lt;div class=&quot;large&quot;&gt;

      &lt;span&gt;BBB&lt;/span&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

答案2

得分: -1

默认对齐方式基于文本的基线。

Image

你可以通过使用 flexbox(在父元素上设置 "display:flex; align-items: baseline")来获得类似的行为。

英文:

The default alignment is based on the baselines of the texts.

Image

You can obtain similar behaviour usign flexbox ("display:flex; align-items: baseline" on the parent)

答案3

得分: -2

你面临的问题是 line-height

将较小的文本垂直对齐到顶部,并减小较大字体的行高。

结果:

两个内联块级 div 在它们具有不同字体大小时如何垂直对齐?

代码:

<div style="width: 300px; vertical-align:top;">
    <div style="display: inline-block; font-size: 16px; vertical-align:top;">AAA</div>
    <div style="display: inline-block; font-size: 88px; line-height: 60px;">BBB</div>
</div>

资源以scss计算适当的行高,与字体大小相比,以垂直对齐文本:

https://iamvdo.me/en/blog/css-font-metrics-line-height-and-vertical-align

英文:

The issue you're facing is line-height.

vertical align your smaller text to top, and reduce the line height of the bigger font.

Result:

两个内联块级 div 在它们具有不同字体大小时如何垂直对齐?

code:

&lt;div style=&quot;width: 300px; vertical-align:top;&quot;&gt;
    &lt;div style=&quot;display: inline-block; font-size: 16px; vertical-align:top;&quot;&gt;AAA&lt;/div&gt;
    &lt;div style=&quot;display: inline-block; font-size: 88px; line-height: 60px;&quot;&gt;BBB&lt;/div&gt;
&lt;/div&gt;

Resource to calculate proper line height compared to font size to vertically align your text using scss:

https://iamvdo.me/en/blog/css-font-metrics-line-height-and-vertical-align

答案4

得分: -3

因为文本的字体大小使它们具有相同的字体大小,所以这将解决你的问题。

英文:

it is because the font size of text make them same font size so that will fix you issue

答案5

得分: -3

这是您的答案 @Tim R,它不是来自ChatGPT。

英文:

here is your answer @Tim R its not from the chatGPT 两个内联块级 div 在它们具有不同字体大小时如何垂直对齐?

huangapple
  • 本文由 发表于 2023年8月5日 14:29:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76840410.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定