由于第二个CSS文件包含一些重复的类而清除CSS样式

时间:2012-08-30 22:54:31

标签: css

由于重复的样式名称,是否有一种从div中清除CSS样式的全局方法?我们正在合并内容,并试图通过使用多个样式表来最小化工作。以下是我一直在进行的测试:http://jsfiddle.net/Codewalker/yU2sW/7/。但是,由于它是JSFiddle,而不是引用两个外部样式表,我将它们组合在CSS窗口中。请注意secondary_container div如何从其他容器继承蓝色文本。我还粘贴了以下代码:

<div class="container">
<h1>This is in an h1 tag.</h1>
<p>This is at the very top, outside the secondary_container stuff. All kinds of content     can go down here. All of this is within a paragraph tag. I'll write one more sentence then     copy all this over again. Sound good? Wait that's two more sentences. Actually four. Oh well. So much for that. Here goes a little copying and pasting. All kinds of content can go down here. All of this is within a paragraph tag. I'll write one more sentence then copy all this over again. Sound good? Wait that's two more sentences. Actually four. Oh well. So much for that. Here goes a little copying and pasting.</p>

<div id="secondary_container">
<div class="container">

<p>This is within the container div that's within the secondery_container div. All kinds of content can go down here. All of this is within a paragraph tag. I'll write one more sentence then copy all this over again. Sound good? Wait that's two more sentences. Actually four. Oh well. So much for that. Here goes a little copying and pasting. All kinds of content can go down here. All of this is within a paragraph tag. I'll write one more sentence then copy all this over again. Sound good? Wait that's two more sentences. Actually four. Oh well. So much for that. Here goes a little copying and pasting.</p>
</div>
</div>
</div>

这是CSS,它实际上是两个独立的外部样式表:

/* BEGINNING OF FIRST CSS FILE styler.css */

.container {
background-color: #00FF00;
height: auto;
width:960px;
margin: 0 auto;
line-height: 20px;
}

h1 { font-family:Arial, Helvetica, sans-serif; font-size:14px; color: #F00; text-transform:uppercase; }
p { font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#00F; 

}

/* BEGINNING OF SECOND CSS FILE secondary_container.css 
*/

#secondary_container .container h1 { font-family:Georgia, "Times New Roman", Times, serif; color: #0F0; }
#secondary_container .container p { font-style:italic; line-height:14px;}

1 个答案:

答案 0 :(得分:0)

你似乎有一点名称空间的冲突,理想情况下,如果你认为你应该不会遇到这种情况。要么更精确,要么更不含糊。例如,您可以将元素包装在单独的div中,或者使用css链接。

CSS有许多有用的片段,如果您遇到>之类的问题,可以使用它们,如果像div > p.myClass一样使用,它只适用于pmyClass的{​​{1}},div的孩子。然后,您甚至可以使用:first-child选择器使其更具体,这意味着它只会应用于第一个p标记,其类别为.myClass,这是一个孩子的DIV。 div > p.myClass:first-child

真的如果课程中不需要样式属性,为什么要使用它呢?