html - 使用 CSS 为第 nth-child 添加伪类

标签 html css css-selectors pseudo-class

我想使用悬停使奇数 div 的底部边框颜色与 #75dcff 不同。但是,.card:hover div:nth-child(odd) 不起作用。我可以将伪类应用于第 n 个子元素吗?

.card {
  margin: 30px;
  padding: 20px 40px 40px;
  max-width: 500px;
  text-align: left;
  background: #fff;
  border-bottom: 4px solid #ccc;
  border-radius: 6px;
}

.card:hover {
  border-color: #75dcff;
}

.card:hover div:nth-child(odd) {
  border-color: #ff7c5e;
}
<div class="card"></div>
<div class="card"></div>
<div class="card"></div>

最佳答案

您的选择器不正确。 .card:hover div:nth-child(odd) 正在选择作为 .card 后代的奇数索引 div,但您的结构表明它们应该相同事物。调整您的选择器以匹配悬停时的奇数元素:

.card {
  margin: 30px;
  padding: 20px 40px 40px;
  max-width: 500px;
  text-align: left;
  background: #fff;
  border-bottom: 4px solid #ccc;
  border-radius: 6px;
}

.card:hover {
  border-color: #75dcff;
}

.card:nth-child(odd):hover {
  border-color: #ff7c5e;
}
<div class="card"></div>
<div class="card"></div>
<div class="card"></div>

关于html - 使用 CSS 为第 nth-child 添加伪类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48084421/

相关文章:

jquery - 使用 <ul><li> 而不是 <select> 进行导航/过滤

css - 无法使用 Tailwindcss 居中表单

html - 纯CSS : Select elements that have n or more siblings

jquery - :first-child only matches the first

PHP 相当于第 n 个 child

jquery - 在包装器中居中多个绝对定位的 div

jquery - FloatThead Table 上面有一个可折叠的 div

html - 如何显示不同的号码图像覆盖图像?

css - 第 n 个 child (奇数)不工作

css - 创建模式的布局和样式