html - 无论 sibling 之间的位置如何,我如何选择具有特定类的第一个元素?

标签 html css

<分区>

请检查以下代码。第一种样式完美应用,但“常规 1”的第二种样式不适用。我必须在“常规”类的第一个元素上应用蓝色。

.plan-list .basic:first-child {
    background: red;
  }
  .plan-list .regular:first-child {
    background: blue;
  }
<ul class="plan-list">
  <li class="plan basic">basic 1</li>
  <li class="plan regular">regular 1</li>
  <li class="plan regular">regular 2</li>
  <li class="plan basic">basic 2</li>
</ul>

最佳答案

应该是

<style>
  .plan-list .basic:first-child {
    background: red;
  }
  .plan-list .regular:nth-child(2) {
    background: blue;
  }
</style>

<ul class="plan-list">
  <li class="plan basic">basic 1</li>
  <li class="plan regular">regular 1</li>
  <li class="plan regular">regular 2</li>
  <li class="plan basic">basic 2</li>
</ul>

这是因为,虽然这个 li 是那个类(class)的第一个 child , 它实际上是其父项中的第二个子项

关于html - 无论 sibling 之间的位置如何,我如何选择具有特定类的第一个元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57077133/

上一篇:css - 移动菜单 - 汉堡包图标 - 'hover' 颜色不变, 'focus' 链接不起作用

下一篇:css - 如何实现精确的CSS渲染?

相关文章:

javascript - 如何在 wordpress 上移动 iframe?

CSS:是否可以将浏览器默认样式应用于按钮

javascript - 在元素离开鼠标指针后移除悬停效果

html - flex 方向 : column; not working in IE and Edge

html - CSS: float:left with a margin-right 不会将所有元素推开

css - 仅影响 parent

html - 网页末尾的空白,IE 和 Chrome 显示在不同的地方?

javascript - 带 + 和 - 符号的可折叠列表不起作用

html - 数据句 HTML/CSS 中的居中文本

html - 水平对齐 div 并完美适合父级的宽度