Css 当第一个子项在div中时,如何选择它

Css 当第一个子项在div中时,如何选择它,css,css-selectors,Css,Css Selectors,这是我的代码,我将选择第一个 这样好吗?它在文本菜单中选择图像后的每个跨度 .textTextmenu img + span {} 这里是所有让你明白的 1) .textTextmenu span:first-of-type { color:red } 2) .textTextmenu :nth-child(1){border:1px solid red;} 3) .textTextmenu span:nth-child(2){color:red;} 你为什么不能

这是我的代码,我将选择第一个


这样好吗?它在文本菜单中选择图像后的每个跨度

.textTextmenu    img + span {} 

这里是所有让你明白的

1)    .textTextmenu span:first-of-type { color:red } 
2)    .textTextmenu :nth-child(1){border:1px solid red;}
3)    .textTextmenu span:nth-child(2){color:red;}

你为什么不能使用第一种字体

注意:与许多CSS伪选择器一样,第一个of-type只是第n个of-type1的别名,正如第一个child是第n个child1的别名一样。.text菜单的of是一个img

你可能真的想要调查,或者,例如

在这个特定示例中,其他可行的方法包括或类似于:

.textTextmenu span {
    /* Style the first span */
}
.textTextmenu span + span {
    /* Style the next span */
}


要选择图像,请执行以下操作,而不是:

.text菜单span:第一个子项{}

.text菜单img:first child{}

这里有一把小提琴在起作用:

我有点困惑你想要什么,但是。。。 对吧?

这将选择div内的第一个img标记

.textTextmenu img + span{
  border:2px solid red;
 /*How to select First child? / this way is wronge and image is not selected*/
}

查看演示

哦,很抱歉之前没有看到图像。如果您需要支持旧浏览器ie8及更低版本,这很好。如果你在其他回答中支持的比你能使用的更高的n个孩子谢谢,这是真的,有没有办法使用第一个孩子?谢谢,这个答案真的很满意。我很困惑,你的HTML说你需要选择跨度,但是你的帖子和CSS说图像没有被选择。这是哪一个?对不起,我错了,现在让它成为真实的第一个相同类型的子元素是span,img是单个元素!好的,我取消删除了我的答案您已将代码更改为正确,使答案无效,那么问题是什么?谢谢,您能告诉我如何选择第一个孩子,即第一个孩子的跨度谢谢,您能告诉我如何选择第一个孩子,即第一个孩子的跨度。@Kermani您不能选择第一个孩子的跨度,因为它不是其父代的第一个孩子,即第一个孩子的跨度。如果需要跨度,请使用第n个子项或第n个类型。这些是你唯一的选择。你应该已经从15分钟前的问题中知道了这一点。。。
.textTextmenu span:first-of-type {
  ...styles go here... 

}
.textTextmenu span:first-of-type {}
.textTextmenu :nth-child(2) {}
.textTextmenu span {
    /* Style the first span */
}
.textTextmenu span + span {
    /* Style the next span */
}
.textTextmenu span {
    /* Style the first span */
}
.textTextmenu span:last-child {
    /* Style the next span */
}
.textTextmenu img:first-child{
  border:2px solid red;
 /*How to select First child? / this way is wronge and image is not selected*/
}
.textTextmenu img:first-child{
  border:2px solid red;
}
.textTextmenu img + span{
  border:2px solid red;
 /*How to select First child? / this way is wronge and image is not selected*/
}