当前位置:  开发笔记 > 前端 > 正文

仅当第一个元素有其他兄弟元素时才对其进行定位

如何解决《仅当第一个元素有其他兄弟元素时才对其进行定位》经验,为你挑选了1个好方法。

在没有添加任何类(或触摸HTML)的情况下,如果该div中有第二个元素,有没有办法将div中的第一个元素作为目标?以下是我的HTML:

I WANT TO APPLY CSS TO THIS
But only if there is 2nd .content element in the same parent element
Don't apply here
Don't apply here
I WANT TO APPLY CSS TO THIS
But only if there is 2nd .content element in the same parent element

为了获得更好的图片,可以使用一些上下文...我想把.content它放在中心,如果它是内部唯一的.content .grid.但如果有两个.contentdiv,那么我希望它们彼此相邻.

注意:

我已经知道我可以瞄准第二个.content

.grid .content:nth-child(2) { ... }

MortenMoulde.. 9

.grid > .content:first-child:not(:only-child) {
    color: blue;
}
I WANT TO APPLY CSS TO THIS
But only if there is 2nd .content element in the same parent element
Don't apply here
Don't apply here
I WANT TO APPLY CSS TO THIS
But only if there is 2nd .content element in the same parent element

:first-child:not(:only-child) 将是你的选择器.



1> MortenMoulde..:

.grid > .content:first-child:not(:only-child) {
    color: blue;
}
I WANT TO APPLY CSS TO THIS
But only if there is 2nd .content element in the same parent element
Don't apply here
Don't apply here
I WANT TO APPLY CSS TO THIS
But only if there is 2nd .content element in the same parent element
推荐阅读
mobiledu2402851377
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有