`
liudaoru
  • 浏览: 1582782 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

The CSS ex unit[z]

阅读更多

From: www.xs4all.nl/~sbpoley/webmatters/emex.html

Summary

I have a few times seen it suggested that the ex unit is useful for setting the widths of CSS boxes. However, having done a little experimenting with the em and ex units in CSS, I do not share that view.

In fact my conclusion is that the CSS ‘ex’ unit is practically useless. A unit based on a typical character width would indeed have been useful in getting CSS boxes to wrap at approximately the same point in the text, regardless of which font the reader is using. But the ex is defined as follows:

The ‘ex’ unit is defined by the font’s ‘x-height’. The x-height is so called because it is often equal to the height of the lowercase "x". However, an ‘ex’ is defined even for fonts that don’t contain an "x".

However, if one is defining the height of a box in terms of the font-size, one clearly wants to use the font-size itself – i.e. the em. While if one is defining the width of a box, it is not obvious why there is any advantage in using the x height. I have racked my brains on this one, and invited opinions on Usenet; I cannot think of any use for the ex unit. If you can, please contact me.

Was this something that went wrong during the creation of the CSS specification?

The rest of this article gives some background for this.

Examples – box sizes

Below are some boxes with the widths shown. Of the browsers I have tried, only Mozilla – and so presumably also other Gecko-based browsers, such as Netscape 6/7 – uses the ex units according to specification. Opera and Internet Explorer seem to assume 1 ex is 0.5 em and leave it at that. Actually half an em does correspond to a typographical unit, but another one: the en.

(Since the original version of this page it has been pointed out to me that the Opera / Internet Explorer behaviour is dictated by the limitations of the Windows operating system. Apparently the Gecko browsers do all their own character rendering, which goes some way to explain why they are so large.)

 

30 em Times New Roman

A sentence made up of lots of very short words to see how the wrap goes with some em and ex values.

 

30 em Arial

A sentence made up of lots of very short words to see how the wrap goes with some em and ex values.

 

60 ex Times New Roman

A sentence made up of lots of very short words to see how the wrap goes with some em and ex values.

 

60 ex Arial

A sentence made up of lots of very short words to see how the wrap goes with some em and ex values.

 

60 ex Verdana

A sentence made up of lots of very short words to see how the wrap goes with some em and ex values.

 

60 ex Bernhard Fashion BT

A sentence made up of lots of very short words to see how the wrap goes with some em and ex values.

 

For those people using other configurations: the image below shows how the above boxes look in Mozilla on Windows. You can see how the Bernhard Fashion font gets a very narrow box because it has very low lower-case letters, though it is actually not much narrower than Times New Roman. It is hard to see how this is useful.

[Appearance of text blocks in Mozilla]

Now, it is possible to create examples where the ex unit does give a more consistent block size (at any rate in Mozilla). This is the case where the fonts concerned have a similar aspect ratio for their lower-case letters (ratio of height to width) but very different aspect values (ratio of em to ex). This is however not generally true.

Font sizes

A helpful e-mail from a typesetter pointed out that it can be useful in typesetting to set fonts according to their ex size: fonts with the same ex size tend to be about equally legible. I suspect this may have been why the ex unit was originally introduced: the intention was to be able to specify something like { font-x-size: 1ex } This could have been very useful: if the author’s selected font had a significantly different aspect value from the reader’s selected default, it would set a font size of approximately equal readability. However font-x-size never materialised.

Stan Brown did point out a conceivable use for the ex. If one wanted to use a graphic to represent a character which has no defined character entity reference, and the character was required to be the size of a lower-case letter, then in principle one could set the height of the image to 1ex. In practice however browser resizing of images is not good enough to make that effective, and in any case one would have to search hard to find a character for which this was necessary.

It has also been suggested that the ex might be useful for margin/padding settings. I guess this is just about possible, at least for top and bottom, though again whether it gives better results than the em probably depends heavily on which fonts are in use.

Winding up

In summary then, although the ex may just be marginally useful in one or two circumstances, your web pages will not suffer if you ignore its existence completely.

Incidentally I’ve found an error in the CSS specification:

“In bicameral scripts, the subjective apparent size and legibility of a font are less dependent on their ‘font-size’ value than on the value of their ‘x-height’, or, more usefully, on the ratio of these two values, called the aspect value (font size divided by x-height). The higher the aspect value, the more likely it is that a font at smaller sizes will be legible.”

Given the last quoted sentence (and also the sentences that follow it in the specification) it is clear that the aspect value is not as stated, but is actually x-height divided by font-size.

Finally, I’ll mention an interesting article Typographical measurement systems on ems, ens and related matters. <!---->

分享到:
评论

相关推荐

    CSS-Unit-Converter:在CSS单位之间转换值的简单工具

    CSS单位转换器 使用CSS单位转换器在几乎每个CSS 单位之间转换值。 请通过进行实际检查。 建造 确保已安装 安装所有依赖项 npm install 建立一切 npm run build 观看所有内容(并根据更改进行重建) npm run ...

    The CSS Anthology: 101 Essential Tips, Tricks & Hacks, 3rd Edition

    The CSS Anthology: 101 Essential Tips, Tricks & Hacks is a compilation of best practice solutions to the most challenging CSS problems. The third edition of this best-selling book, published in full ...

    The.Zen.of.CSS.Design.-.Visual.Enlightenment.for.the.Web.part1

    The.Zen.of.CSS.Design.-.Visual.Enlightenment.for.the.Web.part1

    CSS The Definitive Guide 4th Edition

    This revised edition provides a comprehensive guide to CSS implementation, along with a thorough review of the latest CSS specifications. CSS is a constantly evolving language for describing the ...

    The Zen of CSS Design (3-3)

    The Zen of CSS Design ,绝佳的CSS设计图书

    Z01866Web前端开发技术 HTML、CSS、JavaScript 第2版.pdf

    Z01866Web前端开发技术 HTML、CSS、JavaScript 第2版

    Ultimate CSS reference

    the css reference. The reference for the css development

    CSS The Definitive Guide, 4th Editio

    《CSS The Definitive Guide, 4th Edition》是一本权威的CSS(Cascading Style Sheets)指南,专为深入理解和应用CSS技术的开发者而设计。这本书由业界专家Eric A. Meyer撰写,是CSS学习者的必备参考书。第四版更是...

    CSS The Definitive Guide, 4th Edition(中文机翻)

    《CSS The Definitive Guide, 4th Edition》是一本权威的CSS(Cascading Style Sheets)指南,中文机器翻译版提供了对原英文版的辅助理解。这本书详细讲解了CSS的各个方面,对于深入理解和掌握CSS技术具有极大的价值...

    相册css源码.7z相册css源码.7z

    【标题】"相册css源码.7z相册css源码.7z" 提供的是一个关于创建相册的CSS源代码集合。这个压缩包很可能是为了帮助开发者或者设计师构建美观、功能丰富的图片展示相册,利用CSS来实现各种布局、动画效果以及交互性...

    css课程.7z

    《CSS课程深度解析》 CSS,全称Cascading Style Sheets,即层叠样式表,是网页设计中不可或缺的一部分。本课程围绕“css课程”展开,通过一系列基础案例,旨在帮助学习者深入理解和掌握CSS的核心概念和技术应用。 ...

    react-css-themr, CSS模块易于主题化和组合.zip

    react-css-themr, CSS模块易于主题化和组合 响应 CSS ThemrCSS模块易于主题化和组合。$ npm install --save react-css-themr注意:对文档的反馈和贡献非常感谢。 为什么?在使用模块样式时,通常从同一

    css framework 漂亮的CSS

    Add the following four links to the &lt;head&gt; of your page:: &lt;link rel="stylesheet" href="../css/screen.css" type="text/css" media="screen, projection"&gt; &lt;link rel="stylesheet" href="../css/print.css" ...

    CSS CSS CSS

    CSS CSS 资料套装 CSS CSS 资料套装 CSS CSS 资料套装 CSS CSS 资料套装 CSS CSS 资料套装 CSS CSS 资料套装 CSS CSS 资料套装

    Enduring CSS

    The problems of CSS at scale - specificity, the cascade and styles intrinsically tied to element structure. The shortfalls of conventional approaches to scaling CSS. The ECSS methodology and the ...

    Reset_CSS css样式重置文件

    CSS重设就是由于各种浏览器解释CSS样式的初始值有所不同,导致设计师在没有定义某个CSS属性时,不同的浏览器会按照自己的默认值来为没有定义的样式赋值,所以我们要先定义好一些CSS样式,来让所有浏览器都按照同样的...

    CSS Quick Syntax Reference

    The CSS Quick Syntax Reference is a 150-page syntax reference to the Cascading Style Sheet specification and style sheet language. It presents the essentials of CSS in a well-organized format that can...

Global site tag (gtag.js) - Google Analytics