2014-03-27 73 views
0

我有两个跨度为字体设置

<span class="class-1">Text1</span> 
<span class="class-1">Text2</span> 

而且.class-1有风格

.class-1 { 
    font-weight: bold; 
    color: red; 
} 

现在我需要从.Text-2删除font-weight: bold,是否可以不创建另一个CSS做类?

+0

font-bold:true is invalid css rules –

+0

'class-1'缺少一个前导'.'。什么颜色是'读'? – j08691

回答

1

Fiddle

<span class="class-1">Text1</span> 
<span class="class-1">Text2</span> 

和Class-1具有风格

.class-1 { 
     font-weight: bold; 
     color: red; 
    } 


.class-1 + .class-1{/*this will be applied on the secound span*/ 
     font-weight: normal; 
     color:green; 
    } 

这种替代:

.class-1 { 
     font-weight: bold; 
     color: red; 
    } 


.class-1:last-child{/*this will be applied on the secound span*/ 
     font-weight: normal; 
     color:green; 
    } 
/*or this*/ 
.class-1:nth-child(2){ 
     font-weight: normal; 
     color:green; 
    } 

内嵌CSS

<span class="class-1" style="font-weight: bold;color: red;">Text1</span> 
<span class="class-1" style="font-weight: normal;color:green;">Text2</span> 

Fiddle

,如果你有在HTML

之间

东西:

<span class="class-1">Text1</span> 
<p>Hello do you have time to talk about Css?</p> 
<span class="class-1">Text2</span> 

CSS:

.class-1 { 
     font-weight: bold; 
     color: red; 
    } 

.class-1:nth-of-type(2) { 
    font-weight: normal; 
    color: green; 
} 

Fiddle

+0

应该意识到所有这些选择器都属于http://caniuse.com/#feat=css-sel3。 –

1

假设你的代码看起来大致是这样的

<style> 
.class-1 { 
    font-weight: bold; 
    color: red; 
} 
</style> 
<span class="class-1">Text 1</span> 
<span class="class-1">Text 2</span> 

您可以添加的样式属性为文本2的跨度,如果你绝对不能创建一个类。改变它看起来像下面。

<span class="class-1" style="font-weight: normal;">Text 2</span> 
+0

谢谢,任何方式排除内联样式,因为这将被拒绝说不是一个好的做法 – Techonthenet

+0

我在其他线程中评论,但第n个孩子选择器属于http://caniuse.com/#feat=css-sel3所以这取决于你需要支持哪些浏览器。 –