我可以让 CSS :after 伪元素在元素外部附加内容吗?
我想使用相邻链接之间的 HTML »
实体来格式化链接的面包屑路径,因此它看起来像这样:
我在 CSS 中添加了一条规则:
nav#breadcrumb-trail a:after {
content: " » ";
}
但这是将实体添加到链接内部,而不是外部 - 即我'我得到这个:
我是否误解了 CSS :after
伪元素的行为?文档似乎暗示它将指定的内容添加到指定的元素之后,而不是将其添加到元素容器的内部。有什么想法吗?
I want to format a breadcrumb trail of links using an HTML »
entity between adjacent links, so it looks like this:
I've added a rule to my CSS:
nav#breadcrumb-trail a:after {
content: " » ";
}
but this is adding the entity INSIDE the link, instead of outside it - i.e. I'm getting this:
home » about us » history » this page
Am I misunderstanding the behaviour of the CSS :after
pseudo-element? Documentation seems to imply it adds the specified content after the specified element, rather than prepending it to the inside of the element's container. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
规范 说:
注意这句话末尾的关键短语,它指的是内部内容(或内部文本)。因此,伪元素被插入到指定元素的内容末尾的开头。因此,正确的双角引号将插入到超链接的文本之后,而不是超链接之后。
这是具有两个伪元素的此类链接的 DOM 元素树的可视化表示:
我猜想解决此行为的一种方法是将每个链接包装在
span
中,然后将样式应用于nav#breadcrumb-trail span:after
,但这会导致不必要的标记......但无论如何都值得一试。The spec says:
Note the key phrase at the end of this sentence, which refers to the inner content (or inner text). So, the pseudo-elements are inserted into the beginning of the end within the specified elements' content. Therefore the right double angled quote is being inserted after the text of your hyperlinks, rather than after the hyperlinks.
Here's a visual representation of the tree of the DOM element for one such link with both pseudo-elements:
I guess one workaround to this behavior, is to wrap each link in a
span
then apply styles tonav#breadcrumb-trail span:after
, but that'd result in unnecessary markup... worth a shot in any regard though.通常,无论如何,您都将这些菜单编码为有序列表,因此这样做是有意义的:
Normally you code these menus as ordered lists anyway, so it makes sense to do something like this instead:
鉴于 CSS 的灵活性和令人惊叹的 ,看起来非常可行。在当前的 Chrome、FF 和 IE 中尝试过此操作,它按预期完成工作,无需添加额外的标记:
小警告:绝对定位的“元素包含在框的尺寸中,因此浮动或在其旁边对齐的元素无法动态尊重伪元素内容的宽度。
Given the flexibility and awesomeness of CSS, it looks pretty much do-able. Tried this in current Chrome, FF, and IE and it does the job just as expected, without the need to add addtional markup:
Small Caveat: Absolutely positioned `elements are included in the boxes' dimensions, therefore elements floating or aligned next to it are unable to dynamically respect the width of the pseudo elements' content.