html - 保持子元素的字体大小不受根元素字体大小的影响

标签 html css fonts size font-size

我正在创建一个单页布局,它使用一些纵横比媒体查询来防止滚动。

但我遇到的一个问题是,我希望在触发某个媒体查询后整个站点缩小:这相对容易,因为我所有的单位都定义为 rems,所有我必须做的是将根 html 元素的 font-size 更改为相关元素,例如 1.75vw 这使得我所有的 rem 定义的元素随窗口宽度缩小。

但是,我希望一个元素忽略这一点并保持其原始大小。我怎样才能让 footer 元素“突破”html 的字体大小,并像默认行为一样保持恒定大小?想法?

/* ///////////////////////////////// INITIAL /////////////////////////////// */
html {
  color: #777;
  font-family: sans-serif;
}
code {
  font-weight: bold;
  letter-spacing: -0.075rem;
}

/* /////////////////////////////////// VIEWPORT //////////////////////////// */

/* / / / / / / / / / / / / / / / / ASPECT RAITOS / / / / / / / / / / / / / / */
@media ( max-aspect-ratio: 3/1 ) {
  html {
    background-color: #eee;
    font-size: 1.75vw;
  }
}
<!DOCTYPE html>
<html>
  <body>
    <div class="wrp">

      <main>
        <p>Regular paragraph text</p> 
      </main>
      
      <footer>        
        <p>
          I don't want the <code>font-size</code> of this paragrah to be
          affected by the <code>html</code> element's <code>font-size</code>.
          I want this element's <code>font-size</code> to stay at 16 pixels
          at all times as the page is resized.
        </p>
      </footer>

    </div>
  </body>
</html>

最佳答案

只需为 footer 元素定义 css font-size 并且它不会受到影响

footer{ font-size:16px;}

Here is a working example:

/* ///////////////////////////////// INITIAL /////////////////////////////// */
html {
  color: #777;
  font-family: sans-serif;
}
code {
  font-weight: bold;
  letter-spacing: -0.075rem;
}
footer { font-size:16px ;}
/* /////////////////////////////////// VIEWPORT //////////////////////////// */

/* / / / / / / / / / / / / / / / / ASPECT RAITOS / / / / / / / / / / / / / / */
@media ( max-aspect-ratio: 3/1 ) {
  html {
    background-color: #eee;
    font-size: 1.75vw;
  }
}
<!DOCTYPE html>
<html>
  <body>
    <div class="wrp">

      <main>
        <p>Regular paragraph text</p> 
      </main>
      
      <footer>        
        <p>
          I don't want the <code>font-size</code> of this paragrah to be
          affected by the <code>html</code> element's <code>font-size</code>.
          I want this element's <code>font-size</code> to stay at 16 pixels
          at all times as the page is resized.
        </p>
      </footer>

    </div>
  </body>
</html>

关于html - 保持子元素的字体大小不受根元素字体大小的影响,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43567459/

相关文章:

html - ios从html获取href标签

jquery - 内容出现然后消失然后再次出现

jquery - 如何选择仅与特定类关联的 HTML 元素

emacs - 如何在emacs中设置自定义字体?

javascript - 如何使用 CSS 或 Javascript 或 jQuery 使粗体字体变细?

fonts - 仅更改 SWT 中的字体大小

html - 如何停止在 &lt;header&gt; 或 &lt;footer&gt; 下显示文本?

jquery - 带有 css 菜单的单页网站,需要 javascript 根据点击导航来淡入和淡出内容

c# - 在 C# 中以编程方式生成 HTML,以打印报告为目标

html - li 中跨度的中心背景图像,文本居中并在底部垂直对齐