2013-01-03 64 views
2

我知道类似的问题已经问了一堆,但我无法找到我的具体答案。如果我有一堆CSS选择器非常相似,但只是不同,我可以如何嵌套或分组它们。了解CSS分组/嵌套

这是我正在做的。

#cell-left { 
    background-color:#DDDDDD; 
    border:2px solid; 
    height:400px; 
    margin:20px 10px 0px 32px; 
    padding: 40px 15px 15px 15px; 
    text-align:center; 
} 
#cell-center { 
    background-color:#DDDDDD; 
    border:2px solid; 
    height:400px; 
    margin:20px 10px 0px 10px; 
    padding: 40px 15px 15px 15px; 
    text-align:center; 
} 
#cell-right { 
    background-color:#DDDDDD; 
    border:2px solid; 
    height:400px; 
    margin:20px 32px 0px 20px; 
    padding: 40px 15px 15px 15px; 
    text-align:center; 
} 
#row { 
    width:100%; 
    margin-top:0px; 
} 

正如你可以看到所有的单元格对彼此非常普遍,他们只是略有不同的利润率。我知道有一种方法可以将所有单元格完全相同,然后在CSS中添加一个.right,.center和.left,只需保留边缘,并减少大量代码。

在此先感谢您的答案。

+1

也将它有为这些使用类选择器更好,为什么? – recneps

回答

2

创建一个包含重复属性并将其添加到每个DOM元素的单元类。

CSS

.cell{ 
    background-color:#DDDDDD; 
    border:2px solid; 
    height:400px; 
    padding: 40px 15px 15px 15px; 
    text-align:center; 
} 

#cell-left{ 
    margin:20px 10px 0px 32px; 
} 

#cell-center { 
    margin:20px 10px 0px 10px; 
} 

#cell-right { 
    margin:20px 32px 0px 20px; 
} 

HTML

<div id="cell-left" class="cell">Something</div> 
<div id="cell-right" class="cell">Something</div> 
<div id="cell-center" class="cell">Something</div> 

例子:http://jsfiddle.net/hKLMj/

1

我建议你使用类,它是集团唯一的出路,你想要什么。顺便说一句,我想你会有几个相同的单元格和几行的页面,对不对?我认为你知道,但在以下情况下:在同一个页面上使用两次相同的id是非常糟糕的做法,类应该在同一页面上多次使用,并且id应该是。只能使用一次/页

你可以这样做:

.cell { 
    background-color:#DDDDDD; 
    border:2px solid; 
    height:400px; 
    padding: 40px 15px 15px 15px; 
    text-align:center; 
} 
.cell-left { 
    margin:20px 10px 0px 32px; 
} 
.cell-center { 
    margin:20px 10px 0px 10px; 
} 
.cell-right { 
    margin:20px 32px 0px 20px; 
} 
.row { 
width:100%; 
margin-top:0px; 
} 

而且在你HTML这样做

<table border="1"> 
    <tr class="row"> 
     <td class="cell cell-left">row 1, cell 1</td> 
     <td class="cell cell-right">row 1, cell 2</td> 
    </tr> 
    <tr class="row"> 
     <td class="cell cell-left">row 2, cell 1</td> 
     <td class="cell cell-right">row 2, cell 2</td> 
    </tr> 
</table> 

你可以连续类

+1

这正是我所需要的,不,我不知道使用相同的ID会不好,但现在我知道了。 – recneps

+1

很高兴知道它有帮助;)(顺便说一句,如果你的问题得到解答,不要忘记验证答案) – Kalzem

+1

你可以完全摆脱类 - 它们在这里没有必要,只是污染了HTML标记 –

2

如果你只有一个左,中间和右边的单元格,那么你很好,ID

否则使用,因为ID -s 必须是唯一的,你不能有两个elementswith页面上的相同ID

这里是你的CSS的缩短版本。由于你的cell-s是某种孩子(让我们假设他们是<td>-<tr>.row类) - 你不必使用类。这将使您的标记清洁:

tr.row td { 
    background-color: #ddd; 
    border: 2px solid; 
    height: 400px; 
    padding: 40px 15px 15px 15px; 
    text-align:center; 
    margin:20px 10px 0px 10px; 
} 

还,如果有一个row其中的3个,你不必使用类定义左,右:

tr.row td:first-child { 
    margin:20px 10px 0px 32px; /* left cell */ 
} 

tr.row td:last-child { 
    margin:20px 32px 0px 20px; /* right cell */ 
} 

而且HTML

<tr class="row"> 
    <td> left cell </td> 
    <td> center cell </td> 
    <td> right cell </td> 
</tr> 

DEMO

0

CSS不支持分组或嵌套。现在,一些CSS 编译器(例如LESS,SASS)支持这样的概念,以及“混入”等巧妙的技巧..

。但是,请记住,所有匹配 CSS规则。选择器的特定性和声明的顺序只决定哪些值覆盖其他值。所以,没有类或其他大喊大叫:

#cell-left, #cell-center, #cell-right, #row { 
    background-color:#DDDDDD; 
    border:2px solid; 
    height:400px; 
    padding: 40px 15px 15px 15px; 
} 
#cell-left, #cell-center, #cell-right { 
    text-align: center; 
} 

#cell-left { 
    margin:20px 10px 0px 32px; 
} 
#cell-center { 
    margin:20px 10px 0px 10px; 
} 
#cell-right { 
    margin:20px 32px 0px 20px; 
} 
#row { 
    width:100%; 
    margin-top:0px;  
} 

现在,虽然它可能是有益的使用类或清洁的标记 - 特别是如果这样做会导致更容易维护 - 上面将获得相同的结果作为初始岗位。因人而异。

+0

@ZoltanToth良好的捕获 - 更新。 :) – 2013-01-03 01:19:52

0

这将是更语义正确使用double class selectors

.cell { 
    background-color: #DDDDDD; 
    border: 2px solid; 
    height: 400px; 
    padding: 40px 15px 15px 15px; 
    text-align: center; 
} 
.cell.left { 
    margin: 20px 10px 0px 32px; 
} 
.cell.center { 
    margin: 20px 10px 0px 10px; 
} 
.cell.right { 
    margin: 20px 32px 0px 20px; 
} 

这样,你就可以这样写:

<div class="cell left">Something</div> 
<div class="cell center">Something</div> 
<div class="cell right">Something</div> 

甚至:

<div class="left cell">Something</div> 
<div class="center cell">Something</div> 
<div class="right cell">Something</div>