【发布时间】:2017-07-19 03:00:55
【问题描述】:
我正在尝试根据页面上的时间顺序设置一组随机图像的样式,仅使用 CSS 而不使用任何 html 类 (old demo)。
由于该部分中的所有图像都是链接,因此我将 <a> ...</a> 包裹在 img 标签周围,但现在 css 选择已损坏!
我尝试了各种选择器,但都坏了(未选择,应用的背景颜色不正确):
sponsor img:nth-child(1)
sponsor img:nth-of-type(1)
sponsor a img:nth-child(1)
a img:nth-child(1)
JSFIDDLE Demo 损坏的时间选择器。我忽略了什么?谢谢!
【问题讨论】:
-
您只需将 img: 替换为 a:.. 并在 nth-child() 之后添加 img
-
img现在是您的主播的直接子代,而不是sponsor,只有一个子代,将每个主播视为img:nth-child(1)。所以如果n !== 1,img:nth-child(n)将不起作用。您需要做的是更改您的选择器,如@Garric15 在下面的答案中建议。
标签: html css layout css-selectors