代码之家  ›  专栏  ›  技术社区  ›  rism

如何在不指定内部div高度的情况下,在内部div周围放置一致的5px边距?

css
  •  0
  • rism  · 技术社区  · 15 年前

    外部div包含一个固定宽度和可变高度的内部div。内部div与外部div之间的所有4条边上都应有5px的边距。

    如果未指定内部div的高度,如何实现这一点?

    对于指定的内部div的高度,这非常简单。但内部div是母版页的内容容器。对于使用母版页的每个页面,进入该div的内容将具有不同的高度。

    以下是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;
    
    }
    

    ***注意:将#内容的边距底部设置为5px不起作用。

    HTML:

        <div id="contentframe">
            <div id="content">
                variable height content will go in here
            </div>
        </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-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>
    
    1 回复  |  直到 15 年前
        1
  •  2
  •   o.k.w    15 年前

    这应该适用于IE7/8和其他非IE浏览器。

    <p> 元素。这是由于空内容/填充/边框区域(在本例中为contentFrame)的展开边距造成的。参考 W3C Box Model

    有几种方法可以解决这个问题,其中之一是引入一个薄(1px)边框,将其混合到DIV的背景中,然后用宽度/高度进行补偿。下面是另一个通过操纵 <P> 内容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>