Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to place a div below another div?

Tags:

html

css

I have a #slider div with an image. After that, I have a #content div which has text. I have tried position:relative so I think it should come after the previous div, I mean #slider but here it is not coming that way.

What is the problem here? How to overcome it?

HTML

<div id="slider">     <img src="http://oi43.tinypic.com/25k319l.jpg"/> </div> <div id="content">     <div id="text">         sample text     </div> </div> 

CSS

#slider {     position:absolute;     left:0;     height:400px; } #slider img {     width:100%; }  #content {     position:relative; }  #content #text {     position:relative;     width:950px;     height:215px;     color:red; } 

JSFIDDLE

like image 841
rram Avatar asked Oct 12 '13 19:10

rram


People also ask

How do I position a div under one another?

If you want the two div s to be displayed one above the other, the simplest answer is to remove the float: left; from the css declaration, as this causes them to collapse to the size of their contents (or the css defined size), and, well float up against each other.

How do you make a div under another div in CSS?

You can use the CSS position property in combination with the z-index property to overlay an individual div over another div element. The z-index property determines the stacking order for positioned elements (i.e. elements whose position value is one of absolute , fixed , or relative ).

How do I place a div under absolute div?

You would need to either use relative positioning on both, or absolute positioning for both and set their specific top and left values. Alternatively you could set a top margin on footer that makes it drop enough so it is positioned below the container. You also need to look at your css.


1 Answers

You have set #slider as absolute, which means that it "is positioned relative to the nearest positioned ancestor" (confusing, right?). Meanwhile, #content div is placed relative, which means "relative to its normal position". So the position of the 2 divs is not related.

You can read about CSS positioning here

If you set both to relative, the divs will be one after the other, as shown here:

#slider {     position:relative;     left:0;     height:400px;      border-style:solid;     border-width:5px; } #slider img {     width:100%; }  #content {     position:relative; }  #content #text {     position:relative;     width:950px;     height:215px;     color:red; } 

http://jsfiddle.net/uorgj4e1/

like image 153
Alain1405 Avatar answered Sep 21 '22 23:09

Alain1405