我想使用指定我的字体大小vw,如
font-size: 3vw;
Run Code Online (Sandbox Code Playgroud)
但是,我还想将字体大小限制为36px.如何才能实现max-font-size不存在的等价物- 是使用媒体查询的唯一选择?
Dan*_*eld 105
font-size: 3vw;意味着字体大小将是视口宽度的3%.因此,当视口宽度为1200px时 - 字体大小为3%*1200px = 36px.
因此,使用单个媒体查询可以轻松实现最大字体大小36px,以覆盖默认的3vw字体大小值.
div {
font-size: 3vw;
}
@media screen and (min-width: 1200px) {
div {
font-size: 36px;
}
}Run Code Online (Sandbox Code Playgroud)
<div>hello</div>Run Code Online (Sandbox Code Playgroud)
话虽如此font-size,以上述方式使用视口单元是有问题的,因为当视口宽度小得多 - 比如320px - 那么渲染的字体大小将变为0.03 x 320 = 9.6px,这非常(太小).
为了克服这个问题,我建议使用一种名为Fluid Type AKA CSS Locks的技术.
CSS锁是一种特定的CSS值计算,其中:
- 有最小值和最大值,
- 和两个断点(通常基于视口宽度),
- 在这些断点之间,实际值从最小值到最大值呈线性关系.
(从这篇关于CSS锁的文章中也可以非常详细地解释数学.)
因此,假设我们要应用上述技术,使得最小字体大小在视口宽度为600px或更小时为16px,并且将线性增加,直到在视口宽度为1200px时达到最大32px.
我们可以使用这个SASS mixin为我们做所有的数学计算,这样CSS看起来像这样:
/*
1) Set a min-font-size of 16px when viewport width < 600px
2) Set a max-font-size of 32px when viewport width > 1200px and
3) linearly increase the font-size from 16->32px
between a viewport width of 600px-> 1200px
*/
div {
@include fluid-type(font-size, 600px, 1200px, 16px, 32px);
}
Run Code Online (Sandbox Code Playgroud)
// ----
// libsass (v3.3.6)
// ----
// =========================================================================
//
// PRECISE CONTROL OVER RESPONSIVE TYPOGRAPHY FOR SASS
// ---------------------------------------------------
// Indrek Paas @indrekpaas
//
// Inspired by Mike Riethmuller's Precise control over responsive typography
// http://madebymike.com.au/writing/precise-control-responsive-typography/
//
// `strip-unit()` function by Hugo Giraudel
//
// 11.08.2016 Remove redundant `&` self-reference
// 31.03.2016 Remove redundant parenthesis from output
// 02.10.2015 Add support for multiple properties
// 24.04.2015 Initial release
//
// =========================================================================
@function strip-unit($value) {
@return $value / ($value * 0 + 1);
}
@mixin fluid-type($properties, $min-vw, $max-vw, $min-value, $max-value) {
@each $property in $properties {
#{$property}: $min-value;
}
@media screen and (min-width: $min-vw) {
@each $property in $properties {
#{$property}: calc(#{$min-value} + #{strip-unit($max-value - $min-value)} * (100vw - #{$min-vw}) / #{strip-unit($max-vw - $min-vw)});
}
}
@media screen and (min-width: $max-vw) {
@each $property in $properties {
#{$property}: $max-value;
}
}
}
// Usage:
// ======
// /* Single property */
// html {
// @include fluid-type(font-size, 320px, 1366px, 14px, 18px);
// }
// /* Multiple properties with same values */
// h1 {
// @include fluid-type(padding-bottom padding-top, 20em, 70em, 2em, 4em);
// }
////////////////////////////////////////////////////////////////////////////
div {
@include fluid-type(font-size, 600px, 1200px, 16px, 32px);
}
@media screen and (max-width: 600px) {
div {
font-size: 16px;
}
}
@media screen and (min-width: 1200px) {
div {
font-size: 36px;
}
}Run Code Online (Sandbox Code Playgroud)
<div>Responsive Typography technique known as Fluid Type or CSS Locks.
Resize the browser window to see the effect.
</div>Run Code Online (Sandbox Code Playgroud)
采用CSS Poly Fluid Sizing的流体响应式排版
Vil*_*usL 25
另一种方法是慢慢增加字体大小,这不会限制最大字体大小,但即使在非常宽的宽度上它也会看起来更好.不以完美的方式回答问题,但是它的1行......
font-size: calc(16px + 1vw);
Run Code Online (Sandbox Code Playgroud)
Joh*_*kel 13
这是另一个想法。calc函数使用双精度浮点数。因此,它在1e18附近表现出阶跃函数。例如,
width: calc(6e18px + 100vw - 6e18px);
Run Code Online (Sandbox Code Playgroud)
这将捕捉到值0px,1024px,2048px等。请参见pen https://codepen.io/jdhenckel/pen/bQNgyW
步进功能可用于通过一些聪明的数学创建绝对值和最小/最大。例如
max(x, y) = x - (x + y) * step(y - x)
Run Code Online (Sandbox Code Playgroud)
当z <0时,给定的步骤(z)为零,否则为1。
只是一个想法,不是很实用,但是尝试起来可能很有趣。