Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have absolute div fit parent width/padding?

Tags:

html

css

I need to have positioning set to absolute so I can have #bottom fixed at the bottom of the screen. I also need to have this fit the width/padding of the container #panel. When I set the position to absolute however, the width just fills the whole screen's width, how can I stop this? I need #bottom to fit inside the width/padding of #panel.

HTML:

<div id="panel">
    <div id="bottom">
        <div class="update"></div>
    </div>
</div>

CSS:

#panel {
    width: 21.25%;
    height: 100%;
    background-color: #0794ea;
    float: left;
    padding: 0 1.5%;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
}

.update {
    width: 100%;
    background-color: #006699;
    text-align: center;
    height: 56px;
    color: white;
}

#bottom {
    position: absolute;
    bottom: 20px;
    width: 100%;
}

enter image description here

Above is an image of what's happening. Green is the padding and blue is the content area it should be fitting in (dark blue is the actual div (#panel) that I'm trying to fit in the content area). I'm assuming because it's absolute it's ignoring this, I'm looking for a way to get around this.

Fiddle: http://jsfiddle.net/qTJhW/

Thanks

like image 262
jskidd3 Avatar asked Jul 29 '13 11:07

jskidd3


People also ask

How do you fit a full width to a div?

What you could do is set your div to be position: absolute so your div is independent of the rest of the layout. Then say width: 100% to have it fill the screen width. Now just use margin-left: 30px (or whatever px you need) and you should be done.

How do you set the height of an absolute div?

Centering div (vertical/horizontally) Make inner div position absolute and give top and bottom 0. This fills the div to available height. (height equal to body.)


1 Answers

The problem with what you are doing is that it is taking the the entire width including the padding and aligning it to the left side width the padding. You can fix this by using a wrapper with a relative position. Also don't forget to make the #panel position relative.

The code you end up with is something along these lines:

<div id="panel">
    <div class="wrapper">
        <div id="bottom">
            <div class="update">
                a          
            </div>
        </div>    
    </div>
</div>

And the CSS:

#panel {
    width: 21.25%;
    height: 100%;
    background-color: #0794ea;
    float: left;
    padding: 0 1.5%;
    box-sizing: border-box;
}

.update {
    width: 100%;
    background-color: #006699;
    text-align: center;
    height: 56px;
    color: white;
}

.wrapper {
    position: relative;
    height: 100%;
}

#bottom {
    position: absolute;
    bottom: 20px;
    left: 0;
    width: 100%;
    background: yellow;
}

Here is an example: http://codepen.io/DanielVoogsgerd/pen/Lezjy

like image 118
Daniël Voogsgerd Avatar answered Sep 20 '22 16:09

Daniël Voogsgerd