57

I've read a fair bit about resizing fonts recently, most of it deriding using px as an unforgivable crime (ok, maybe not that bad, you get the idea) because it doesn't resize properly in older browsers.

I really want to have a standard that I use myself, in the past that has been px, simply because it's simple, easy to understand and fairly easy to achieve the exact font sizes that are specified in designs - but I'm now doubting using px.

I used em on a project recently because it needed text-resizing functionality which I made using jQuery. But I found it quite frustrating because of the way that em amplifies if you have two elements inside of each other both with an em size specified (hope that makes sense)

So I was wondering about using % for font resizing, I've seen a couple of big websites use this technique (namely, Yahoo) and from what I understand it seems to have all of the advantages of em without the incredibly annoying amplification thing.

So in short, I'm just wondering if there are any issues with using % for font-sizing in CSS? Would it be better than using PX in terms of font-resizing? And are there any noticeable draw backs?

Apologies if the blurb before the question is a little much :/ I'm still kind of getting used to the whole QA thing

Filburt
  • 17,626
  • 12
  • 64
  • 115
Sean
  • 6,389
  • 9
  • 45
  • 69
  • 4
    I might be wrong, but I think using `%` is prone to exactly the same flaws you attribute to `em`. If you have two `div` elements, one within the other, each with a `font-size: 110%` then that'll be calculated exactly the same as a `font-size: 1.1em`. Unless I'm *really* mistaken. – David Thomas Apr 25 '12 at 21:11
  • I'll try some HTMLing really quickly to check, I've not heard that before but it'd be a real bummer if it was true . . . main reason for me wanting to use % was that it didn't do that! – Sean Apr 25 '12 at 21:13
  • Nope, you were wrong, only em does the multiply flaw: http://jsfiddle.net/gHe5S/ – Sean Apr 25 '12 at 21:16
  • 2
    @Sean Dunwoody: Your fiddle is flawed. You're using different values for `em` and `%`. If I make the values equal (`1em` : `100%`), the results tally: http://jsfiddle.net/BoltClock/gHe5S/1 – BoltClock Apr 25 '12 at 21:20
  • Apologies, bad mistake on my part - it would appear that % has exactly the same drawbacks as em does :/ I was so sure that it didn't – Sean Apr 25 '12 at 21:24
  • 7
    CSS3 defines a new new unit "rem" which is em based upon the root (html) font-size.(http://www.w3.org/TR/css3-values/#rem-unit) Support is far from universal, but it does cover IE9, Safari 5, Chrome, Firefox 3.6+, and Opera 11.60+. (Source: http://snook.ca/archives/html_and_css/font-size-with-rem) – devstruck Apr 25 '12 at 21:31
  • 1
    http://stackoverflow.com/q/132685/340760 – BrunoLM Sep 25 '12 at 22:27

6 Answers6

24

In CSS3, use rem (root em). Sizing will not be affected by em size of the parent element.

The root font size is set by setting the font size on the :root pseudo-element, like so:

:root {
    font-size: 16px;
}
TylerH
  • 20,799
  • 66
  • 75
  • 101
Richik SC
  • 864
  • 1
  • 8
  • 18
  • 3
    This is what I've taken to doing actually! Some people seem to state they prefer `em` over `rem` but I've never understood why ... ems always seemed to overly complicate things for me. – Sean Jul 07 '14 at 18:06
  • 2
    This answer could be further improved by adding a comment of what is defined as root (without checking, I am assuming it is `body`), and maybe also browser support issues (if any). – Canned Man Aug 19 '18 at 19:30
  • 2
    The `:root` element in html documents is the `html` element, not the `body`. – vrugtehagel Nov 10 '20 at 19:11
11

try using this

body {
  margin: 0px;
  padding: 0px;
  font-size: 62.5%;
}

body>#wrapper {
  font-size:1em;
}

so, when you say something like 1em inside the "wrapper" you'll have a size very similar to 10px. here's a example table:

3em == 30px
.5em == 5px
8.5em == 85px

This will help you in the transition

P.d: of course, you need a wrapper tag after the body

Alwin Kesler
  • 1,450
  • 1
  • 20
  • 41
5

The standard in web design as far as I have experienced it is to use a percent to set the font size in the body, then to use ems to change the font sizing after that. You could use percents outside the body tag with no adverse side effects, but I think many developers find ems easier to work with (anyone is free to check me on this).

The danger comes if you use ems to set the body font size; older browsers choke and incorrectly display the text, especially when zoomed.

bronzehedwick
  • 2,862
  • 2
  • 22
  • 28
0

There's a jQuery plugin called FitText. It resizes text based on percents. If the visitor for some reason has JavaScript disabled, it'll just display as normal, so set a reasonable PX or EM backup.

It depends on jQuery, so you'll need to include that in your page (if you don't have it already).


jquery.fittext.js for reference:

/*global jQuery */
/*! 
* FitText.js 1.0
*
* Copyright 2011, Dave Rupert http://daverupert.com
* Released under the WTFPL license 
* http://sam.zoy.org/wtfpl/
*
* Date: Thu May 05 14:23:00 2011 -0600
*/

(function( $ ){

    $.fn.fitText = function( kompressor, options ) {

        var settings = {
        'minFontSize' : Number.NEGATIVE_INFINITY,
        'maxFontSize' : Number.POSITIVE_INFINITY
      };

            return this.each(function(){
                var $this = $(this);              // store the object
                var compressor = kompressor || 1; // set the compressor

        if ( options ) { 
          $.extend( settings, options );
        }

        // Resizer() resizes items based on the object width divided by the compressor * 10
                var resizer = function () {
                    $this.css('font-size', Math.max(Math.min($this.width() / (compressor*10), parseFloat(settings.maxFontSize)), parseFloat(settings.minFontSize)));
                };

                // Call once to set.
                resizer();

                // Call on resize. Opera debounces their resize by default. 
        $(window).resize(resizer);

            });

    };

})( jQuery );
Brigand
  • 84,529
  • 20
  • 165
  • 173
  • 1
    The question is about using different size measurements for fonts in CSS, not about fitting text. – Cobby Mar 04 '14 at 04:00
0

The issues and errors of % is only in the misconceptions.

Elements inherit font-size from their parent, unless overridden by a more specific selector. Example:

strong {font-size: 120%}

This example defines the font size for the strong element so that its text will always be larger than the surrounding text, in whatever context it is used. Assuming that headings and paragraphs use different font sizes, the emphasized words in this example will each be larger than their surrounding text. Source w3.org

Elements inherit font-size from their parent. So, if a font-size is specified for the body-element (or the html-element by default), all other elements inherit that value (unless overridden by a more specific selector).

body {
    font-size: 62.5%;
}

There is no problems to use % all way for a simplified understanding and a good responsive design. Then in some cases rem must be used to make a reference back to the :root value; if a reuse of an element inside another makes it smaller and you not want it to be resized.

Sure, you can get used to this way of thinking, but Many prefer the other way around by using rem as much as possible by a value of 1 with some decimals. Then they set this in beginning of css:

html {
    /* scaled version of users default setting */
    font-size: 62.5%;
}
Dan Froberg
  • 168
  • 8
-1

Maybe will this make more sense of making a font-resize script. I had made a script that did exactly what you desire, but I cant find it any more.

Pseudo-code:

var fontSize = 15; // (em) input
var fontResize = -1;// (em)input
fontSize = fontSize+fontResize; //current div

for(every inherent parent classname == 'classname-em')
document.classnameParentSize[numberofclass] = classnameChildSize[numberOfClass]/100*90;

So explained in words. The script needs to resize some fonts, when that is done it will also look for some parent divs to resize those fonts too, except they will be resized 10% less than its inherent. With some puzzling you will have the right conversion. Also try to avoid paddings and border widths.

Bob Dolman
  • 31
  • 9