我有可变宽度的HTML布局,在可变宽度的内容<div>
左侧有一个固定宽度的菜单(由css max-width和min-width设置)。对于非常狭窄的浏览器窗口,我希望将内容包含在菜单下方,我目前通过在菜单和内容上设置float:left
来实现此目的。
<html>
<head>
<title></title>
</head>
<body>
<div style="width: 200px; float: left; border: 1px black solid">Menu (200px wide)</div>
<div style="max-width: 800px; min-width: 300px; float:left; border: 1px black solid">Content div. This div has max-width: 800px; min-width 300px. It has enough text that it expands to its max-width if there is space available to do so.</div>
</body>
</html>
在此示例中,只要浏览器视口小于1000px(菜单宽度+内容最大宽度),当前就会发生内容div
的换行。我希望首先减小内容的宽度,并且仅当视口小于500px宽(菜单宽度+内容最小宽度)时才将内容包裹在菜单下面
有没有办法实现这一点,要么是我当前安排的浮动<div>
,还是其他?
答案 0 :(得分:5)
请检查这是否是您想要的行为。
<强>样本强>
<强> HTML 强>
<html>
<head>
<title></title>
</head>
<body>
<div class="menu">Menu (200px wide)</div>
<div class="content">Content div. This div has max-width: 800px; min-width 300px. It has enough text that it expands to its max-width if there is space available to do so.</div>
</body>
</html>
<强> CSS 强>
.menu {
width: 200px;
float: left;
border: 1px black solid
}
.content {
max-width: 800px;
min-width: 300px;
margin-left: 200px;
border: 1px black solid
}
@media all and (max-width: 500px) {
.menu {
float: none;
}
.content {
margin-left: 0;
}
}
答案 1 :(得分:3)
我想这就是你想要的:http://jsfiddle.net/joplomacedo/WXFQz/
解决方案是一个简单的媒体查询 - 低于屏幕宽度 XYZpx 执行此。如果您之前从未听说过这篇文章http://css-tricks.com/resolution-specific-stylesheets/
对于那些看不到小提琴的人,这里是html和css:
HTML:
<div class="container"> <!-- it's possible to do it without this extra element. it's simply more intuitive this way -->
<div class="menu"></div>
<div class="content"></div>
</div>
CSS:
.container {
max-width: 1000px; /* your self defined 800px max-width for the content-div + 200px from the .menu's width */
min-width: 200px;
}
.menu,
.content {
height: 200px;
}
.menu {
float: left;
width: 200px;
}
.content {
margin-left: 200px; /* same as '.menu's width */
}
@media (max-width : 400px) {
.menu {
float: none;
width: auto;
}
.content {
margin-left: 0;
}
}
答案 2 :(得分:0)