Is there such a thing as min-font-size and max-font-size?

前端 未结 11 569
隐瞒了意图╮
隐瞒了意图╮ 2021-01-29 22:13

I\'m trying to make a font in a div responsive to the browser window. So far, it has worked perfectly, but the parent div has a max-width of 525px. Res

相关标签:
11条回答
  • 2021-01-29 22:32

    I got some smooth results with these. It flows smoothly between the 3 width ranges, like a continuous piecewise function.

    @media screen and (min-width: 581px) and (max-width: 1760px){
        #expandingHeader {
            line-height:5.2vw;
            font-size: 5.99vw;
        }
        #tagLine {
            letter-spacing: .15vw;
            font-size: 1.7vw;
            line-height:1.0vw;
        }
    }
    
    @media screen and (min-width: 1761px){
        #expandingHeader {
            line-height:.9em;
            font-size: 7.03em;
    
        }
        #tagLine {
            letter-spacing: .15vw;
            font-size: 1.7vw;
            line-height:1.0vw;
        }
    }
    
    @media screen and (max-width: 580px){
        #expandingHeader {
            line-height:.9em;
            font-size: 2.3em;
        }
        #tagLine {
            letter-spacing: .1em;
            font-size: .65em;
            line-height: .10em;
        }
    }
    
    0 讨论(0)
  • 2021-01-29 22:33

    You can use Sass to control min and max font sizes. Here is a brilliant solution by Eduardo Boucas.

    https://eduardoboucas.com/blog/2015/06/18/viewport-sized-typography-with-minimum-and-maximum-sizes.html

    @mixin responsive-font($responsive, $min, $max: false, $fallback: false) {
      $responsive-unitless: $responsive / ($responsive - $responsive + 1);
      $dimension: if(unit($responsive) == 'vh', 'height', 'width');
      $min-breakpoint: $min / $responsive-unitless * 100;
    
      @media (max-#{$dimension}: #{$min-breakpoint}) {
        font-size: $min;
      }
    
      @if $max {
        $max-breakpoint: $max / $responsive-unitless * 100;
    
        @media (min-#{$dimension}: #{$max-breakpoint}) {
          font-size: $max;
        }
      }
    
      @if $fallback {
        font-size: $fallback;
      }
    
      font-size: $responsive;
    }
    
    .limit-min {
      @include responsive-font(3vw, 20px);
    }
    
    .limit-min-max {
      @include responsive-font(3vw, 20px, 50px);
    }
    
    0 讨论(0)
  • 2021-01-29 22:35

    Yes, there seems some restrictions by some browser in SVG. The developertool restrict it to 8000px; The following dynamically generated Chart fails for example in Chrome.

    Try http://www.xn--ffffdelei-n2a.de/2018/test-von-svt/

    <svg id="diagrammChart"
         width="100%"
         height="100%"
         viewBox="-400000 0 1000000 550000"
         font-size="27559"
         overflow="hidden"
         preserveAspectRatio="xMidYMid meet"
    >
        <g class="hover-check">
            <text class="hover-toggle" x="-16800" y="36857.506818182" opacity="1" height="24390.997159091" width="953959" font-size="27559">
                <set attributeName="opacity" to="1" begin="ExampShow56TestBarRect1.touchstart"
                     end="ExampShow56TestBarRect1.touchend">
                </set>
                <set attributeName="opacity" to="1" begin="ExampShow56TestBarRect1.mouseover"
                     end="ExampShow56TestBarRect1.mouseout">
                </set>
                Heinz: -16800
            </text>
            <rect class="hover-rect" x="-16800" y="12466.509659091" width="16800" height="24390.997159091" fill="darkred">
                <set attributeName="opacity" to="0.1" begin="ExampShow56TestBarRect1.mouseover"
                     end="ExampShow56TestBarRect1.mouseout">
                </set>
                <set attributeName="opacity" to="0.1" begin="ExampShow56TestBarRect1.touchstart"
                     end="ExampShow56TestBarRect1.touchend">
                </set>
            </rect>
            <rect id="ExampShow56TestBarRect1" x="-384261" y="0" width="953959" height="48781.994318182"
                  opacity="0">
            </rect>
    
        </g>
    </svg>
    
    0 讨论(0)
  • 2021-01-29 22:38

    I am coming a bit late here, I don't get that much credit for it, I am just doing a mix of the answers below because I was forced to do that for a project.

    So to answer the question : There is no such thing as this CSS property. I don't know why, but I think it's because they are afraid of a misused of this property, but I don't find any use case where it can be a serious problem.

    Whatever, what are the solutions ?

    Two tools will allow us to do that : media queries ans vw property

    1) There is a "fool" solution consisting in making a media query for every step we eant in our css, changing font from a fixed amount to another fixed amount. It works, but it is very boring to do, and you don't have a smooth linear aspect.

    2) As AlmostPitt explained, there is a brillant solution for the minima :

    font-size: calc(7px + .5vw);
    

    Minimum here would be 7px in addition to 0.5% of the view width. That is already really cool and working in most of cases. It does not require any media query, you just have to spend some time finding the right parameters.

    As you noticed it is a linear function, basic maths learn you that two points already find you the parameters. Then just fix the font-size in px you want for very large screens and for mobile version, then calculate if you want to do a scientific method. Thought, it is absolutely not necessary and you can just go by trying.

    3) Let's suppose you have a very boring client (like me) who absolutely wants a title to be one line and no more. If you used AlmostPitt solution, then you are in trouble because your font will keep growing, and if you have a fixed width container (like bootstrap stoping at 1140px or something in large windows). Here I suggest you to use also a media query. In fact you can just find the amout of px size maximum you can handle in your container before the aspect become unwanted (pxMax). This will be your maximum. Then you just have to find the exact screen width you must stop (wMax). (I let you inverse a linear function on your own).

    After that just do

    @media (min-width: [wMax]px) {
        h2{
            font-size: [pxMax]px;
        }
    }
    

    Then it is perfectly linear and your font-size stop growing ! Notice that you don't need to put your previous css property (calc...) in a media query under wMax because media query are considered as more imnportant and it will overwrite the previous property.

    I don't think it is useful to make a snippet for this, as you would have trouble to make it to whole screen and it is not rocket science afterall.

    Hope this could help others, and don't forget to thank AlmostPitt for his solution.

    0 讨论(0)
  • 2021-01-29 22:40

    Rucksack is brilliant, but you don't necessarily have to resort to build tools like Gulp or Grunt etc.

    I made a demo using CSS Custom Properties (CSS Variables) to easily control the min and max font sizes.

    Like so:

    * {
      /* Calculation */
      --diff: calc(var(--max-size) - var(--min-size));
      --responsive: calc((var(--min-size) * 1px) + var(--diff) * ((100vw - 420px) / (1200 - 420))); /* Ranges from 421px to 1199px */
    }
    
    h1 {
      --max-size: 50;
      --min-size: 25;
      font-size: var(--responsive);
    }
    
    h2 {
      --max-size: 40;
      --min-size: 20;
      font-size: var(--responsive);
    }
    
    0 讨论(0)
提交回复
热议问题