0

An outline of the scenario is that I have a div of class some-class which will either have 1 or 2 child divs. Whenever it has 1 I want that child div (div.some-class > div) to have width: 100%;, but whenever it has 2 I want them (div.some-class > div:nth-of-type(1), div.some-class > div:nth-of-type(2)) to have respective widths of 60% and 40%. I can't modify the HTML because it's generated by a content management system.

Is there a CSS hack that could give me this behavior?

2
  • Nope, you are dealing with dynamic behavior here. Best thing is to create separate rules in CSS and simply apply them through JavaScript Commented Dec 19, 2014 at 18:24
  • 1
    @beautifulcoder Dynamic behavior doesn't require javascript; responsive design is often done through CSS alone. Commented Dec 19, 2014 at 18:34

1 Answer 1

3

Sure, use display: table and the adjacent sibling selector.

body > div {
    display: table;
    width: 600px;
}

div > div {
    background: blue;
    height: 100px;
    display: table-cell;
}

div > div + div {
    background: red;
    width: 40%;
}
<div>
    <div></div>
</div>

<hr>

<div>
    <div></div>
    <div></div>
</div>

Sign up to request clarification or add additional context in comments.

Comments

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.