html - Div 标签之间的交替背景颜色

标签 html css

我有一个由 div 组成的表格结构。

我只想改变 divLabel 的背景颜色。

表格的每个 divrow 都有一个 divlabel 和一个 divdata 元素。

这是我到目前为止尝试过的:-

http://jsfiddle.net/o5dv4qkc/

不幸的是,标签的颜色不会交替,而是只显示奇数行提到的相同颜色:-

.About_RowLabel:nth-of-type(odd) {
    background-color: #DEF3CA;
}

谁能告诉我为什么这不适用于我的 div 元素?

最佳答案

您没有正确选择 child :向上移动一级到他们的 parent ,然后从那里开始,例如 .someParentClass:nth-of-type(even) .child{}: demo

.About_Row {
  height: 2em;
  width: 500px;
  /*float:left;*/
}
.About_RowLabel {
  width: 98px;
  color: black;
  float: left;
  background-color: #71cd7b;
}
.About_RowData {
  width: 200px;
  float: left;
  margin-left: 20px;
}
.About_Row:nth-of-type(odd) .About_RowLabel {
  background-color: #DEF3CA;
}
.serv_resize {
  border: 1px solid #E2E2E2;
  color: #444;
  width: 425px;
  float: left;
  margin: 10px 10px 10px 0;
  padding: 5px;
  height: 239px;
}
<div id="you" class="serv_resize">
  <div class="About_Row">
    <div class="About_RowLabel">
      BirthDate:
    </div>
    <div class="About_RowData">
      <asp:Label ID="lblDoB" runat="server" Text="DoB"></asp:Label>
    </div>
  </div>
  <div class="About_Row">
    <div class="About_RowLabel">
      As of:
    </div>
    <div class="About_RowData">
      <asp:Label ID="lblAsOf" runat="server" Text="Dt"></asp:Label>
    </div>
  </div>
  <div class="About_Row">
    <div class="About_RowLabel">
      Phone:
    </div>
    <div class="About_RowData">
      <asp:Label ID="lblPhone" runat="server" Text="Ph"></asp:Label>
    </div>
  </div>
</div>

关于html - Div 标签之间的交替背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27412947/

相关文章:

javascript - 从日期选择器中获取日期并将其显示在 div 中

javascript - 按钮不应该调用 php 函数

jquery - 在一个div中添加一个div

jquery - 淡出、应用 css、淡入 - JQUERY

html - 元素后面的下拉菜单

javascript - 在元素列表中,将编号索引放在 id 中还是使用索引选择器?

html - 将年份值限制为只有 4 位数字

css - 无法使用 BS4 找到 css 选择器

javascript - 在 IE 6 中如何让 float 页脚粘在视口(viewport)底部?

html - 与文本左侧的图像链接