首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在将px更改为rem后,字体大小没有响应。

在将px更改为rem后,字体大小没有响应。
EN

Stack Overflow用户
提问于 2019-01-17 05:08:53
回答 1查看 2K关注 0票数 3

这个Dannie Vinther笔展示了如何在不使用media queries的情况下使字体具有响应性,之后我决定制作另一个钢笔,但使用rem作为Dannie的笔使用px。我做了一些改变,但不起作用。

这是我的代码:

代码语言:javascript
复制
* {
  /* Setting root font size*/
  --root-size: 20px;
  font-size: var(--root-size, 20px);
  /* Typography */
  --main-font: 'Slabo 27px', serif;
  /* Calculation, Ranges from 421px to 1199px */
  --responsive: calc((var(--min-font) * var(--root-size)) + (var(--max-font) - var(--min-font)) * ((100vw - 420px) / (1200 - 420)));
}

h1 {
  /* Set max and min font sizes */
  --max-font: 2.5;
  /* 2.5rem equals to 50px */
  --min-font: 1.25;
  /* 1.25rem equals to 25px */
  font-family: var(--main-font);
  font-size: var(--responsive);
}

p {
  font-family: sans-serif;
  margin: auto;
  width: fit-content;
  /* Set max and min font sizes */
  --max-font: 1;
  /* 1rem equals to 25px */
  --min-font: 0.7;
  /* 0.7rem equals to 14px */
  font-size: var(--responsive);
}

@media (min-width: 1200px) {
  h1,
  p {
    font-size: calc(var(--max-font) * var(--root-size));
  }
}

@media (max-width: 420px) {
  h1,
  p {
    font-size: calc(var(--min-font) * var(--root-size));
  }
}


/* Whatever */

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html,
body {
  height: 100%;
}

body {
  display: flex;
}

div {
  margin: auto;
  padding: var(--root-size);
}

a,
a:visited,
a:focus,
a:active,
a:link {
  text-decoration: none;
  outline: 0;
}

a {
  color: skyblue;
  transition: .2s ease-in-out color;
}

h1,
h2,
h3,
h4 {
  margin: .3em 0;
}
代码语言:javascript
复制
<link href="https://fonts.googleapis.com/css?family=Slabo+27px" rel="stylesheet">
<div>
  <h1>Responsive text with CSS Custom Properties</h1>
  <p>Custom Properties <a rel="noopener noreferrer" target="_blank" href="http://caniuse.com/#feat=css-variables">isn't supported</a> by IE.</p>
  <p>Inspired From <a href="https://codepen.io/dannievinther/pen/GNExxb?editors=1100">A PEN BY Dannie Vinther</a></p>
</div>

在我的代码中,font-size几乎是常量。它在720px上发生变化。当屏幕font-size大于或等于720px时,width就等于720px。和25px时,屏幕width小于720px。当屏幕font-size720px下降到420px时,只观察到很少的变化。

我找不出根本原因。请帮帮忙。谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-01-17 08:59:45

你的公式不对。如果我们考虑这一部分:((100vw - 420px) / (1200 - 420)),当100 to等于420 to,1px等于1200 to时。然后乘以(var(--max-font) - var(--min-font)),它等于1.25,因此基本上在0px1.25px之间有一个值,如果我们添加主部分((var(--min-font) * var(--root-size))),那么在25px之间只有一个font-size --E 212 26.25px not D14>。

在第二部分中,您忽略了var(--root-size)的乘法。这样做,您将在0px和25px之间有一个值,但是存在一个问题,因为您不能执行这个乘法,因为您将拥有使用calc无效的px*px。

为了克服这一问题,您的font-size需要定义为不统一的,而您的fomula需要进行如下调整:

代码语言:javascript
复制
* {
  /* Setting root font size*/
  --root-size: 20;
  font-size: calc(var(--root-size, 20) * 1px);
  /* Typography */
  --main-font: 'Slabo 27px', serif;
  /* Calculation, Ranges from 421px to 1199px */
  --responsive: calc((var(--min-font) * var(--root-size) * 1px) + (var(--max-font) - var(--min-font)) * var(--root-size) * ((100vw - 420px) / (1200 - 420)));
}

h1 {
  /* Set max and min font sizes */
  --max-font: 2.5;
  /* 2.5rem equals to 50px */
  --min-font: 1.25;
  /* 1.25rem equals to 25px */
  font-family: var(--main-font);
  font-size: var(--responsive);
}

p {
  font-family: sans-serif;
  margin: auto;
  width: fit-content;
  /* Set max and min font sizes */
  --max-font: 1;
  /* 1rem equals to 25px */
  --min-font: 0.7;
  /* 0.7rem equals to 14px */
  font-size: var(--responsive);
}

@media (min-width: 1200px) {
  h1,
  p {
    font-size: calc(var(--max-font) * var(--root-size) * 1px);
  }
  body {
   background:red;
  }
}

@media (max-width: 420px) {
  h1,
  p {
    font-size: calc(var(--min-font) * var(--root-size) * 1px);
  }
  body {
   background:blue;
  }
}


/* Whatever */

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html,
body {
  height: 100%;
}

body {
  display: flex;
}

div {
  margin: auto;
  padding: var(--root-size);
}

a,
a:visited,
a:focus,
a:active,
a:link {
  text-decoration: none;
  outline: 0;
}

a {
  color: skyblue;
  transition: .2s ease-in-out color;
}

h1,
h2,
h3,
h4 {
  margin: .3em 0;
}
代码语言:javascript
复制
<link href="https://fonts.googleapis.com/css?family=Slabo+27px" rel="stylesheet">
<div>
  <h1>Responsive text with CSS Custom Properties</h1>
  <p>Custom Properties <a rel="noopener noreferrer" target="_blank" href="http://caniuse.com/#feat=css-variables">isn't supported</a> by IE.</p>
  <p>Inspired From <a href="https://codepen.io/dannievinther/pen/GNExxb?editors=1100">A PEN BY Dannie Vinther</a></p>
</div>

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54229374

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档