简体   繁体   English

CSS伪类,以DIV中的第一个DIV .class为目标

[英]CSS psuedo classes, target first DIV .class within DIV

I thought I knew this. 我以为我知道这一点。 There's three div s (.service-box) inside one wrapper div (.p- div ) 一个包装div (.p- div )内有三个div (.service-box)

How do you target the first (or any.) of the three inner div s? 您如何定位三个内部div第一个(或任何一个)?

This works, shows the structure is correct, will remove all inner div s. 这项工作有效,表明结构正确,将删除所有内部div

div.p-div > div.service-box { display: none!important;}

I just can't target the first inner div . 我只是无法定位第一个内部div

I've tried first-child , nth-of-type(1) , nth-child(1) 我试过了first-childnth-of-type(1)nth-child(1)

I did notice nth-of-type(1n) removes all inner div s like the first CSS line. 我确实注意到nth-of-type(1n)删除了所有内部div如第一行CSS。 While nth-of-type(2n) removes all but the 2nd inner div . 虽然nth-of-type(2n)删除了第二个内部div Basically doing the opposite of what I would expect (apply display:none only to the 2nd div , leaving the others intact) 基本上与我期望的相反(将display:none仅应用于2 div ,将其他保持不变)

Gotta say I'm a little perplexed. 得说我有点困惑。 Could just edit the HTML directly but.I thought we were past that you know? 可以直接编辑HTML但我想我们已经过去了,您知道吗?

.p-div > .service-box:first-of-type {
    background-color: red;
}

Get a child of .p-div that has class .service-box and then select only the first child. 获取具有类.service-box .p-div的子级,然后仅选择第一个子级。

.p-div could be anything: some-selector > .service-box:first-of-type .p-div可以是任何东西: some-selector > .service-box:first-of-type

CodePen: http://codepen.io/theblindprophet/pen/JKpwLk CodePen: http ://codepen.io/theblindprophet/pen/JKpwLk

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM