Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specify fallback font sizes in CSS?

Is there any way to specify different font sizes for fallback fonts in CSS? I want to do something like this (which obviously does not work):

div {
    font-family: "Arial Narrow", Arial, Helvetica, sans-serif;
    font-size: 20px, 18px, 18px, 18px;
}

The idea being that Arial Narrow would display at 20px if the user has it installed; if not, the browser would fall back to Arial at 18px, then Helvetica at 18px, etc.

Or, could I use JS to achieve a similar effect?

like image 862
daGUY Avatar asked May 11 '11 21:05

daGUY


People also ask

What is CSS font fallback?

So basically fallback fonts are used when the current font isn't available. For example, if your CSS selector looked like this: h1 { font-family: 'Roboto', 'Open Sans', Helvetica, Arial, sans-serif; } The website will basically parse this as: Is 'Roboto' available?

How do I select font size in CSS?

For example, suppose the font-size of the <body> of the page is set to 16px . If the font-size you want is 12px , then you should specify 0.75em (because 12/16 = 0.75). Similarly, if you want a font size of 10px , then specify 0.625em (10/16 = 0.625); for 22px , specify 1.375em (22/16).

How do you change the font size in CSS font?

To change the size of your text with inline CSS, you have to do it with the style attribute. You type in the font-size property, and then assign it a value.

What does font size 12px mean?

The 12px is the height of the font in pixels. The 13px is the height of the line in pixels.


2 Answers

I understand what you want, but I think the answer to your question is "No, this can't be done in CSS", at least not in CSS2 afaik.

Hoping someone can prove me wrong, 'cause i want this too :D

I suppose JS can accomplish this, at least up to some point. Not sure if there is a "is this font installed?" method in JS, but you may be able to make some educated guesses based on OS and such. Got no experience there sorry.

Edit: some quick googling does provide a few clever JS tricks, though I haven't tried them yet. E.g. http://remysharp.com/2008/07/08/how-to-detect-if-a-font-is-installed-only-using-javascript/

Another edit, after some more searching: I was triggered by the "someone should propose it" :D. It seems CSS3 spec has the "font-size-adjust", which may be of use here. However, support in browsers other than Firefox may not be optimal at the time I write this. Here's the W3 word on that property: http://www.w3.org/TR/WD-font/#font-size-adjust

like image 52
Jeroen Avatar answered Oct 02 '22 01:10

Jeroen


I had a related problem using CSS3 fonts, which obviously don't work in IE6-8. The fallback font (Arial) is much bigger than the default font. Got round it in a similar way to mVChr but by detecting the browser. Not pretty really, but the joys of having to support IE. Code (with jQuery):

<script>
    $(document).ready(function() {
        //change font sizes in IE6-8 because they display Arial not Dincon
        if(navigator.userAgent.indexOf('MSIE 6') > -1 
         || navigator.userAgent.indexOf('MSIE 7') > -1 
         || navigator.userAgent.indexOf('MSIE 8') > -1) {
            $('.offending-class').css('font-size', '11px');
        }
    });
</script>
like image 36
james-geldart Avatar answered Oct 02 '22 02:10

james-geldart