【发布时间】:2014-10-22 19:22:46
【问题描述】:
我正在尝试使用 :nth-child 伪类为每个侧边栏小部件标题提供唯一的背景颜色和标题文本旁边的图标。
目前,它为所有 3 个侧边栏小部件标题显示 :nth-child(1) 的背景颜色和背景图像,而不是 Twitter 的 :nth-child(1) :nth-child( 2) 用于投票, :nth-child(3) 用于 Instagram。
.main .sidebar .widgettitle:nth-child(1) {
background-color: #25abdc !important;
color: #ffffff;
background-image: url('/wp-content/uploads/2014/08/twitter-icon.png');
background-repeat: no-repeat !important;
background-position: 15% !important;
}
.main .sidebar .widgettitle:nth-child(2) {
background-color: #f18484 !important;
color: #FFFFFF;
background-image: url('/wp-content/uploads/2014/08/poll-icon.png');
background-repeat: no-repeat !important;
background-position: 15% !important;
}
.main .sidebar .widgettitle:nth-child(3) {
background-color: #25abdc !important;
color: #ffffff !important;
background-image: url('/wp-content/uploads/2014/08/instagram-icon.png') ;
background-repeat: no-repeat !important;
background-position: 15% !important;
}
【问题讨论】:
-
因为它是
nth-child而不是nth-of-class。 CSS 伪类(例如:nth-child、:nth-of-type、...)匹配父级子树中的第 n 个 child 或 元素类型。他们不会查看类列表。 -
请阅读How to create a Minimal, Complete, and Verifiable example。在这种情况下,除了发布所有相关代码之外,还要添加一个演示问题的小提琴。 . . .哦,是的,在询问之前研究这个网站是必不可少的。祝你好运!
-
nth-child和nth-of-type都适用于具有相同父级的元素并且仅适用于元素......而不是类。 -
@HashemQolami - 我尝试了 :nth-of-class,但没有奏效,并从所有样式中删除了样式。
标签: css wordpress widget css-selectors