简体   繁体   English

:nth-child 定位嵌套的 div

[英]:nth-child to target nested divs

I'm having trouble finding a way to do this.我很难找到一种方法来做到这一点。 This is my structure:这是我的结构:

<div class="flex_container">
    <div class="flex_item_9">
        <div class="flex_label">First Label</div>
        <div class="flex_data">Some Data</div>
    </div>
    <div class="flex_item_9">
        <div class="flex_label">Second Label</div>
        <div class="flex_data">Some Data</div>
    </div>
    <div class="flex_item_9">
        <div class="flex_label">Third Label</div>
        <div class="flex_data">Some Data</div>
    </div>
</div>

This div structure gets repeated in a PHP While Loop as it is pulled from a database.这个 div 结构在 PHP While 循环中被重复,因为它是从数据库中提取的。 On the desktop, I'd like to hide the flex_label div after the first one is displayed.在桌面上,我想在第一个显示后隐藏 flex_label div。 I only want to show the labels once for each while loop.我只想为每个 while 循环显示一次标签。 I figured :nth-child would work, so I tried the following in my CSS.我认为 :nth-child 会起作用,所以我在我的 CSS 中尝试了以下内容。

div.flex_container .flex_label:nth-child(2) {
      display: none;
}

But, had no results.但是,没有结果。 Nothing changed in the browser.浏览器中没有任何变化。 All flex_labels were displayed.显示了所有 flex_labels。

Is this something that can be handled in CSS, or will i need to incorporate something into my while loop to add additional classes to each DIV block?这是可以在 CSS 中处理的东西,还是我需要将某些东西合并到我的 while 循环中以向每个 DIV 块添加额外的类?

EDIT编辑

If there were 3 items to display:如果要显示 3 个项目:

 <div class="flex_container"> <div class="flex_item_9"> <div class="flex_label">SHOW LABEL</div> <div class="flex_data">Some Data</div> </div> <div class="flex_item_9"> <div class="flex_label">SHOW LABEL</div> <div class="flex_data">Some Data</div> </div> <div class="flex_item_9"> <div class="flex_label">SHOW LABEL</div> <div class="flex_data">Some Data</div> </div> </div> <div class="flex_container"> <div class="flex_item_9"> <div class="flex_label">* HIDE LABEL</div> <div class="flex_data">Some Data</div> </div> <div class="flex_item_9"> <div class="flex_label">* HIDE LABEL</div> <div class="flex_data">Some Data</div> </div> <div class="flex_item_9"> <div class="flex_label">* HIDE LABEL</div> <div class="flex_data">Some Data</div> </div> </div> <div class="flex_container"> <div class="flex_item_9"> <div class="flex_label">* HIDE LABEL</div> <div class="flex_data">Some Data</div> </div> <div class="flex_item_9"> <div class="flex_label">* HIDE LABEL</div> <div class="flex_data">Some Data</div> </div> <div class="flex_item_9"> <div class="flex_label">* HIDE LABEL</div> <div class="flex_data">Some Data</div> </div> </div>

My goal is to get this to display like a spreadsheet on a desktop.我的目标是让它像桌面上的电子表格一样显示。 Single row of labels, and multiple rows of data.单行标签和多行数据。 Mobile display will be different, but already working.移动显示会有所不同,但已经可以使用了。

The following selectors should work:以下选择器应该可以工作:

/* flex_label inside flex_container that is 2nd (or 3rd or 4th or ...) child */
.flex_container:nth-child(n + 2) .flex_label {}


/* flex_label inside flex_container that follows a flex_container */
.flex_container ~ .flex_container .flex_label {}

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

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