Elias Zamaria
Elias Zamaria

Reputation: 101073

CSS: set font weight depending on fallback font

I am trying to set the font-weight for an element based on the font that gets chosen. For example, I may be trying to do something like this:

h1 {
    font-family: Arial Narrow, Impact, sans-serif;
    font-weight: ?;
}

Let's say I want the font-weight to be "bold" if the user has Arial Narrow installed on their system, but "normal" if the browser has to use Impact, and maybe "bold" if the user's system has neither of those fonts. Is this possible? If so, how would I go about doing this?

Upvotes: 10

Views: 2056

Answers (2)

CAnicaTA
CAnicaTA

Reputation: 11

Won't this work? Untested.

<style>
@font-face {
    font-family: "ArialBold";
    src: local("Arial Narrow");
    font-weight: bold;
}
@font-face {
    font-family: "ImpactNormal";
    src: local("Impact");
    font-weight: normal;
}
@font-face {
    font-family: "SansSerifBold";
    src: local("sans-serif");
    font-weight: bold;
}
h1 {
    font-family: ArialBold, ImpactNormal, SansSerifBold;
}
</style>

Upvotes: 0

Pekka
Pekka

Reputation: 449495

I'm 99.99999% sure this can't be done without some serious JavaScript magic, and even with JavaScript it's damn difficult to find out which font was used in the end.

Related: get computed font-family in JavaScript asked by yours truly

Upvotes: 7

Related Questions