[英]css responsive fluid html font size based on rem
I want to implement the example from css-tricks based on rems.我想基于 rems 实现来自css-tricks的示例。 This is what I got so far.
这是我到目前为止得到的。 However, the scaling does not work as intended, and the font size increases only tiny amounts.
但是,缩放不会按预期工作,字体大小只会增加很小的量。 What is the mistake?
错误是什么?
html {
font-size: 1rem;
}
@media screen and (min-width: 320px) {
html {
font-size: calc(1rem + 2 * ((100vw - 20rem) / 680));
}
}
@media screen and (min-width: 1000px) {
html {
font-size: 3rem;
}
}
I assume that 2 * ((100vw - 20rem) / 680)
returns a px value.我假设
2 * ((100vw - 20rem) / 680)
返回一个 px 值。 If that's true.如果那是真的。 How can I change it to return
rem
instead?我该如何更改它以返回
rem
呢?
Edited to add some clarifications: I want to use rem
instead of px
because this allows the user to overwrite the default font size in the browser.编辑以添加一些说明:我想使用
rem
而不是px
,因为这允许用户覆盖浏览器中的默认字体大小。
The term 2 * ((100vw - 20rem) / 680)
is between 0
and 2
(1 rem equals 16px on normal font size).术语
2 * ((100vw - 20rem) / 680)
介于0
和2
之间(1 rem 等于正常字体大小的 16px)。 This is what I want to achieve.这就是我想要实现的。 I want to have
font-size: 1rem + [0, 2]rem
between 320 and 1000px viewport width.我想要
font-size: 1rem + [0, 2]rem
在 320 和 1000px 视口宽度之间。 A linearly increasing rem function based on the viewport width.基于视口宽度线性增加的 rem function。
Here is a link to a sandbox example .这是沙盒示例的链接。
Edit 2: I think what I want to achieve is not possible.编辑 2:我认为我想要实现的目标是不可能的。 If the user increases the default font size by 50%, I want the scaling factor also increase by 50%:
font-size: 1rem + [0, 2 * 1.5]rem
.如果用户将默认字体大小增加 50%,我希望缩放因子也增加 50%:
font-size: 1rem + [0, 2 * 1.5]rem
。
The current problem is that the part 2 * ((100vw - 20rem) / 680))
needs to be rem based.当前的问题是第
2 * ((100vw - 20rem) / 680))
需要基于 rem。 This is not possible because there is no way in CSS to strip the unit.这是不可能的,因为 CSS 中没有办法剥离该单元。 If I could strip the unit, I could do this:
2rem * strip-unit((100vw - 20rem) / 680))
如果我可以剥离单元,我可以这样做:
2rem * strip-unit((100vw - 20rem) / 680))
If you want a responsive font size then you can use View Width too, no need to use calc or rem for that.如果您想要响应式字体大小,那么您也可以使用 View Width,无需为此使用 calc 或 rem。
Just change this so you can try it out:只需更改此设置,您就可以尝试一下:
HTML {
font-size: 5vw;
}
View width goes from 0 to 100 so you know how much room you have to work with.视图宽度从 0 到 100,因此您知道必须使用多少空间。
edit: I personally haven't found out how to scale on both axis yet but just the X-axis works good enough in most cases.编辑:我个人还没有找到如何在两个轴上进行缩放,但在大多数情况下,只有 X 轴工作得很好。
There is a great article on CSS Fluid Fonts at Modern Fluid Typography Using CSS Clamp在Modern Fluid Typography Using CSS Clamp上有一篇关于 CSS Fluid Fonts 的精彩文章
I made a simple Excel spread sheet that will generate the clamp function according to the article.我制作了一个简单的 Excel 电子表格,它将根据文章生成夹具 function。 CSS Clamp Excel Calculator
CSS 夹具 Excel 计算器
Here is a solution for fluid fonts I got from Creating a Fluid Type Scale with CSS Clamp这是流体 fonts 的解决方案,我从Creating a Fluid Type Scale with CSS Clamp得到
After some tweaking I ended up with the following Sass code to generate the clamp function.经过一些调整后,我最终得到以下 Sass 代码来生成钳位 function。
/*
From https://www.aleksandrhovhannisyan.com/blog/fluid-type-scale-with-css-clamp/
Generates the css clamp function.
Focused on font-size, but may be used for margins and padding
Usage:
clamped(min-size-px, max-size-px, min-browser-width-px, max-browser-width-px)
font-size: clamped(26px, 36px, 600px, 1200px);
font-size: clamped(26px, 36px); //using width default values
Output:
font-size: clamp(1.63rem, 1.11vw + 1.28rem, 2.25rem);
*/
@use "sass:math";
@use "sass:map";
// Default min-mix browser width values for clamped function
$default-min-bp: 500px;
$default-max-bp: 1400px;
// Convert pixels to rems.
@function to-rems($px) {
$rems: math.div($px, 16px) * 1rem;
@return $rems;
}
//round a to number of decimal places.
@function rnd($number, $places: 0) {
$n: 1;
@if $places > 0 {
@for $i from 1 through $places {
$n: $n * 10;
}
}
@return math.div(math.round($number * $n), $n);
}
// Generate css for clamp
@function clamped($min-px, $max-px, $min-bp: $default-min-bp, $max-bp: $default-max-bp) {
$slope: math.div($max-px - $min-px, $max-bp - $min-bp);
$slope-vw: rnd($slope * 100, 2);
$intercept-rems: rnd(to-rems($min-px - $slope * $min-bp), 2);
$min-rems: rnd(to-rems($min-px), 2);
$max-rems: rnd(to-rems($max-px), 2);
@return clamp(#{$min-rems}, #{$slope-vw}vw + #{$intercept-rems}, #{$max-rems});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.