1

I have 7 divs in a row with all very little content. I want to have the first 3 one a line then the next 3, and so on.

<div class="parent">
 <div class="child-a">abc</div>
 <div class="child-b">def</div>
 <div class="child-c">ghi</div>
 <div class="child-d">jkl</div>
 <div class="child-e">mno</div>
 <div class="child-f">pqr</div>
 <div class="child-g">stu</div>
</div>

So how can I make this work?

For the first line I have

.child-a, .child-b, .child-c {
 padding: 0 2% 0 0;
 width:100%
 display: inline;
 float: left;
 }

What would the css be for child-c, child-d, child-e so that they would be displayed below child-a, child-b, child-c rather than on the same line?

My complete code: http://jsfiddle.net/winchendonsprings/UfswL/11/

asked Jul 20, 2011 at 4:28
2
  • Give certain width to parent class. Commented Jul 20, 2011 at 4:33
  • Note that width:100% display: inline; cancel each other - you're missing a ;, so neither work. Fortunately, float: left; hides this issue. Think about what you're doing - do you really need float here? Can you give the children and the parent widths, as Thilakar suggested? (for example 50px/160px will keep 3 in a line) Commented Jul 20, 2011 at 4:50

3 Answers 3

6

A better way to go would be something like this.

.parent div { float: left; }
.parent div:nth-child(3n + 1) { clear: left; }

Every element after the third will go to next line. Demo

This is do what you asked the first 3 one a line then the next 3, and so on. efficiently despites some browsers compatiblity issues.

answered Jul 20, 2011 at 4:46
Sign up to request clarification or add additional context in comments.

1 Comment

Maybe you can give me a hand with the exact code. jsfiddle.net/winchendonsprings/b3uwy
1

You forgot the dots for the classes:

.child-a, .child-b, .child-c,
.child-d, .child-e, .child-f,
.child-g, .child-h, .child-i {
 padding: 0 2% 0 0;
 width:100%
 display: inline;
 float: left;
}
.child-d { clear: left; }
.child-g { clear: left; }

This will make it so that 'd' and 'g' can't have a floated element to their left.

answered Jul 20, 2011 at 4:38

1 Comment

This worked great too. Basically the same as the answer above. Thanks!
1

What I am getting is you want 3 divs one top row, then 3 divs on second row, then the last two divs on last row. If that is correct, you dont need 7 ID's. Instead use one class.

.child_div {
 width: 33%;
 height: 50px; /* for beautification */
 float: left;
}

All child divs must use this class. The divs will automatically align themselves side by side.

A couple of issues that may arise:

  • The parent div will have 0 height because all of it's child divs are floated. To fix this, you can set the parent div's height (does not respond to content length), or set overflow:hidden; or overflow:auto; ("pulls" the child content inside)
  • Different browsers may react a little differently. For example for IE, you need width:32.5%; for example because it works a bit different. Hope it helps.
allicarn
2,9292 gold badges30 silver badges47 bronze badges
answered Jul 20, 2011 at 4:48

1 Comment

This worked too. But divs with more content to them didn't look quite as good. Maybe if all the divs had a lot or a little content this would be best. But the divs varied so it kind of looked strange with some lines wrapping and some not. Thanks

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.