在实际的原始Sass(不是 scss)中,您可以使用以下 mixins 自动设置段落和所有标题font-size。
我喜欢它,因为它更紧凑。而且打字速度更快。除此之外,它提供相同的功能。无论如何,如果你仍然想坚持新的语法 - scss,那么请随意将我的 Sass 内容转换为 scss:
 [CONVERT SASS TO SCSS HERE]
下面我给你四个 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()。有关更多信息,请参见下面的代码和注释。
我想您可以像对标头标签一样为媒体查询自动执行此操作,但是我们都使用不同的媒体查询,因此并不适合所有人。我使用移动优先的设计方法,所以我会这样做。随意复制和使用此代码。
将这些混音复制并粘贴到您的文件中:
=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()
这将生成此输出。您可以自定义参数以生成不同的结果集。然而,因为我们都使用不同的媒体查询,一些 mixin 必须手动编辑(样式和媒体)。
生成的 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;
}