开发者

How to place a consistent 5px margin around an inner div without specifiying inner divs height?

开发者 https://www.devze.com 2022-12-18 05:30 出处:网络
There is an inner div of fixed width and variable height contained by an outer div. The inner div should have a 5px margin on all 4 sides between it and the outer div.

There is an inner div of fixed width and variable height contained by an outer div. The inner div should have a 5px margin on all 4 sides between it and the outer div.

How can this be achieved if the height of inner div is not specified?

With the height of the inner div specified this is very simple. But the inner div is a content container for a masterpage. The content going into this div will have a different height for each page that uses the master page.

If the inner divs height is not set, the bottom margin (between where the inner div ends and the outer div ends) is always 2-3px too long. i.e. 5 5 5 8 This happens across browser types.

Here is the CSS:

#contentframe
{
    position: relative;
    width: 1010px;
    left: 0px;
    top: 0px;
    margin: 0px;
    padding-top:5px;
    padding-bottom:5px;
    padding-left: 14px;
}
#content
{
    position: relative;
    left: 0px;
    top: 0px;
    width: 980px;
    margin: 0px;
    padding: 0px;
    background-color: #cccccc;

}

*** Note: Setting margin-bottom of #content to 5px does not work.

HTML:

    <div id="contentframe">
        <div id="content">
            variable height content will go in here
        </div>
    </div>

This should be dead simple. Set: Outer divs padding to 5px and that's it. But that only works if inner divs height is specified. Otherwise there is an annoyingly "high" bottom margin.

EDIT: Full Source

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<st开发者_如何学编程yle type="text/css"> 
body
{
    -x-system-font: none;
    background-color: #A2A2A2;
    margin-top: 0px;
    margin-bottom: 35px;
    padding: 0px;
}
#centeredframe
{
    width: 1010px;
    margin-left: auto;
    margin-right: auto;
    margin-top: 0px;
    padding: 0px;
}

#contentframe
{
    position: relative;
    width: 1010px;
    left: 0px;
    top: 0px;
    margin: 0px;
    padding-top: 5px;
    padding-bottom: 5px;
    padding-left: 5px;
    background-color: #ffffff;
}

#content
{
    position: relative;
    left: 0px;
    top: 0px;
    width: 1005px;
    margin: 0px;
    padding: 0px;
    height:300px;
    color: #ffffff;
    background-color: #000000;
}

</style>
</head>
<body>
    <div id="centeredframe">
        <div id="contentframe">
            <div id="content">
                <p>hgjghjghjghjg<p>
                <p>hgjghjghjghjg<p>
                <p>hgjghjghjghjg<p>
                <p>hgjghjghjghjg<p>
            </div>
        </div>
    </div>
</body>
</html>


This should work on IE7/8 and other non-IE browsers.

The extra 'padding/margin' to the top and/or bottom is introduced by the <p> elements. This is due to the uncollapsing margin for empty-content/padding/border areas (in this case, the contentFrame). Refer to the W3C Box Model on collapsing margins.

There are a few ways around it, one of which is to introduce a thin (1px) border which blends into the background of the DIV and then compensating with the width/height. Below is another hack by manipulating the margin/padding of the <P> element within the content DIV.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css"> 
body
{
    -x-system-font: none;
    background-color: #A2A2A2;
    margin: 0;
    margin-bottom: 35px;
    padding: 0px;
}
#centeredframe
{
    width: 1010px;
    margin: 0 auto; /* merged */
    padding: 0;
}

#contentframe
{
    width: 1000px;
    margin: 0;
    padding: 5px;
    background-color: #ffffff;
}
#content
{
    padding: 0;
    color: #ffffff;
    background-color: #000000;
    height: auto;
    min-height: 300px;
}
#content p
{
    margin: 0px; /* removed the default margin of p element*/
    padding: 16px 0; /* replaced with padding to have background*/
}
</style>
</head>
<body>
    <div id="centeredframe">
        <div id="contentframe">
            <div id="content">
                <p>hgjghjghjghjg</p>
                <p>hgjghjghjghjg</p>
                <p>hgjghjghjghjg</p>
                <p>hgjghjghjghjg</p>
            </div>
        </div>
    </div>
</body>
</html>
0

精彩评论

暂无评论...
验证码 换一张
取 消