Responsive font size in CSS

前端 未结 30 2752
刺人心
刺人心 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 07:57

    I had just come up with an idea with which you only have to define the font size once per element, but it is still influenced by media queries.

    First, I set the variable "--text-scale-unit" to "1vh" or "1vw", depending on the viewport using the media queries.

    Then I use the variable using calc() and my multiplicator number for font-size:

    /* Define a variable based on the orientation. */
    /* The value can be customized to fit your needs. */
    @media (orientation: landscape) {
      :root{
        --text-scale-unit: 1vh;
      }
    }
    @media (orientation: portrait) {
      :root {
        --text-scale-unit: 1vw;
      }
    }
    
    
    /* Use a variable with calc and multiplication. */
    .large {
      font-size: calc(var(--text-scale-unit) * 20);
    }
    .middle {
      font-size: calc(var(--text-scale-unit) * 10);
    }
    .small {
      font-size: calc(var(--text-scale-unit) * 5);
    }
    .extra-small {
      font-size: calc(var(--text-scale-unit) * 2);
    }
    <span class="middle">
      Responsive
    </span>
    <span class="large">
      text
    </span>
    <span class="small">
      with one
    </span>
    <span class="extra-small">
      font-size tag.
    </span>

    In my example I only used the orientation of the viewport, but the principle should be possible with any media queries.

    0 讨论(0)
  • 2020-11-22 07:57
     h1 { font-size: 2.25em; } 
     h2 { font-size: 1.875em; }
     h3 { font-size: 1.5em; }
     h4 { font-size: 1.125em; }
     h5 { font-size: 0.875em; }
     h6 { font-size: 0.75em; }
    
    0 讨论(0)
  • 2020-11-22 07:59

    If you don't mind to use a jQuery solution you can try TextFill plugin

    jQuery TextFill resizes text to fit into a container and makes font size as big as possible.

    https://github.com/jquery-textfill/jquery-textfill

    0 讨论(0)
  • 2020-11-22 07:59

    jQuery's "FitText" is probably the best responsive header solution. Check it out at GitHub: https://github.com/davatron5000/FitText.js

    0 讨论(0)
  • 2020-11-22 08:00

    I've been playing around with ways to overcome this issue, and believe I have found a solution:

    If you can write your application for Internet Explorer 9 (and later) and all other modern browsers that support CSS calc(), rem units, and vmin units. You can use this to achieve scalable text without media queries:

    body {
      font-size: calc(0.75em + 1vmin);
    }
    

    Here it is in action: http://codepen.io/csuwldcat/pen/qOqVNO

    0 讨论(0)
  • 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;
    
    }
    
    0 讨论(0)
提交回复
热议问题