html - CSS::before 是 nullifying::first-letter

标签 html css

HTML代码:

<h1>Example title</h1>

CSS:

h1::first-letter{
    display:none;
}
h1::before{
    content: url(http://icons.iconarchive.com/icons/ariil/alphabet/32/Letter-E-icon.png);
}

我的问题是:为什么::before 会取消::first-letter 规则?这里发生了什么?如果::before 被删除,::first-letter 工作正常。

在不更改 html 的情况下,是否有任何替代方法可以针对此示例中的第一个字母?

http://jsfiddle.net/Borachio/kvaxmhth/

最佳答案

来自 the spec :

After the rule p::before {content: "Note: "}, the selector p::first-letter matches the "N" of "Note".

另请注意,display 属性在 :first-letter:first-line 伪元素上无效。同样,根据规范:

A future version of this specification may allow this pseudo-element to apply to more display types.

这是预期的行为。

解决方法:

HTML:

<div><h1>Example title</h1></div>

和 CSS:

h1{
    display: inline-block;
}
h1::first-letter{
    font-size: 0px;
}
div::before{
    content: url(http://icons.iconarchive.com/icons/ariil/alphabet/32/Letter-E-icon.png);
}

演示:http://jsfiddle.net/kvaxmhth/3/

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

相关文章:

html - Ipad show title-图片标签

javascript - 在 Hangman Game 中屏幕重置后显示我的获胜图像

javascript - css --> <table> --> 当表格数据包含多个元素时移除垂直空间

python - 如何使用 flask 和 html 返回在下拉列表中选择的值列表

html - Bootstrap CSS : div with image is overlapping div with text

jquery - 自定义 Bootstrap 标签文本位置

javascript - 如何在整页图像 slider 中提供箭头键事件

javascript - HTML/Javascript 函数对新手的帮助

html - Bootstrap 导航栏总是折叠的

javascript - Bootstrap 切换按钮主题、按钮文本和 div