div的HTML-CSS位置问题

dom*_*enk 1 html css

我遇到了一个问题,我测试的任何东西似乎都没有用.

我的HTML:

<div id="parent">
    <div id="top_parent">Top_background
        <div id="top">Top_Picture</div></div>
    <div id="content">Here comes random stuff<div>
        </div>

        <div id="bottom">Footer</div>
Run Code Online (Sandbox Code Playgroud)

CSS:

#top_parent {
    background:#f00;
    height:100px;
}

#top{
    background:#0f0;
    float:right;
    position:relative;   
    height:100px;
    width:50%;
}
#content{
    top:-50px;
    background:#00f;
    <!--   position:relative;-->
    height:100px;
    width:80%;
    margin:auto;
}
#parent{
    background:#000;
    height:350px;
    width:100%; 
}

#bottom {
    height: 50px;
    background:#ff0;
    bottom:0px;
    <!--position:absolute; -->
    <!--position:relative; -->
}
Run Code Online (Sandbox Code Playgroud)

现在我的问题是,页脚不会在parentdiv下面,它停留在内容区域.我究竟做错了什么?

jsF链接:我的来源

谢谢您的帮助.

Ode*_*ded 5

你没有关闭这个div:

<div id="content">Here comes random stuff<div>
Run Code Online (Sandbox Code Playgroud)

应该:

<div id="content">Here comes random stuff</div>
Run Code Online (Sandbox Code Playgroud)

如果你缩进你的div,你可以很容易地看到这个:

<div id="parent">
  <div id="top_parent">Top_background
    <div id="top">Top_Picture</div>
  </div>
  <div id="content">Here comes random stuff<div> <!-- Can see the problem -->
</div>
<div id="bottom">Footer</div>
Run Code Online (Sandbox Code Playgroud)

  • @Auro - 就像我说的,如果你使用干净的缩进标记,这样的东西很容易被发现. (2认同)