Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I automatically stack divs vertically inside a parent?

Here's what I am trying to accomplish...

  1. "parent" has position:relative
  2. "div 1-3" have position:absolute

However, whenever I do this, I find myself having to assign specific "top" values in my CSS. So div 1 might be top:50px, div 2 would be top:150px, and div 3 would be top:225px;

Is there a way to make sure the divs continue to stack inside the parent without assigning top values and/or absolute positioning?

like image 716
Fingeldor Avatar asked Oct 10 '13 00:10

Fingeldor


People also ask

How do I make two divs stack on top of each other?

Position the outer div however you want, then position the inner divs using absolute. They'll all stack up.

How do I arrange two divs vertically?

To align two <div> elements vertically in Bootstrap 3, you can try using the CSS Flexible Box Layout. In the example below, we display the needed row as a flex container box with the CSS display property and then, align the flex-items (columns) vertically with the align-items property.


2 Answers

A div should already display as a block and take up a full "row". Here is some HTML and CSS to give an example, compare it to your code:

http://jsfiddle.net/mWcWV/

<div id="parent">      <div class="child">Foo</div>     <div class="child">Bar</div>     <div class="child">Baz</div>  </div> 
like image 56
Joseph Avatar answered Sep 22 '22 19:09

Joseph


Should be straight:

HTML:

<div class="container">     <div class="red"></div>     <div class="blue"></div>     <div class="green"></div> </div> 

CSS:

.container {     position: relative;     width: 500px;     height: 500px;     background-color: #ffbf00; } .red {     background-color: #f00;     width: 200px;     height: 150px;     margin: 5px auto; } .blue {      background-color: #0f0;     width: 200px;     height: 150px;     margin: 5px auto; } .green {     background-color: #00f;     width: 200px;     height: 150px;     margin: 5px auto; } 

Check this fiddle.

like image 34
TheVillageIdiot Avatar answered Sep 21 '22 19:09

TheVillageIdiot