代码之家  ›  专栏  ›  技术社区  ›  Gennady Shumakher

动态DIV框高度与子DIV的中心位置

  •  0
  • Gennady Shumakher  · 技术社区  · 17 年前

    我正在尝试使用css样式构建html,如下图所示: alt text

    我使用以下html来定位内部div:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html>
    <body>
    <div style="background-color: yellow; width: 100px; height: 100px; 
    position: relative;">
        <div style="background-color: gray;  position: absolute; 
        top: 8px; left: 8px; right: 8px; bottom: 8px"></div>
    </div>
    </body>
    </html>
    

    但问题是,如果我希望外部div的高度是动态的,它就会停止工作。删除“高度”值将隐藏外部div。

    如何在所有条件下实现这一目标?

    3 回复  |  直到 7 年前
        1
  •  1
  •   peirix    17 年前

    我不太确定你要带着这个去哪里。但是看起来你只想在里面的div周围加上填充物?

    <div style="background-color: yellow; width: 100px; 
                position: relative; padding: 8px">
    <div style="background-color: gray;">Hello</div>
    

        2
  •  1
  •   please delete me please delete me    17 年前

    好的,您需要这样做(假设您将外部div放置在您想要的位置):

    <div style="float:left; background-color:yellow; width:100px;">
         <div style="background-color:gray; margin:8px;">You need enough content here to push the width out far enough...</div>
    </div>
    

        3
  •  0
  •   Soul_Master    17 年前

    如果相对位置对象中只有绝对位置对象,则必须固定相对位置对象的高度值。