5

我正在为我的网站制作一组按钮,我需要一些专业的见解。

为了减少 CSS 膨胀,我想将我的按钮子类化为不同的颜色,例如.button.blue 。

以后会不会出现以下问题?(假设我不只创建一个 .blue 类)我是否必须改用 .button.button-blue 之类的东西?

.button {
  display:inline-block;
  padding: 9px 18px;
  margin: 20px;
  text-decoration: none;
  font-family: Arial, Helvetica, sans-serif;
  font-size: 12px;
  font-weight: bold;
  text-align: center;
  background: #FFE150;
}
.button.blue {
  background: #49b8e7;
  border:1px solid #54abcf;
  border-bottom:1px solid #398fb4;
  color:#FFF
  text-shadow: 0 1px 0 rgba(255,255,255, 0.5);
}
.header{
  height: 50px;
}
.header.blue {
  background: blue;
  color: #fff;
}
4

4 回答 4

8

假设您希望它们像这样工作,那么您在多类中所拥有的东西将可以正常工作:

<div class="button blue">
Will use .button and .button.blue
</div>

<div class="button">
Will only use .button
</div>

<div class="header blue">
Will  use .header and .header.blue
</div>

<div class="header">
Will only use .header
</div>

<div class="blue">
Will use neither of the .blue declarations because it doesn't contain header or button.
</div>
于 2012-02-09T15:00:21.213 回答
1

像 .button.blue 这样的选择器实际上选择了一个同时具有“blue”和“button”作为类的元素,而不是一个名为 .button.blue 的类。请参阅http://www.w3.org/TR/CSS21/selector.html#class-html

您可以使用您列出的 .button.blue 样式规则,但您需要重新排列 HTML 以便拥有类似<button type="button" class="button blue"/>. 但是,您实际上并不需要一个按钮类,因为它是一个按钮(或<input type="submit">等)足以在您的选择器中使用。您可以编写一个简单的 CSS 规则button.blue, input[type=submit].blue{}

于 2012-02-09T14:51:00.600 回答
0

好像button.blue就够了。

两者之间的唯一区别是您是否使用<button class="button blue">, 或<button class="button button-blue">

你甚至不需要用蓝色复制这幅画......你可以这样做:

.button
{
// button style
}

.header
{
// header style
}

.blue
{
   background: blue; 
   color: #fff; 
}

当然,如果您将蓝色类添加到它们中的每一个。(<div class="header blue"><button class="button blue">)

于 2012-02-09T14:55:12.273 回答
0

组合应用您想要主题的颜色的类。

HTML:

<input type="text" class="text-field-required default" .../>
<select class="autocomplete-drop-down blue">...</select>
<a href="#" class="button-link green" .../>

CSS:

.text-field-required {
    //component css theme without colors
}
.default {
    //default color css theme for any component
}
.blue {
    //blue css theme for any component
}
.green {
    //green css theme for any component
}
于 2012-02-09T15:08:34.330 回答