Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to position absolute inside a div?

I'm having a strange problem positioning a set of divs inside another div. I think it will be best to describe it with an image:

image

Inside the black (#box) div there are two divs (.a, .b) that have to positioned in the same place. What I'm trying to achieve is pictured in the first image, second one is the effect I get. It looks like if the divs were floated without clearing or something, which is obviously not the case. Any ideas would be welcome!

Here's the code for this sample:

CSS:

#box {     background-color: #000;     position: relative;     padding: 10px;     width: 220px; }  .a {     width: 210px;     position: absolute;     top: 10px;     left: 10px;     background-color: #fff;     padding: 5px; }  .b {     width: 210px;     position: absolute;     top: 10px;     left: 10px;     background-color: red;     padding: 5px; }  #after {     background-color: yellow;     padding: 10px;     width: 220px; } 

HTML:

    <div id="box">         <div class="a">Lorem</div>         <div class="b">Lorem</div>     </div>      <div id="after">Hello world</div> 
like image 489
Justine Avatar asked Jul 16 '09 13:07

Justine


People also ask

How do you set an absolute position in a div?

you have to set #header to either absolute or relative positioning to keep the contents of its children inside it. Show activity on this post. Select css for your out-most div and set its position relative, then set all the positions for all your divs.

How do you position an absolute element?

Absolute An element with position: absolute is removed from the normal document flow. It is positioned automatically to the starting point (top-left corner) of its parent element. If it doesn't have any parent elements, then the initial document <html> will be its parent.

Can a div be position absolute and relative?

If you position it as absolute , then you're positioning it relative to its closest ancestor which is fixed or relative ... Clearly, nothing can be absolute and relative at the same time. Either you want the element to position itself based on another ancestor's position or based on the page flow.

How do I center an absolute div in CSS?

If you want to center something horizontally in CSS you can do it just by, using the text-align: center; (when working with inline elements) or margin: 0 auto; (when working with block element).


1 Answers

  1. First all block level elements are postioned static to the 'document'. The default positioning for all elements is position: static, which means the element is not positioned and occurs where it normally would in the document. Normally you wouldn't specify this unless you needed to override a positioning that had been previously set.
  2. Relative position: If you specify position: relative, then you can use top or bottom, and left or right to move the element relative to where it would normally occur in the document.
  3. When you specify position: absolute, the element is removed from the document and placed exactly where you tell it to go.

So in regard to your question you should position the containing block relative, i.e:

#parent {   position: relative; } 

And the child element you should position absolute to the parent element like this:

#child {   position: absolute; } 

See: Learn CSS Positioning in Ten Steps

like image 110
Jimmy Obonyo Abor Avatar answered Oct 21 '22 07:10

Jimmy Obonyo Abor