dongzhang7157 2010-09-14 02:09
浏览 80
已采纳

如何设置if /或条件,以便我的设计根据用户浏览器的字体大小而变化?

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">
<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{' . "
" . 'height:' . $divheight; . "
" . '}'
  //end styles
?>

Code comments are GREATLY appreciated! Thank you!

  • 写回答

2条回答 默认 最新

  • doutuo1908 2010-09-14 02:17
    关注

    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)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug