html - 某些样式不适用于 :after

标签 html css

你好

我尝试对使用 :after 添加的内容进行样式设置,color: ; 被覆盖,但 text-decoration: ; 没有'不工作。

code.html

<html>
<head>
    <style>
        .separate-hyphen *:not(:last-child):after {
            content: " -\00a0";
            text-decoration: none;
            color: red;
        }
    </style>
</head>
<body>
    <div class="separate-hyphen">
        <a href="link1.extension">Link 1</a>
        <a href="link2.extension">Link 2</a>
    </div>
</body>
</html>

结果 (这是一张图像)

enter image description here

最佳答案

因为伪元素 :after 渲染在元素的内部,而不是外部,所以它的样式当然不会影响外部容器样式:

+--------------------+
|         +--------+ |
| Content | :after | |
|         +--------+ |
+--------------------+

你需要找到另一种方法。也许可以通过绝对定位在视觉上将 :after 移动到其容器之外:

.separate-hyphen *:not(:last-child) {
  margin-right: 10px;
}
.separate-hyphen *:not(:last-child):after {
  content: " -\00a0";
  text-decoration: none;
  color: red;
  position: absolute;
  padding: 0 5px;
}
<div class="separate-hyphen">
  <a href="link1.extension">Link 1</a>
  <a href="link2.extension">Link 2</a>
</div>

关于html - 某些样式不适用于 :after,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32797304/

相关文章:

javascript - 在同一行制作表单元素

html - 如何使文本与图标对齐?

html - CSS Div 对齐兼容性

javascript - 如果条件不可行

html - 是什么让 <button> 元素上的文本垂直居中?

java - 将 mp3 从 java/c++ 服务器流式传输到 html5 的最佳方式?

javascript - 如何根据列条件设置默认颜色

css - 响应式按钮设计

javascript - FlexBox 对齐(拉伸(stretch))最后一个元素

html - 下拉菜单显示菜单和子菜单之间的差距?