Responsive font size in CSS

前端 未结 30 3234
刺人心
刺人心 2020-11-22 07:23

I\'ve created a site using the Zurb Foundation 3 grid. Each page has a large h1:

30条回答
  •  独厮守ぢ
    2020-11-22 08:00

    In actual original Sass (not scss) you could use the below mixins to automatically set the paragraph's and all headings' font-size.

    I like it because it is much more compact. And quicker to type. Other than that, it provides the same functionality. Anyway, if you still want to stick to the new syntax - scss, then feel free to convert my Sass content to scss here: [CONVERT SASS TO SCSS HERE]

    Below I give you four Sass mixins. You will have to tweak their settings to your needs.

    =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
    

    After you finish playing with settings just make a call on one mixin - which is: +config-and-run-font-generator(). See code below and comments for more information.

    I guess you could do it automatically for a media query like it is done for header tags, but we all use different media queries, so it would not be appropriate for everyone. I use a mobile-first design approach, so this is how I would do that. Feel free to copy and use this code.

    COPY AND PASTE THESE MIXINS TO YOUR FILE:

    =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
    

    CONFIGURE ALL MIXINS TO YOUR NEEDS - PLAY WITH IT! :) AND THEN CALL IT ON THE TOP OF YOUR ACTUAL SASS CODE WITH:

    +config-and-run-font-generator()
    

    This would generate this output. You can customize parameters to generate different sets of results. However, because we all use different media queries, some mixins you will have to edit manually (style and media).

    GENERATED 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;
    
    }
    

提交回复
热议问题