【问题标题】:Font size with percentage value (%) not scaling with screen size带有百分比值 (%) 的字体大小不随屏幕大小缩放
【发布时间】:2023-03-22 08:35:01
【问题描述】:

我不明白为什么当我缩小屏幕时我的字体没有变小。

我将所有 CSS 设置为百分比,以便所有内容都能响应。

相反,当我缩小屏幕时,字体不会改变大小,从而破坏了设计。

我正在我的 24 英寸显示器上构建我的投资组合网站,然后在我的 13 英寸屏幕上检查它以确保它可以缩放。

#about {
  height: 100%;
  width: 100%;
  background-image: -webkit-gradient(linear, left top, right bottom, color-stop(0.12, #061419), color-stop(0.61, #36875F));
  background-repeat: no-repeat;
}
#about .container-fluid,
#about .container-fluid .row {
  height: 100%;
  width: 100%;
}
.about-left {
  height: 100%;
  background-image: url('../../images/jay-ocean.jpg');
  background-repeat: no-repeat;
  background-position: 0 100%;
}
.about-right {
  width: 50%;
}
.about-content {
  width: 50%;
  margin: 0 auto;
  padding-top: 23%;
  text-align: center;
}
.about-content-title h1 {
  text-align: center;
  font-size: 320%;
}
.about-content-info p {
  font-size: 110%;
  word-spacing: 0.8em;
}
.about-personal-info h4 {
  font-size: 110%;
  word-spacing: 0.8em;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<section id="about">

  <div class="container-fluid">

    <div class="row">

      <div class="about-left col-lg-6 col-md-6 col-sm-12 col-xs-12">
      </div>

      <div class="about-right col-lg-6 col-md-6 col-sm-12 col-xs-12">

        <div class="about-content">

          <div class="about-content-title">
            <h1>I'M JAY.</h1>
          </div>

          <br />

          <div class="about-content-info">
            <p>An entrepenurial minded, Full Stack Developer. Whenever I'm up against a challenge that I care about, it gives me a rush. Focusing on the big picture is important to me, but I never forget the smaller details. Anything that is not challenging
              is boring, and makes me yawn.
              <br />
              <br />Currently seeking a Javascript position, using the MEAN stack, in New York City. Being rebellious, ambitious, and hard working are values that are very important to me. I want to join a company that has similar values and has goals of reaching
              ridiculous levels of success, not just modest realistic ones. I love working with a solid team.
            </p>
          </div>

          <br />

          <div class="about-personal-info">
            <h4>Email:</h4>
            <h4>ICON ICON ICON ICON</h4>
          </div>

        </div>

      </div>

    </div>

  </div>

</section>

【问题讨论】:

  • 将字体大小设置为百分比会更改字体大小相对于原始大小。如果一个字体是 14pt 并且你添加了一个 200% 的字体大小规则,它将是 28pt。它与屏幕大小无关。
  • % 单位将某事物的大小设置为 -其父项-的百分比。我无法从给定的代码中看出,但 .about-content-title 是否设置为不同的大小?如果可能,您是否尝试过使用开发人员工具更轻松地分析不同的屏幕尺寸? 编辑: ninja'd(&@Eraph 的回答更准确)
  • 那么你定义了两次 font-size 吗?即字体大小:14px;字体大小:50%;?

标签: html css


【解决方案1】:

我想你要找的是viewport percentage units。

试试这个:

.about-content-title h1 { font-size: 5vw; }

通过此调整,当您重新调整浏览器窗口的大小时,字体将按比例缩放。

来自规范:

5.1.2. Viewport-percentage lengths: the vw, vh, vmin, vmax units

视口百分比长度与 初始包含块。当初始的高度或宽度 包含块被更改,它们会相应地缩放。

  • vw unit - 等于初始包含块宽度的 1%。
  • vh unit - 等于初始包含高度的 1% 阻止。
  • vmin 单位 - 等于vw 或vh 中的较小者。
  • vmax 单位 - 等于vw 或vh 中的较大者。

使用百分比值 (%) 不会相对于视口缩放字体,因为这些值是相对于父级或祖先级的。见规范:4.3. Percentages: the &lt;percentage&gt; type

【讨论】:

    【解决方案2】:

    字体在使用百分比时不会响应式地改变大小。

    请参阅Viewport Sized Typeography 上的这篇文章。

    您可以使用vmin、vmax、vw 或vh 来获得响应。例如:

    div {
      font-size: 5vmin;
    }
    

    这会随着屏幕的变大或变小而改变字体的大小。

    来自文章:

    1vw = 视口宽度的 1%

    1vh = 视口高度的 1%

    1vmin = 1vw 或 1vh,取较小者

    1vmax = 1vw 或 1vh,取较大者

    另一方面,百分比大小设置字体大小根据上下文,它将是其层次结构中首先具有特定设置字体大小的父级。

    【讨论】:

      猜你喜欢
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      • 2019-04-28
      • 2016-10-02
      • 2020-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多