Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Font-Weight CSS Transition in Chrome

Trying to get font-weight to gracefully transition from '400' to '600' using CSS but it doesn't appear to be working in Chrome. Is this a known bug or am I doing something wrong?

Check the Fiddle here for an example

like image 835
Funktr0n Avatar asked May 18 '13 22:05

Funktr0n


People also ask

Is there font weight in CSS?

The font-weight CSS property sets the weight (or boldness) of the font. The weights available depend on the font-family that is currently set.

How do I enable transition CSS?

To trigger an element's transition, toggle a class name on that element that triggers it. To pause an element's transition, use getComputedStyle and getPropertyValue at the point in the transition you want to pause it. Then set those CSS properties of that element equal to those values you just got.

How do I change the font weight in Google fonts?

font-weight:800; Like this its fallback proof, so if the google font should "fail" your backup font Arial/Helvetica(Sans-serif) use the same weight as the google font. Note that the different font weights have to be specifically imported via the link tag url (family query param of the google font url) in the header.


1 Answers

The problem is that font weights, when represented numerically, must be a multiple of 100. To animate between 400 and 600, the font would change from 400 to 500 to 600 (3 'frames', if you like) and wouldn't look very smooth. An animation wouldn't increment the weight by 1 each time (400, 401, 402...) it would increment the weight by 100 (400, 500, 600). If your animation lasted 2 seconds, after 1 second the weight would suddenly become 500, and after 2 seconds the weight would suddenly become 600; there are no in-between variations.

A further problem with what you're attempting here is that the font you're using (or JSFiddle's default, at least) doesn't have anything different for font-weight:500, meaning it defaults to 400:

<p style="font-weight:400;">a - 400, normal</p> <p style="font-weight:500;">a - 500 (no support, defaults to 400)</p> <p style="font-weight:600;">a - 600 (bold)</p> <p style="font-weight:650;">a - 650 (not valid, defaults to 400)</p> 

http://jsfiddle.net/r4gDh/6/

Numeric font weights for fonts that provide more than just normal and bold. If the exact weight given is unavailable, then 600-900 use the closest available darker weight (or, if there is none, the closest available lighter weight), and 100-500 use the closest available lighter weight (or, if there is none, the closest available darker weight). This means that for fonts that provide only normal and bold, 100-500 are normal, and 600-900 are bold.

https://developer.mozilla.org/en-US/docs/Web/CSS/font-weight

This basically means that you can't smoothly animate font-weight. Even if you had support for all weights between 100 and 900 the change wouldn't be pleasant and there would be a dramatic change between 500 and 600 (where lighter weight meets darker weight).

like image 170
James Donnelly Avatar answered Sep 17 '22 12:09

James Donnelly