0

I need to apply css to a parent and 3 nested deep children, this works:

.the-parent,  
.the-parent > div,
.the-parent > div > div,
.the-parent > div > div > div{
    display: flex
}

Is this the only way to accomplish this?

I was thinking of simply:

.the-parent div{
    display: flex
}

But that will select ALL children deep, and I only need 3 children deep.

I wonder if there is better syntax such as:

.the-parent >(3) div{
    display: flex
}

I hope it make sense, thank you

0

4 Answers 4

0

Unfortunately, CSS does not have a built-in way to select elements up to a specific depth. Your method is the way to achieve this using pure CSS.

However, if you're looking for a more concise solution, you can use CSS preprocessors like SASS or LESS to generate the nested selectors programmatically. Here's how you can simplify the process using SASS:

.the-parent {
  @for $i from 1 through 3 {
    & > #{repeat('div > ', $i - 1)}div {
      display: flex
    }
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

This is great. Love it. Thank you pal
0

You can use two selectors like below:

.parent {
  border: 1px solid blue;
  padding: 10px;
}
.parent * {
  padding: 10px;
  min-height: 10px;
  border: 1px solid;
}

/* set to all div */
.parent div {
  border-color: red;
}
/* reset after x depth */
.parent > * > * > * > * div {
  border-color: initial;
}
<div class="parent">
  <div>
    <div>
      <div>
        <div>
          <div>
            <div>
              <div>
                <div>
                  <div>
                    <div>
                      <div></div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

Comments

0

With nesting css it looks quite compact:

.parent {
  padding: 10px;
  background: yellow;
}
.parent div {
  padding: 10px;
  background: yellow;
}

.parent{
  &, &>div, &>div>div, &>div>div>div{
    background: blue;
  }
}
<div class="parent">
  <div>
    <div>
      <div>
        <div>
          <div>
            <div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

Comments

-1

If you are trying to do a css only solution, your provided method is probably the best approach to it, even when kind of ugly. What you could do instead is giving each div you want to address a specific class name and then run the normal css selection. Something like this:

<div class="parent">
  <div class="child1">
    <div class="child2">
      <div class="child3"></div>
    </div>
  </div>
</div>

with the selection:

.parent,
.child1,
.child2,
.child3 {
  display: flex;
}

But I am afraid that this solution is not much compact or "prettier" than before. Just maybe a bit more readable for humans.

One last thing that comes to mind is the :nth-child() functionality. Maybe you can find a way to do what you want to do with an architecture more like in the provided example.

Good luck!

1 Comment

Thank you for the insights. I will give a try to :nth-child(), thank again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.