我可以让 CSS :after 伪元素在元素外部附加内容吗?

发布于 2024-10-22 23:40:22 字数 733 浏览 7 评论 0原文

我想使用相邻链接之间的 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:

home » about us » history » this page

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

°如果伤别离去 2024-10-29 23:40:22

规范 说:

正如其名称所示,:before 和 :after 伪元素指定元素的文档树内容之前和之后的内容位置。

注意这句话末尾的关键短语,它指的是内部内容(或内部文本)。因此,伪元素被插入到指定元素的内容末尾的开头。因此,正确的双角引号将插入到超链接的文本之后,而不是超链接之后

这是具有两个伪元素的此类链接的 DOM 元素树的可视化表示:

a
  a:before
  content
  a:after

我猜想解决此行为的一种方法是将每个链接包装在 span 中,然后将样式应用于 nav#breadcrumb-trail span:after,但这会导致不必要的标记......但无论如何都值得一试。

The spec says:

As their names indicate, the :before and :after pseudo-elements specify the location of content before and after an element's document tree content.

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:

a
  a:before
  content
  a:after

I guess one workaround to this behavior, is to wrap each link in a span then apply styles to nav#breadcrumb-trail span:after, but that'd result in unnecessary markup... worth a shot in any regard though.

桃扇骨 2024-10-29 23:40:22

通常,无论如何,您都将这些菜单编码为有序列表,因此这样做是有意义的:

#breadcrumb-trail ol { 
    list-style: none; 
    margin: 0;
    padding: 0; 
}

#breadcrumb-trail li { 
    display: inline; 
}

#breadcrumb-trail li:after { 
    content: ' » '; 
}

#breadcrumb-trail li:last-child:after { 
    content: none; 
}
<nav id="breadcrumb-trail">
    <h1>My Amazing Breadcrumb Menu</h1>
    <ol>
        <li><a href="">about</a></li>
        <li><a href="">fos</a></li>
    </ol>
</nav>

Normally you code these menus as ordered lists anyway, so it makes sense to do something like this instead:

#breadcrumb-trail ol { 
    list-style: none; 
    margin: 0;
    padding: 0; 
}

#breadcrumb-trail li { 
    display: inline; 
}

#breadcrumb-trail li:after { 
    content: ' » '; 
}

#breadcrumb-trail li:last-child:after { 
    content: none; 
}
<nav id="breadcrumb-trail">
    <h1>My Amazing Breadcrumb Menu</h1>
    <ol>
        <li><a href="">about</a></li>
        <li><a href="">fos</a></li>
    </ol>
</nav>

过潦 2024-10-29 23:40:22

鉴于 CSS 的灵活性和令人惊叹的 ,看起来非常可行。在当前的 Chrome、FF 和 IE 中尝试过此操作,它按预期完成工作,无需添加额外的标记:

#box {
  width: 100px;
  height: 100px;
  background: #eee;
  position: relative;
}

#box:after {
  content: '...appended text outside box';
  position: absolute;
  margin-left: 100%;
  left: 0;
  width: 200px;
}
<div id="box">
  Some Box
</div>

小警告:绝对定位的“元素包含在框的尺寸中,因此浮动或在其旁边对齐的元素无法动态尊重伪元素内容的宽度。

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:

#box {
  width: 100px;
  height: 100px;
  background: #eee;
  position: relative;
}

#box:after {
  content: '...appended text outside box';
  position: absolute;
  margin-left: 100%;
  left: 0;
  width: 200px;
}
<div id="box">
  Some Box
</div>

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文