html - CSS :first-letter not working

标签 html css

我正在尝试将 CSS 样式应用到一些从 Microsoft Word 文档生成的 HTML 片段。 Word 生成的 HTML 相当糟糕,并且包含许多内联样式。它是这样的:

<html>
    <head></head>
    <body>
        <center>
            <p class=MsoNormal><b style='mso-bidi-font-weight:normal'><span
               style='font-size:12.0pt;line-height:115%;font-family:"Times New Roman"'>Title text goes here<o:p></o:p></span></b></p>

            <p class=MsoNormal style='margin-left:18.0pt;line-height:150%'><span
                style='font-size:12.0pt;line-height:150%;font-family:"Times New Roman"'>Content text goes here.<o:p></o:p></span></p>
    </body>
</html>

...非常简单,我想为标题部分的第一个字母设置样式。它只需要更大并且使用不同的字体。为此,我尝试使用 :first-letter 选择器,类似于:

p b span:first-letter {
    font-size: 500px !important;
}

但是好像不行。这是一个证明这一点的 fiddle :

http://jsfiddle.net/KvGr2/

任何想法有什么问题/如何正确设置标题部分的第一个字母的样式?我可以对标记进行微小的更改(例如在事物周围添加包装器 div),但并非没有困难。

最佳答案

::first-letter 不适用于 inline 元素,例如 span::first-letter 适用于 block 元素,例如段落、表格标题、表格单元格、列表项或具有 display 属性的元素设置为 inline-block

因此,最好将 ::first-letter 应用于 p 而不是 span

p::first-letter {font-size: 500px;}

或者如果你想在 span 中使用 ::first-letter 选择器,那么这样写:

p b span::first-letter {font-size: 500px !important;}
span {display:block}

MDN provides the rationale对于这种不明显的行为:

The ::first-letter CSS pseudo-element selects 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.

...

A first line has only meaning in a block-container box, therefore the ::first-letter pseudo-element has only an effect on elements with a display value of block, inline-block, table-cell, list-item or table-caption. In all other cases, ::first-letter has no effect.

另一个奇怪的情况(除了不处理内嵌项)是如果你使用 :before :first-letter 将适用于前面不是实际的第一个字母,请参阅 codepen

例子

引用资料

https://developer.mozilla.org/en-US/docs/Web/CSS/::first-letter http://reference.sitepoint.com/css/pseudoelement-firstletter

关于html - CSS :first-letter not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7631722/

相关文章:

javascript - 尝试为列表/表格创建重命名功能

PHP MySqli 多查询 & while 在一页中 - 有助于简化代码

css - 影响桌面的媒体查询

javascript - 使用 HTML/Javascript 在鼠标越过某些选定文本时更改光标类型

html - 为什么这种转换在 Chrome 和 Firefox 上的表现不同?

html - 在悬停时删除 div 的覆盖

javascript - 使用 Python 进行 AJAX 调用

css - 如何摆脱 JavaFX Textview 周围的白色边框

javascript - 如何为 JavaScript 选项卡创建 HTML 书签 anchor

html - 设计一个打印 html 标志和标签的 java web 应用程序