CSS: absolutely position div above parent div

SR7

I want to position a DIV absolutely, at top: 0. The yellow div (#menu) in this diagram is the one in question.

I have HTML:

<html>
    <head>
        <link rel="stylesheet" type="text/css" href="style.css">
    </head>
    <body>
        <div id="container_head">
            &nbsp;
        </div>
        <div id="container_body">
            <div id="main">
                <div id="menu">
                    &nbsp;
                </div>
                <div id="content">
                    &nbsp;
                </div>
            </div>
            <div class="clear"></div>
        </div>
        <div id="container_foot">
            &nbsp;
        </div>
    </body>
</html>

and CSS:

#container_head {
    width: 100%;
    background-color: #00F;
    z-index: 10;
}
#container_body {
    width: 100%;
}
#container_foot {
    width: 100%;
    background-color: #F00;
}
#main {
    width: 960px;
    margin: 0 auto;
    position: relative;
}
#menu {
    background-color: rgba(255,255,0,0.5);
    width: 170px;
    padding: 10px;
    float: left;
    position: absolute;
    top: 0;
    z-index: 100;
}
#content {
    background-color: #0F0  ;
    width: 750px;
    padding: 10px;
    float: right;
}
.clear {clear: both;}

However, the yellow div does not appear at y position 0. Here is the fiddle.

Update

I appreciate the answers. I am still in a quandary. #container_head is not going to be the same height for every user. It is a responsive slider. Also, I need #menu to be on the left side of a region the same shape as #main, so it slots in next to #content.

Abhitalks

After having read this thread, your comments and the updated question, it seems that you are not inclined to change your markup and layout. With this in mind let's fix only the styles to suit your existing markup.

Before you go on, have a look at this demo:

Demo: http://jsfiddle.net/abhitalks/2fYGj/6/

There are a few problems that we need to understand and we'll fix them one by one.

(1.) You have percent widths on containers and pixel widths on children. In your question you state that the containers esp. header needs be responsive. I would suggest using same units for the entire layout i.e. using percent widths for all your layout components.

(2.) You have paddings and margins applied to some elements which will cause problems on widths if you don't have proper box-sizing model to start with.

Let's fix these two problems:

We add a minimal reset to your CSS:

* {
    margin: 0; padding: 0; box-sizing: border-box;
}

We now apply percent widths to all elements:

#main {
    width: 100%;
    ...
}
#menu {
    width: 10%; /* within 100% of parent i.e. main */
    ...
}
#content {
    width: 70%; /* within 100% of parent i.e. main */
    ...
}

Now on to other problems:

(3.) You have percent widths on your container_head, container_body and container_foot. 100% width of what? It is of the parent. So we have to apply width to the html,body as well, to be on safe side.

We fix that by adding this to your CSS:

html, body {
    width: 100%;
}

(4.) You have your #menu absolutely positioned. Now, absolute position means taking the element out of flow and making it positioned relatively to its nearest relatively positioned parent. If no such parent exists, then it becomes relative to body. In your case, its immediate parent i.e. #main is relatively positioned, so the menu tried to position itself relative to #main. But, that's not what you want. You want it to be relative to #container_head. But #container_head is not in its parent hierarchy. (And you don't want to change your markup) otherwise adding a wrapper would have solved this problem.

So we fix this by removing position: relative on #main:

#main {
    ...
    /* position: relative;  <-- Remove positioning from main */
}

This will force the #menu to be positioned relative to page. This will introduce another problem, which is that if you have a margin-top applied to #container_head, then the menu will pull up. To mitigate that, you will then have to apply a top on #menu which is equal to the margin-top of #container_head. (This can be best understood by this demo: http://jsfiddle.net/abhitalks/LbRKb/)

(5.) You need your #menu to line-up with the #content. And #menu is absolutely positioned. Absolute position means it is out of its flow and hence your floats on menu and content are now meaningless.

We fix it by assigning a percent margin to both menu and content:

#menu {
    width: 10%; 
    top: 0; left: 10%; /* whatever indent you want from left */
    /* float: left <-- not required now */
    ...
}
#content {
    width: 70%;
    margin-right: 10% /* right margin equal to left pos of menu */
    margin-left: 20%; /* left margin equal to left pos of menu + menu width */
    /* float: right <-- not required now */
    ...
}

(6.) Because your menu is absolutely positioned now, its height may exceed that of #main if there is little or no content in #content. In that scenario it will overlap main into the footer and even beyond.

We fix this by assigning a min-height to both #menu and #main and making sure that main's height is more than that of menu:

#main {
    min-height: 200px; /* must be more than that of menu */
    ...
}
#menu {
    min-height: 100px;
    ...
}

.

There you go. Now you have a layout that exactly resembles the diagram picture you posted.

Updated (based on Op's comments):

In order to constrain the menu and content size using max-width, the above method will break the gap between menu and content as mentioned by the Op in the comments below.

To fix this turned out to be easy. The trick would be to use right instead of left on the #menu.

Demo 3: http://jsfiddle.net/abhitalks/2fYGj/17/ Demo 3 (Full-Screen): http://jsfiddle.net/abhitalks/2fYGj/17/embedded/result/

Hope that helps.

.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Expand and position absolutely positioned div to relatively positioned parent

From Dev

position image absolutely to relative div

From Dev

Absolutely positioned div is not recognizing parent div

From Dev

DIV CSS Layout - fixed position expand parent

From Dev

CSS: position div at the bottom of parent element?

From Dev

Position div with relative position to the bottom inside absolutely positioned div

From Dev

Make absolutely positioned div the height of the parent container

From Dev

Absolutely positioned div overflowing the boundary of relative parent

From Dev

Position div under parent div

From Dev

Place div directly above parent div

From Dev

Centering text on absolutely positioned parent div's top-left corner using only CSS

From Dev

How to make parent fullscreen div to grow with absolutely positioned child using CSS and jeet?

From Java

How to center div vertically inside of absolutely positioned parent div

From Dev

Fit absolutely positioned DIV *completely* in relatively positioned parent DIV?

From Dev

Fit absolutely positioned DIV *completely* in relatively positioned parent DIV?

From Dev

take div out of parent and add to parent above

From Dev

Adding Padding to absolutely positioned div within parent expanding outside parent

From Dev

CSS position fixed div still displayed in respect to parent element

From Dev

How to Move Parent Div Position using css on hover

From Dev

Moving child div above another child div in each parent div

From Dev

Position Angular $modalInstance above specific div

From Dev

Can't position a div above other docs?

From Dev

position a div at bottom css

From Dev

CSS position, div inside div specific position

From Dev

CSS position, div inside div specific position

From Dev

Making overflowing div position to bottom parent div

From Dev

div Element changes position (margin) of parent div

From Dev

Making overflowing div position to bottom parent div

From Dev

Vertically position DIV in the center of parent DIV?

Related Related

  1. 1

    Expand and position absolutely positioned div to relatively positioned parent

  2. 2

    position image absolutely to relative div

  3. 3

    Absolutely positioned div is not recognizing parent div

  4. 4

    DIV CSS Layout - fixed position expand parent

  5. 5

    CSS: position div at the bottom of parent element?

  6. 6

    Position div with relative position to the bottom inside absolutely positioned div

  7. 7

    Make absolutely positioned div the height of the parent container

  8. 8

    Absolutely positioned div overflowing the boundary of relative parent

  9. 9

    Position div under parent div

  10. 10

    Place div directly above parent div

  11. 11

    Centering text on absolutely positioned parent div's top-left corner using only CSS

  12. 12

    How to make parent fullscreen div to grow with absolutely positioned child using CSS and jeet?

  13. 13

    How to center div vertically inside of absolutely positioned parent div

  14. 14

    Fit absolutely positioned DIV *completely* in relatively positioned parent DIV?

  15. 15

    Fit absolutely positioned DIV *completely* in relatively positioned parent DIV?

  16. 16

    take div out of parent and add to parent above

  17. 17

    Adding Padding to absolutely positioned div within parent expanding outside parent

  18. 18

    CSS position fixed div still displayed in respect to parent element

  19. 19

    How to Move Parent Div Position using css on hover

  20. 20

    Moving child div above another child div in each parent div

  21. 21

    Position Angular $modalInstance above specific div

  22. 22

    Can't position a div above other docs?

  23. 23

    position a div at bottom css

  24. 24

    CSS position, div inside div specific position

  25. 25

    CSS position, div inside div specific position

  26. 26

    Making overflowing div position to bottom parent div

  27. 27

    div Element changes position (margin) of parent div

  28. 28

    Making overflowing div position to bottom parent div

  29. 29

    Vertically position DIV in the center of parent DIV?

HotTag

Archive