2010,正心,修身,养性。
焚我残躯,毁我意志,复我记忆。
子曰:“知之者不如好之者,好之者不如乐之者。”
子曰:“吾尝终日不食,终夜不寝,以思,无益,不如学也。”

快乐的生活,快乐的分享。

first-letter在各浏览器下的区别

first-letter 是设置对象内的第一个字符的样式,基本语法是:Selector : first-letter { sRules } 。但是今天在使用的时候发现了一个有趣的问题。先看看下边的例子吧:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<style type="text/css">
p:first-letter { color:red; font-weight:600; }
li:first-letter { color:red; }
</style>
</head>
<body>
<p>我才12岁</p>
<p>“我才12岁”</p>
<ul>
    <li>我才12岁</li>
    <li>“我才12岁”</li>
    <li>我才12岁</li>
    <li>我才12岁</li>
</ul>
</body>
</html>

按照我的想法,应该是第一个字母的样式显示,可是测试的结果,却跟我的不太一样,先看看ie6、ie7的显示:

ie6、ie7

Opera下的显示:

ie8、Safari、Firefox、Chrome

再来看看ie8、Safari、Firefox、Chrome下的显示:

Opera

在ie6、ie7和Opera下,对于p来说,只认第一个字符,不管是标点还是文字,而其它浏览器缺会将标点和第一个字符认作一起的,而对于列表来说,ie6和ie7会把前边的列表的符号和第一个字符一起改变样式,而其他浏览器只会认第一个字符。那么它们到底谁对呢,其实不用想,肯定应该是ie6、ie7的错,要不然,ie8也不会向其它浏览器趋同哦,不过我们还是需要证据的。

查阅下W3C,会看到:

The :first-letter pseudo-element must select the first letter of the first line of a block, if it is not preceded by any other content (such as images or inline tables) on its line. The :first-letter pseudo-element may be used for “initial caps” and “drop caps”, which are common typographical effects. This type of initial letter is similar to an inline-level element if its ‘float’ property is ‘none’, otherwise it is similar to a floated element.

The ‘:first-letter’ also applies if the first letter is in fact a digit, e.g., the “6″ in “67 million dollars is a lot of money.”

The :first-letter pseudo-element applies to block, list-item, table-cell, table-caption and inline-block elements.

参考资料:http://www.w3.org/TR/CSS21/selector.html#first-letter

注:假如首字出现的标点为英文标点的话,那么所有浏览器下的表现是一样的,也就是第三幅图里边的,浏览器会将标点和第一个字符认为是首个字符。

标签:

这篇文章发布于 2009年01月28日,星期三,12:12,归类于 CSS。 您可以跟踪这篇文章的评论通过 RSS 2.0 feed。 您可以留下评论,或者从您的站点trackback

一条评论

雁过留声

Rss
渴望孩子的纯真、诚实、可爱、无忧无虑。。。。。。

这位姐姐,你好,你的浏览器版本太低了,为了避免您中毒,请您升级您的浏览器!