在实际的原始
Sass(不是scss),您可以使用以下mixin自动设置段落和所有标题的
font-size
。
我喜欢它,因为它更加紧凑。而且输入速度更快。除此之外,它提供了相同的功能。无论如何,如果您仍然想坚持新语法-scss,则可以在此将我的Sass内容转换为scss:
[在此处将Sass转换为scss]
下面给出四个Sass mixin。您将需要根据自己的需求调整其设置。
=font-h1p-style-generator-manual() // You don’t need to use this one. Those are only styles to make it pretty.
=media-query-base-font-size-change-generator-manual() // This mixin sets the base body size that will be used by the h1-h6 tags to recalculate their size in a media query.
=h1p-font-size-generator-auto($h1-fs: 3em, $h1-step-down: 0.3, $body-min-font-size: 1.2em, $p-same-as-hx: 6) // Here you will set the size of h1 and size jumps between h tags
=config-and-run-font-generator() // This one only calls the other ones
在你完成设置后,只需调用一个 mixin - 即:+config-and-run-font-generator()。请参见下面的代码和注释以获取更多信息。
我猜你可以像标题标签一样自动为媒体查询执行此操作,但我们都使用不同的媒体查询,因此这对每个人来说都不合适。我采用移动优先设计方法,所以这就是我会这样做的方式。随意复制并使用此代码。
将这些 mixin 复制并粘贴到您的文件中:
=font-h1p-style-generator-manual()
body
font-family: "Source Sans Pro", "Helvetica Neue", Helvetica, Arial, sans-serif // google fonts
font-size: 100%
line-height: 1.3em
%headers
line-height: 1em
font-weight: 700
p
line-height: 1.3em
font-weight: 300
@for $i from 1 through 6
h#{$i}
@extend %headers
=media-query-base-font-size-change-generator-manual()
body
font-size: 1.2em
@media screen and (min-width: 680px)
body
font-size: 1.4em
@media screen and (min-width: 1224px)
body
font-size: 1.6em
@media screen and (min-width: 1400px)
body
font-size: 1.8em
=h1p-font-size-generator-auto($h1-fs: 3em, $h1-step-down: 0.3, $body-min-font-size: 1.2em, $p-same-as-hx: 6)
$h1-fs: $h1-fs // Set first header element to this size
$h1-step-down: $h1-step-down // Decrement each time by 0.3
$p-same-as-hx: $p-same-as-hx // Set p font-sieze same as h(6)
$h1-fs: $h1-fs + $h1-step-down // Looping correction
@for $i from 1 through 6
h#{$i}
font-size: $h1-fs - ($h1-step-down * $i)
@if $i == $p-same-as-hx
p
font-size: $h1-fs - ($h1-step-down * $i)
// RUN ONLY THIS MIXIN. IT WILL TRIGGER THE REST
=config-and-run-font-generator()
+font-h1p-style-generator-manual() // Just a place holder for our font style
+media-query-base-font-size-change-generator-manual() // Just a placeholder for our media query font size
+h1p-font-size-generator-auto($h1-fs: 2em, $h1-step-down: 0.2, $p-same-as-hx: 5) // Set all parameters here
将所有的混合器配置为您所需的样式 - 玩得开心! :) 然后在您实际的SASS代码顶部调用它:
+config-and-run-font-generator()
这将生成如下输出。您可以自定义参数以生成不同的结果集。但是,由于我们都使用不同的媒体查询,因此某些混合器必须手动编辑(样式和媒体)。
生成的CSS:
body {
font-family: "Source Sans Pro", "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size: 100%;
line-height: 1.3em;
word-wrap: break-word; }
h1, h2, h3, h4, h5, h6 {
line-height: 1em;
font-weight: 700; }
p {
line-height: 1.3em;
font-weight: 300; }
body {
font-size: 1.2em; }
@media screen and (min-width: 680px) {
body {
font-size: 1.4em; } }
@media screen and (min-width: 1224px) {
body {
font-size: 1.6em; } }
@media screen and (min-width: 1400px) {
body {
font-size: 1.8em; } }
h1 {
font-size: 2em; }
h2 {
font-size: 1.8em; }
h3 {
font-size: 1.6em; }
h4 {
font-size: 1.4em; }
h5 {
font-size: 1.2em; }
p {
font-size: 1.2em; }
h6 {
font-size: 1em;
}
css calc
与rem
和vw
结合起来,以避免在极小/大尺寸时出现问题。主要是想让人们意识到rem
避免了em
的问题,从而简化了响应式解决方案(通过媒体查询减少重复)。并且使其不必依赖于SASS
来实现此目的。 - ToolmakerSteverem
来处理其他元素的工作。偶尔使用上面提到的calc
。最重要的是避免重复的媒体查询,这些查询容易在进行更改时出现错误。 - ToolmakerSteve