【问题标题】:Content of after in css, how to split text into two stylescss中after的内容,如何将文本分成两种样式
【发布时间】:2017-06-28 22:30:44
【问题描述】:

我有以下代码:

.button { 
   &[aria-label*='first']::after { content: 'some text'; } 
}

它检查按钮是否有 aria-label,如果它是“第一个”,它将附加“一些文本”。我想用颜色设置“一些文本”的样式,但“一些”有黑色,“文本”有红色。这可能吗?如何实现?

【问题讨论】:

标签: css


【解决方案1】:

虽然您不能在伪元素中使用 HTML 内容,但作为替代方案,您可以在这种情况下同时使用 ::after::before。但请注意,只有这两个。所以,这就是你要走的路:

.button[aria-label*='first']::after {
  position: relative;
  content: 'some';
  left: 1em;
  color: #fcc;
}

.button[aria-label*='first']::before {
  position: relative;
  content: 'text';
  left: 6.5em;
  color: #ccf;
}
<div class="button" aria-label='first-button'>Hi</div>

更好的方法:

.button[aria-label*='first']::after {
  content: 'some';
  color: #f00;
}

.button[aria-label*='first']::before {
  content: 'text';
  color: #ccf;
}
<button class="button" aria-label='first-button'></button>

【讨论】:

  • 您实际上可以在 content 属性中使用 HTML,有点……这是我的答案:stackoverflow.com/a/36073310/2827823
  • 很好,但我已经用图像 &::before { content: url('../../assets/icon.png'); } ,所以我尝试了它,它正在工作,但它替换了我的图像。你能告诉我如何把图像和这部分文字放在一起,我会接受你的回答。谢谢!
  • @Vikast Ha!就像我已经告诉过你的那样,你只能有两个伪元素。在你的情况下,我想,你必须按照 LGSon 的回答来使用他的技巧。你接电话。我想这样更好。
猜你喜欢
  • 2014-05-07
  • 2013-12-10
  • 2011-11-11
  • 2019-06-04
  • 2012-11-20
  • 2013-03-26
  • 1970-01-01
  • 2019-05-02
  • 2016-09-01
相关资源
最近更新 更多