Font scaling based on width of container

后端 未结 30 2954
面向向阳花
面向向阳花 2020-11-21 04:35

I\'m having a hard time getting my head around font scaling.

I currently have a website with a body font-size of 100%. 100% of what though? This seems t

相关标签:
30条回答
  • 2020-11-21 05:05

    Using vw, em & co. works for sure, but IMO it always needs a human's touch for fine-tuning.

    Here's a script I just wrote based on @tnt-rox' answer that tries to automatize that human's touch:

    $('#controller').click(function(){
        $('h2').each(function(){
            var
                $el = $(this),
                max = $el.get(0),
                el = null
            ;
            max =
                max
                ? max.offsetWidth
                : 320
            ;
            $el.css({
                'font-size': '1em',
                'display': 'inline',
            });
            el = $el.get(0);
    
            el.get_float = function(){
                var
                    fs = 0
                ;
                if (this.style && this.style.fontSize) {
                    fs = parseFloat(this.style.fontSize.replace(/([\d\.]+)em/g, '$1'));
                }
                return fs;
            };
    
            el.bigger = function(){
                this.style.fontSize = (this.get_float() + 0.1) + 'em';
            };
    
            while (el.offsetWidth < max) {
                el.bigger();
            }
    
            // Finishing touch.
            $el.css({
                'font-size': ((el.get_float() -0.1) +'em'),
                'line-height': 'normal',
                'display': '',
            });
        });  // end of (each)
    });    // end of (font scaling test)
    div {
      width: 50%;
      background-color: tomato;
      font-family: 'Arial';
    }
    
    h2 {
      white-space: nowrap;
    }
    
    h2:nth-child(2) {
      font-style: italic;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <input type="button" id="controller" value="Apply" />
    <div>
      <h2>Lorem ipsum dolor</h2>
      <h2>Test String</h2>
      <h2>Sweet Concatenation</h2>
      <h2>Font Scaling</h2>
    </div>

    It basically reduces the font-size to 1em and then starts incrementing by 0.1 until it reaches maximum width.

    JSFiddle

    0 讨论(0)
  • 2020-11-21 05:06

    There is a way to do this without JavaScript!

    You can use an inline SVG image. You can use CSS on an SVG if it is inline. You have to remember that using this method means your SVG image will respond to its container size.

    Try using the following solution...

    HTML

    <div>
      <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 360.96 358.98" >
          <text>SAVE $500</text>
      </svg>
    </div>
    

    CSS

    div {
      width: 50%; /* Set your container width */
      height: 50%; /* Set your container height */
    
    }
    
    svg {
      width: 100%;
      height: auto;
    
    }
    
    text {
      transform: translate(40px, 202px);
      font-size: 62px;
      fill: #000;
    }
    

    Example: https://jsfiddle.net/k8L4xLLa/32/

    Want something more flashy?

    SVG images also allow you to do cool stuff with shapes and junk. Check out this great use case for scalable text...

    https://jsfiddle.net/k8L4xLLa/14/

    0 讨论(0)
  • 2020-11-21 05:06

    You may be you looking for something like this:

    http://jsfiddle.net/sijav/dGsC9/4/
    http://fiddle.jshell.net/sijav/dGsC9/4/show/

    I have used flowtype, and it's working great (however it's JavaScript and not a pure CSS solution):

    $('body').flowtype({
        minFont: 10,
        maxFont: 40,
        minimum: 500,
        maximum: 1200,
        fontRatio: 70
    });
    
    0 讨论(0)
  • 2020-11-21 05:07

    In case it's helpful to anyone, most of the solutions in this thread were wrapping text into multiple lines, form e.

    But then I found this, and it worked:

    https://github.com/chunksnbits/jquery-quickfit

    Example usage:

    $('.someText').quickfit({max:50,tolerance:.4})

    0 讨论(0)
  • 2020-11-21 05:08

    In one of my projects I use a "mixture" between vw and vh to adjust the font size to my needs, for example:

    font-size: calc(3vw + 3vh);
    

    I know this doesn't answer the OP's question, but maybe it can be a solution to anyone else.

    0 讨论(0)
  • 2020-11-21 05:08

    Try http://simplefocus.com/flowtype/. This is what I use for my sites, and it has worked perfectly.

    0 讨论(0)
提交回复
热议问题