15

The idea in the following is the first @font-face is for Firefox, the second for IE, and Arial for anything else that can't make sense of the first two. Its all working except for I want to give a different size in the case of Arial, and haven't figured out the syntax to do that.

@font-face {
  font-family: Tribeca;
  src: url("Tribeca.ttf"); format("truetype");
}

@font-face {
  font-family: TribecaIE;
  src: url("Tribec0.eot");
}

BODY
{
    FONT-FAMILY: Tribeca, TribecaIE, Arial; font-size: 195%;
}
Mark
  • 159
  • 1
  • 1
  • 5
  • 1
    I think `format()` should go in the `src` property. Use Font Squirrel's @font-face generator to get best sytax with hack for IE. – Kornel Feb 22 '10 at 22:32

8 Answers8

3

This is not supported by normal CSS rules..

I believe your options are

Gabriele Petrioli
  • 191,379
  • 34
  • 261
  • 317
  • A question related to my problem actually is this: I know on my Firefox it will do software updates without permission which I find kind of presumptuous, (I'm sure there's an option to turn it off) but are most people using Firefox getting the most current version inevitably (i.e. the one that supports @font-face.) – Mark Feb 06 '10 at 21:55
  • `Tools -> Options -> Advanced -> Update(tab)` for the software updates of firefox.. Usually firefox users will be quite up-to-date, because having selected firefox over IE means that they are a bit worried about their browsing experience.. (*i know this is argumentative.. just my opinion*) – Gabriele Petrioli Feb 06 '10 at 22:45
  • Nodernizr did not work for detecting browsers that didn't load the fonts. Unreliable. – Andy Fleming Jan 05 '11 at 07:36
3

I don't believe this is possible with css alone; we will probably need to use javascript.

All we want to do is specify a different font-size if Arial is the active font. Detecting the active font is not exactly straightforward, but here is one method that will work for our particular purpose. We can create a temporary element containing some Arial characters and measure its width, and then create a second element containing characters without specifying a font (so that it defaults to the "active" font) and compare widths. This won't tell us which font is currently active, but can indicate whether or not one of the embedded fonts was loaded with @font-face as they certainly won't have the same width as Arial. If the two elements' widths aren't equal we know that Arial could not have loaded, and so we will only adjust the font-size if the widths are equal.

Even if the browser is unable to load the Arial font, our test is still functional, because when we specify Arial during the test, the browser will default to the same font as it would for the second element. Both elements will still have equal width if the browser is unable to load the embedded fonts with @font-face.

If anyone would like me to further illustrate with code, I'll be happy to write the javascript.

defines
  • 10,229
  • 4
  • 40
  • 56
  • 1
    @DesignerGuy http://stackoverflow.com/questions/12312323/how-to-know-if-a-font-font-face-has-already-been-loaded/12316349#12316349 – Patrick Oct 02 '12 at 07:06
1

I believe it is this (close to what you have):

@font-face {
  font-family: Tribeca;
  src: url("Tribeca.ttf");
}

@font-face {
  font-family: Tribeca;
  src: url("Tribeca.eot");
}

body {
    font-family: Tribeca, Arial;
}

IE won't know how to open the ttf, so it won't bother. Then it will open the eot. Then, you just specify the font by the given name in the body declaration.

geowa4
  • 40,390
  • 17
  • 88
  • 107
1
@font-face {
    font-family: 'Tribeca';
    src: url(Tribeca.eot);
    src: local('Tribeca'), url(Tribeca.ttf) format('truetype');
    }

MSIE will ignore the last line cos it doesn't understand format rule. and yes as pointed by porneL above, format() should go in the src property.

local() will make supporting browsers use local font file if user has it instead of downloading from your server (and probably make IE ignore the line too).

as for the font-size adjustment, as pointed by Gaby: CSS 3 font-size-adjust. but it looks like it's not widely supported, yet.

widyakumara
  • 1,065
  • 1
  • 9
  • 14
0

Target your browsers by knowing which one reads which type of declaration. Conditional Comment load different CSS calls. Then you can specifically tell each one to do something different per rule.

Also there is typekit

Lorenzo816
  • 87
  • 7
0

To void code duplication with @font-face, you can do this via server side. If you use for example some urlrewrite, detect UA, if it's IE - return file with .eot extension, if it's normal browser - ttf.

As for me, it works great.

And for this case, you shouldn't change your css files, just should have 2 files: .ttf & .oet.

Alex Ivasyuv
  • 8,585
  • 17
  • 72
  • 90
0

Although it's against normal good-practices when using CSS, you could use the !important declaration in your conditional CSS.

As an example, we create two stylesheets: the 'default' one, which will have a section for Firefox-specific styles and an Internet Explorer stylesheet.

Then, using the standard <link rel="" /> method of importing stylesheets:

<link rel="stylesheet" href="normal/css/file.css" type="text/css" media="screen, projection">

<!--[if IE]><link rel="stylesheet" href="http://mysite.com/path/to/ie6.css" type="text/css" media="screen, projection"><![endif]-->

Within the 'default' stylesheet, our Firefox styles are wrapped in the following:

@-moz-document url-prefix() {

    #my-id { font-size: 100%; }

}

This works because the @-moz-document url-prefix() section is how Firefox addons style webpages. So, other browsers don't understand it and therefore just skip it.

nchpmn
  • 884
  • 2
  • 8
  • 22
  • Aside from browser specific-hacks, how can you set a different font-size for a backup font. For example, if a custom font-face can't load, how can you have it fall back to another font at a different size? – Andy Fleming Jan 08 '11 at 06:05
  • I'm not sure, unfortunately. Sorry. – nchpmn Jan 08 '11 at 22:45
-1
BODY
{
    FONT: 140% Arial;
    FONT: 195% Tribeca,TribecaIE;
}
Mark
  • 159
  • 1
  • 1
  • 5
  • 1
    Just to be clear, this doesn't appear to be the solution. In further repsonse to Gaby, I read that font-size-adjust isn't really all that well supported yet ( and my core problem here is support for older browsers). Also incredibly, it requires that you specify the aspect ratio of one of your fonts, as if anyone would know that. ALso both it and modernizr seem sort of like overkill to do something it seems browsers should be able to do to begin with , given that thy're able to use an alternate font (the three I just mentioned) So why not alternate font sizes - doesn't make any sense. – Mark Feb 06 '10 at 22:10
  • I can understand and share (*along with all web developers*) the frustration that comes from the multitude of incompatibilities and the lack of existing features in the standards.. it is an evolving process though .. and we are getting there (*i like to think..*). – Gabriele Petrioli Feb 06 '10 at 22:47
  • 1
    It isn't the solution. The second definition of the font property overrides the first, it doesn't provide a fallback. – Quentin Mar 01 '10 at 10:08