简体   繁体   中英

Conditional font-size based on font-family

有没有办法在CSS或jQuery中编写基于font-family的条件字体大小?

Not really. But in some limited and relatively useless sense, yes.

In CSS, properties are independent of each other, except when CSS specifications define a relationship.

In scripting, you can do many things, but the simple jQuery way, querying for css('font-family') only gives you the specified list. If you declare font-family: Ariel, Helvetia, sansserif (this declaration is intentionally broken, though formally valid), it will give you the string Ariel, Helvetia, sansserif , even though none of those fonts is actually used for the element. So this approach is useless for the most common use case (where you want to use a list of fonts, to cover different systems, and wish to make font size vary according to which font gets actually used).

There are tricky ways to try to find out, in JavaScript, the font actually use for an element. They are usually based on evaluating the width of some text and comparing the result with the width of that text in some fonts.

Finally, there is the logical, though poorly supported way of using font-size-adjust . Only Firefox supports it, and the support is partly broken in a serious way (it uses wrong x-height information; eg, the x-height of Verdana is actually 0.545.

Yes you can easyly with jquery:

http://jsbin.com/acuzah/1/edit

if ($("div").css('font-family') === 'Arial')
{
  $("div").css({'color': '#00FF00'});
}

Using css() you read the property font-family, check if its Arial for example, then set whatever css property you want. Or add a class.

http://api.jquery.com/css/

http://api.jquery.com/addClass/

if ($("div").css('font-family') === 'Arial')
{
  $("div").css({'font-size': '100px'});
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM