views:

111

answers:

2

Answerers: How does one literally detect the user's font scale, so that it may be used as a variable?

I am trying to create some if/else statements based on the user's font scaling (not browser zooming), to extend the usability of my sites.

The two given answers are workarounds for a single case (the scenario described). The scenario I initially presented was not the best selection on my part. Still, it is usable, and it's the easiest-to-understand scenario I could come up with on the fly.

"If the font size is changed the value of the offsetWidth/Height properties of elements that are either sized in relation to their text contents or CSS sized with font size related units (em, ex, etc.) will change."

I am trying to figure a way to base my layout/css on font size, as set by the user in his or her browser settings. For example, in Chrome, if a user has set his or her primary font size to 24--

Chrome: "Wrench">"Options">"Under the Hood">"Change Font and Language Settings">"Fonts and Encoding">Serif Font>"Change">Font Size="24".

--

How can I pick up the event? Working with an example..., let's say I have some DIV's with a set height, 60px:

<div class="dwarfer" style="height: 60px;">Whoa?</div>

Default font size is 9 pt. If the user sets his or her font size to 24 pt (not very high for someone who is visually impaired), ctrl+'mousewheeling down' to the minimum font size may not cause the text to reveal. So, it's best to adjust the height by a variable:

$divheight = 5px/1em

If you know what the event is or how to find it, please find the event. Then, please create an example in PHP that uses $divheight to adjust the height based on the user-selected browser font size.

The closest solution I have found for detecting font changes is:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" » "http://www.w3.org/TR/html4/strict.dtd"&gt;
<html dir="ltr">
<head>
  <meta http-equiv="Content-Type" content="text/html; » charset=utf-8"> 
  <title>Font Resizer Demo</title>
  <script type="text/javascript" src=" » textresizedetector.js"></script>
</head>
<body>
  <h1>Resize me, now!</h1>
</body>
</html>

AND

<script type="text/javascript" » src="textresizedetector.js"></script>
<script type="text/javascript">
  // <![CDATA[
  /* id of element to check for and insert test SPAN into */
  TextResizeDetector.TARGET_ELEMENT_ID = 'header';
  /* function to call once TextResizeDetector was initialized */
  TextResizeDetector.USER_INIT_FUNC = init;
  // ]]>
</script>

As seen and tutorialized at http://www.alistapart.com/articles/fontresizing/.

Even using this code, I am having trouble incorporating $divheight directly into my CSS file:

/* CSS */
<?php
  //variables
  $divheight = 5px/1em
  //end variables

  //styles
  echo '.dwarfer{' . "\n" . 'height:' . $divheight; . "\n" . '}'
  //end styles
?>

Code comments are GREATLY appreciated! Thank you!

+2  A: 

Don't use fixed css units like px and pt. Use em or ex.

em  1em is equal to the current font size. 2em means 2 times the size of the current font.
      E.g., if an element is displayed with a font of 12 pt, then '2em' is 24 pt. The 'em'
      is a very useful unit in CSS, since it can adapt automatically to the font that the
      reader uses

ex  one ex is the x-height of a font (x-height is usually about half the font-size)
sheeks06
Thank you, sheeks06. I greatly appreciate the criticism/feedback. I've changed px to em where appropriate. If you know, please include an answer to the question: how do I detect the user-selected/browser-set/device-set font size?
Fohsap
Do you mean how do you detect the font-size?
sheeks06
Yes. How do you detect the BROWSER's font scale? I'm trying to create code that accommodates font-only scaling and not just 'browser zooming'. Ctrl+mwheelUp is not very usable, at all. I always want to make my websites more accessible. It's one of my main focuses. I'd really appreciate an answer as to how one might actually pick up the font scale the user has selected. :)
Fohsap
What are you going to use that for? If your going to use em or ex as a css unit, your layout will be will always be relative to the font-size. Also, have only 1 font-size with a fixed unit declared in your css (prefered if declare to the body). the others will use em or ex. This concept is called liquid design. look it up. http://www.maxdesign.com.au/articles/css-layouts/three-liquid/
sheeks06
A lack of creativity doesn't change the validity or content of the question, or the need for a precise answer. Here's one of perhaps as many as a billion different applications: there is a list of 30 short div's stacked in a page that uses pagination. For every increase of two font sizes, remove 1 div. At the very least, you will have 3 div's. The user can still use the pagination features. They can quickly scroll the page, using only the home and end keys to see all content. The dynamic browsing experience, as intended: fun, consisting of very few keystrokes.
Fohsap
Not to mention, it's an instant indicator that the viewer is a low-vision user. And because I am always trying to make the browsing experience as personal as possible--trying to help the viewer/reader out; basically, kissing my customer's butter--, it would be a huge advantage just to know. To know who is using, what they need, why they need what they need, etc. And overall, the more usable, the better.
Fohsap
I'm not referring to the application.. I'm saying what are you going to do with the font-size (as a value. ex. 12 pt)? Are you going to use it in a computation? If so, you can do it but not directly. 1em = current font size, right? Assign this unit to an HTML element with texts inside. Then, get the height of the element using javascipt. You'll get an APPROX. font-size in px.
sheeks06
Yes, I've been thinking the same thing, sheeks06. That seems to be the best generic solution. Thank you.
Fohsap
And yes, computation. Thank you. This is very helpful.
Fohsap
It's going to be a lot more complicated than you think though. Because users can still change font-size even when the page is already rendered without firing any javascript event(might). My suggestion is approach this problem using pure CSS rather than involving javascript...
sheeks06
I was thinking of using jQuery and CSS or ajax and CSS.
Fohsap
Ok. B) what ever makes you happy :)
sheeks06
+1  A: 

use units of em rather than px. em is relative to the current font size, so to do what you're trying to do, just change your div to something like this:

<div class="dwarfer" style="height: 1.2em;line-height:1.2em;">Whoa?</div>

No javascript required. That div will always be just tall enough to hold one line of text -- when the font gets bigger, the div will get taller to accommodate it, and when the font gets smaller the div will get smaller.

I think that's what you're looking for.


A little additional information about the em unit:

You can use em pretty much anywhere that you would use px. That includes the font-size rule. So:

<div style="font-size: 2em;">Twice as big</div>

The text contained in that div would be twice as big as the text of its parent.

Lee
That's great info, Lee. I'll try to adjust according to your specifications. What I'm trying to create, though, is not something that forces the font size to go up. I'm trying to increase div size based on the user-selected/browser-set font size. I hope you understand?
Fohsap
The first example I provided shows what you're looking for. I edited my answer to improve clarity.
Lee
Follow-up question: I would still like to constrain the number of words and possibly even the number of <div>'s that are printed in accordance with the browser's font size settings. If there's any way to pick up the font size the user has set, please include it in your answer. Thank you very much for the information. It's extremely useful for this case.
Fohsap
There's no way that I'm aware of to get the actual font-size that the user has set. But if you have a div, whose height is set to `1em`, then you use javascript to get the height of that div, then you'll have an pixel amount that is, in some way, proportional to the font-size (don't know if that will be useful to you or not though). Of course, if you're just looking for a way to respect the user's default font settings -- the key is to specify your font-sizes using `em`s, rather than px - if you do this everywhere, then all your page's text will be sized relative to the user's defaults.
Lee
@Fohsap - After reading your comments on @sheeks06's post above, I'm hesitant to chime in, lest I be called uncreative... but consider this: If you have "30 short divs stacked in a page", size those divs in css using `em` units (as @sheeks06 and I have suggested). When the page loads, have some javascript that sums the height of all the divs. Based on the total height, just hide some number of divs. This *is* the strategy of [Progressive Enhancement](http://en.wikipedia.org/wiki/Progressive_enhancement). It *does* accommodate "font-only scaling" (like ctl+mouse_wheel). It works. Try it.
Lee
Good point. I shouldn't have said that. Anyway, I think that your answer is very creative. I'll try it. Thank you.
Fohsap