[英]How to align side bar beneath content if not floated
我傾向於響應式設計,因此遇到了這個問題。
下圖顯示了所有
以下是我正在使用的代碼的考試
<doctype html>
<html>
<head>
<title>Test</title>
<style>
#container{width:100%; height:700px; background:blue; -moz-box-sizing:border-box; padding:10px;}
#box{width:60%; height:70px; background:black;}
#content{width:70%; height:600px; float:left; background:green; margin-top:10px;}
#aside{width:30%; height:800px; float:none; clear:right; background:yellow;}
</style>
</head>
<body>
<div id="container">
<div id="box">
</div>
<div id="content">
</div>
<div id="aside">
</div>
</div>
</body>
</html>
就是這樣: http : //jsfiddle.net/cBh6z/
干得好
#container {width:100%; min-height:700px; background:blue; -moz-box-sizing:border-box; padding:10px;}
#box {width:60%; height:70px; background:black;}
#content {width:70%; height:600px; float:left; background:green; margin-top:10px;}
#aside {width:30%; height:800px; float:none; clear:both; background:yellow;}
您需要清除: 不僅清楚
使用CSS媒體查詢
#container{
width:100%;
height:700px;
background:blue;
box-sizing:border-box;
padding:10px;
}
#box{
width:60%;
height:70px;
background:black;
}
#content{
width:70%;
height:600px;
background:green;
margin-top:10px;
float:left;
}
#aside{
width:30%;
height:800px;
background:yellow;
float:right;
}
@media (max-width: 400px) {
#content,#aside{
display:block;
float:none;
}
#aside{
margin-top:20px;
}
}
當您從一邊移除float:right
,添加clear:left
。
ps如果沒有任何東西浮動正確,則不需要clear:right
沒有浮點的解決方案:
body{margin:0;min-width:500px;}
#container{color:white;width:100%; height:700px; background:blue;padding:10px}
#box{width:60%; height:70px; background:black;margin-bottom:10px}
#content{width:60%; height:300px;background:lightgreen;margin-bottom:10px}
#aside{position:absolute;right:0;top:0;width:30%; height:400px;background:green;margin:10px 10px 0 0}
@media(max-width: 500px){#aside{position:static}}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.