Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

css positioning child divs

Tags:

html

css

Is it possible to do this using css (and how)?

------------------------------------------
|  ----------   ----------   ----------  |
|  | Child 1 |  | Child 3 |  | Child 5 | |
|  ----------   ----------   ----------  |
|  ----------   ----------   ----------  |
|  | Child 2 |  | Child 4 |  | Child 6 | |
|  ----------   ----------   ----------  |
|  ----------                            |
|  | Child 7 |                           |
|  ----------                            |
|  ----------                            |
|  | Child 8 |                           |
|  ----------                            |
------------------------------------------

using the following :

<div class="parent">
   <div class="child">Child1</div>
   <div class="child">Child2</div>
   <div class="child">Child3</div>
   <div class="child">Child4</div>
   <div class="child">Child5</div>
   <div class="child">Child6</div>
   <div class="child">Child7</div>
   <div class="child">Child8</div>
</div>

.parent {width:100%}
.parent div {width:100px; margin:2px;}

Edit: Maybe I didn't explain clearly what i want...so

  1. There can be more than 8 children.....but always an even number (10,12,14 etc)

  2. A div with an even number must be always under his preceding odd div (2 under 1, 4 under 3....8 under 7, 10 under 9)

  3. When parent's width is not enough to hold pairs of childer: it expands its height (like starts a new line)

Edit2: The correct result is here: http://jsfiddle.net/97ZpN/3/

but in this solution i had to put every pair of divs into a sub-container. Is it posiible to do it with the original html?

like image 841
Derick Avatar asked Nov 19 '13 10:11

Derick


People also ask

How do you position a child relative to a parent?

The one key thing to remember when trying to position a child div relative to it's parent is that the child should be given the CSS property position:absolute; and the parent set to either position:absolute; or position:relative;.

How do I fix position relative to parent?

To position an element "fixed" relative to a parent element, you want position:absolute on the child element, and any position mode other than the default or static on your parent element. This will position childDiv element 50 pixels left and 20 pixels down relative to parentDiv's position.

How do I move a div to the bottom of a parent?

Set the position of div at the bottom of its container can be done using bottom, and position property. Set position value to absolute and bottom value to zero to placed a div at the bottom of container.

Can a child div be bigger than parent?

The child DIV needs to be the same width as the browser viewport. The child DIV must stay as a child of the parent div.


1 Answers

hope it will help you

    .parent {width:100%}
 .parent div {float:left; margin:2px;width:30%}
 .parent div.child:nth-child(7),.parent div.child:nth-child(8){
  float:none;
  clear:both;
}

demo: http://jsfiddle.net/97ZpN/

Explanation:

.parent div {float:left; margin:2px;width:30%} 

This line in the CSS will make all the child elements to float towards the left. So the elements will automatically stack one after other.

 .parent div.child:nth-child(7),.parent div.child:nth-child(8){
      float:none;
      clear:both;
    }

The clear:both does the trick here. clear:both means there cannot be any elements on the left or right side of the referenced element.

like image 57
Venu immadi Avatar answered Oct 14 '22 17:10

Venu immadi