For my website I have chosen to use some pretty obscure fonts in my font family. The most well known font (3rd in family) is Century Gothic, which most computers have.
font-family:Tw Cen MT,Gill Sans,Century Gothic,sans-serif;
The problem is that 12px font in century gothic is far bigger than a 12px font in Tw Cen MT & Gill Sans. IF a computer falls back on Century Gothic, the fonts will be a mess. I NEED A Jquery solution that says, century gothic will have a font-size of 75% of the normal value. I don't need it to detect fonts. I just need it to say, if century gotchi is being used, make the fo开发者_如何学Cnt size 75% of the normal value. Any solutions?
If you compose to a vertical rhythm this shouldn't really pose much of a problem.
http://24ways.org/2006/compose-to-a-vertical-rhythm
This is totally overkill, but you could use http://www.lalit.org/lab/javascript-css-font-detect and create a conditional.
I really suggest using something like the first method (vertical rhythm) to make then entire issue irrelevant if at all possible. You'll sleep much better at night.
I think the issue is the relatively tall x-height of Century Gothic. Try using ex
units in your font-size
declaration. ex
units are based on height, unlike em
, px
, and pt
.
Setting the line-height
to a pixel size would also help normalize the height, but would cause problems for users who change the text zoom level in their browser.
Edit: I went back and tested, and ex units don't help. Century Gothic is both taller and wider than the others.
The issue is, the DOM will not allow you to tell which specific font the user has from your listed alternates, and CSS doesn't allow separate font sizes (or adjustments) for each font.
However, JQuery can test the size of a container of text. So, if you provide a hidden element on your page with a known letter in it, you can test the width of that element on the user's browser.
Since the width of that letter will be determined by which font the user has installed, you can compare that width to the width of the same text in your preferred font on your own computer and compute a font size for the user that roughly matches what you intended.
Example code:
<style>
p { font-size: 15pt;
font-family:"Tw Cen MT","Gill Sans","Century Gothic",sans-serif;
}
p#testwidth {
/* Ensure test is invisible and isn't messed up with borders, etc. */
margin:0; padding:0; border:none; color:white; display:inline;
}
</style>
<p>Your text goes here.</p>
<p id="testwidth">m</p>
<script language="Javascript">
window.onload = function() {
// Same as your default CSS for font-size (just the number, not the units)
var desiredFontSize = 15;
// Experiment by testing width when Tw Cen MT *is* available. In pixels.
var expectedWidth = 17;
// width found will depend which font is available
var testedWidth = $('#testwidth').width();
// ratio, rounded to one decimal point
var normalizedFontSize = Math.round(
expectedWidth / testedWidth * desiredFontSize * 10) / 10;
// change the stylesheet to "fix" the font size (in your preferred units)
$('p').css('font-size', normalizedFontSize + 'pt');
}
Four caveats:
- It won't necessarily overcome every difference in size between two alternate fonts. Vertical alignment, etc. may not be perfect if they don't have your preferred font.
- It may annoy users who have a default text zoom level set in their browser, since this will essentially force the font size when the page loads. Fortunately, they can still zoom in or out as desired and override your forced font size.
- I seem to recall IE having a problem with font sizes with many numbers after the decimal point, so I rounded. Should be close enough.
- I used the width of a single letter "m". If you wanted a more accurate result, you could use a longer string of letters provided it doesn't wrap on the user's browser (which would break the width test). A few upper and lowercase letters and a space should be a good test.
There’s a script on lalit.org that guesses which fonts are available on the user’s machine.
You might be able to use that to see if Century Gothic is being used, and if so, load an extra stylesheet that amends your font sizes.
- http://www.lalit.org/lab/javascript-css-font-detect
精彩评论