CSS中父元素和子元素之间的字体大小问题

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

Problems with font-size between the parent and the child element css

问题

Sure, here's the translated CSS and HTML code:

.logo{
    width: 25%;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 1.3rem;
    border: 2px solid gold;
}

.logo h1{
    margin-left: 1vw;
    /*font-size: 1.3rem;*/
    border: 2px solid red;
}
<section id="l" class="logo">
    <i class="fa-solid fa-dragon"></i>
    <h1>博客</h1>
</section>

This is the translation of the CSS and HTML code you provided.

英文:

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

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

.logo{
width: 25%;
display: flex;
justify-content: center;
align-items: center;
font-size: 1.3rem;
border: 2px solid gold;
}

.logo h1{
margin-left: 1vw;
/*font-size: 1.3rem;*/
border: 2px solid red;
}

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

&lt;section id=&quot;l&quot; class=&quot;logo&quot;&gt;
            &lt;i class=&quot;fa-solid fa-dragon&quot;&gt;&lt;/i&gt;
            &lt;h1&gt;Blog&lt;/h1&gt;
&lt;/section&gt;

<!-- end snippet -->

When I change the font size in the .logo selector I can see that my h1 element gets bigger. However, when I do the same but now in the .logo h1 selector the size its different. Note that for both cases I am putting the same font size. I don't know what is going on.

答案1

得分: 1

你可以使用浏览器的开发工具检查功能来精确查看字体大小是如何设置的。

如果父元素的字体大小设置为1.3rem,那就是在设置em的大小。

它的子元素(h1)没有被你明确地设置字体大小,所以它会继承浏览器设置的字体大小。在我查看的情况下,浏览器将h1的字体大小设置为1.5em。

因此,h1中的字体大小变为1.5 * 1.3rem。

在第二种情况下,你通过将字体大小明确设置为1.3rem来覆盖了h1的默认字体大小,这比1.5 * 1.3rem要小。

英文:

You can use your browser's devtools inspect facility to see exactly who/where the font-sizes are being set.

In the case that the font-size is set to 1.3rem in the parent, that is setting the em size.

It's child (the h1) doesn't explicitly have a font-size set by you so it picks up the font-size set by the browser. In the case I looked at this set the font-size of an h1 to 1.5em.

So the font-size in the h1 becomes 1.5 * 1.3rem

In the second case you have overidden the default font-size of the h1 by setting it specifically to 1.3rem - which is smaller than 1.5 * 1.3rem.

答案2

得分: -1

一rem等于根元素的字体大小。例如,如果根元素的字体大小设置为16px,则1rem等于16px。

使用rem单位可以更轻松地创建响应式设计,因为它允许您相对于根元素设置大小,而不是使用绝对像素值。这意味着如果用户更改浏览器设置中的默认字体大小,您的网站仍将保持比例正确。

英文:

One rem is equal to the font-size of the root element. For example, if the font-size of the root element is set to 16px, then 1rem is equal to 16px.

Using rem units can make it easier to create responsive designs, as it allows you to set sizes relative to the root element rather than using absolute pixel values. This means that if the user changes the default font size in their browser settings, your website will still look proportionally correct.

huangapple
  • 本文由 发表于 2023年6月13日 12:50:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76461772.html
匿名

发表评论

匿名网友

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

确定