1

由于样式名称重复,是否有一种全局方法可以从 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;}
4

1 回答 1

0

您似乎有点名称空间的冲突,理想情况下,如果经过深思熟虑,您不应该遇到这种情况。要么更精确,或者更确切地说,不那么模糊。例如,您可以将元素包装在单独的 div 中,或者使用 css 链接。

CSS 有许多有用的片段,如果你遇到麻烦可以使用,例如>,当这样使用时div > p.myClass,只会适用于p's 的类myClass,即 a 的子级div。然后,您甚至可以使用:first-child, 选择器使其更具体,这意味着它仅适用于第一个p标签,其类为.myClass,它是 div 的子标签。div > p.myClass:first-child.

真的,如果类不需要样式属性,为什么要使用它?

于 2012-08-30T23:09:03.753 回答