2011-01-22 155 views
2

我希望身体部分位于菜单下方,但是菜单与身体合并。这段HTML代码有什么问题?

http://jsfiddle.net/aqFGD/

<!DOCTYPE html> 
<html> 
<head> 
    <title>Hi</title> 
    <style type="text/css"> 
    body 
    { 
     background-color: #369; 
    } 
    li.changeMe 
    { 
     background-color: #DEE; 
     color: #369; 
     font-weight: bold; 
     padding: 8px 16px; 
     margin: 5px; 
     display: inline; 
    } 
    #bodystuff 
    { 
     background-color: White; 
     display: block; 
     padding: 15px 10px; 
    } 


    </style> 
</head> 

<body> 
    <div style="margin: 15px 10px 0;"> 
    <div style="font-size: 30pt; font-weight: bold; color: White; font-family: Arial;">My Website</div> 
    <div style="float: right; display: block; margin: 0 0 25px 0"><ul style="list-style-type: none; "><li class="changeMe">Hi</li><li class="changeMe">Hello</li></ul></div> 
    <div id="bodystuff">Hi this is the body</div> 
    </div> 
</body> 
</html> 

回答

1

如果你打算做任何事情不仅仅是玩这个,你应该用我的更新的代码。

我提高了很多东西 - 我可以解释什么,为什么,如果你想。

我假设你想让菜单按钮触摸#bodystuff的顶部,就我所见,其他发布的答案都没有进一步的变化。

Live Demo

<!DOCTYPE html> 
<html> 
<head> 
<title>Hi</title> 
<style type="text/css"> 
body { 
    background-color: #369; 
    font-family: Arial, sans-serif; 
} 
#container { 
    margin: 15px 10px 0; 
} 
#header { 
    font-size: 30pt; 
    color: #fff; 
    margin: 0; 
    padding: 0; 
    font-weight: bold 
} 
#bodystuff { 
    background-color: #fff; 
    clear: both; 
    padding: 15px 10px 
} 
#menu { 
    float: right 
} 
#menu ul { 
    list-style-type: none 
} 
#menu li { 
    background-color: #dee; 
    color: #369; 
    font-weight: bold; 
    float: right; 
    margin: 0 5px; 
} 
#menu a { 
    display: block; 
    padding: 8px 16px; 
} 
</style> 
</head> 

<body> 

<div id="container"> 
    <h1 id="header">My Website</h1> 
    <div id="menu"> 
     <ul> 
      <li><a href="#">Hi</a></li> 
      <li><a href="#">Hello</a></li> 
     </ul> 
    </div> 
    <div id="bodystuff"> 
     Hi this is the body 
    </div> 
</div> 

</body> 
</html> 
1

我想你错过了在需要浮动div的,position:absolute。否则它将内联。

编辑:对不起,我误解了问题。

2

只需添加clear:both#bodystuff的CSS清除浮动。

3

您需要在菜单和身体之间clear。例如:

<!--- menu stuff ---> 
<div style="clear: both;"></div> 
<!--- body stuff ---> 

你的榜样,从而修改:http://jsfiddle.net/redler/aqFGD/3/

3

您有上面的“身体”部分浮动,而你是不是之前新的内容清除。

浮动元素必须清除,即。停止浮动的东西,并开始一个新的。

http://jsfiddle.net/aqFGD/1/

看到更新的演示的修改。

<br style="clear: both;" />