2017-06-28 73 views
1

我有以下代码:内容的CSS后,如何将文本分成两种风格

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

它检查按钮有唱段标签,如果它是“第一”,它会追加“一些文本”。我想用颜色来设计'某些文字',但'一些'有黑色,'文字'有红色。这是可能的和如何?

+0

[CSS内容属性的可能重复:是否有可能插入HTML代替文本?](https://stackoverflow.com/questions/4505093/css-content-property-is-it-possible-to-insert-html-inst ead-of-text) –

+0

@MikeMcCaughan号 –

+3

@PraveenKumar谢谢你为什么这不是重复的理由论证。 –

回答

0

尽管您不能在伪元素中使用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>

+1

你实际上可以在'content'属性中使用HTML,...这里是我的答案:https://stackoverflow.com/a/36073310/2827823 – LGSon

+0

不错,但我已经'之前'占领image&:: before {content:url('../../ assets/icon.png'); },所以我尝试了这个工作,但它取代了我的形象。你能告诉我如何把图像和文字的这一部分,我会接受你的答案。谢谢! – Vikast

+0

@Vikast哈!就像我已经告诉过你的,你只能有两个伪元素。就你的情况而言,我想,你必须按照LGSon所使用的方法回答他的问题。你接电话。我想这是更好的方式。 –