我需要并排水平堆叠两个 div
元素,其中右边的元素应始终自动调整其大小以适应其内容(最大给定宽度),而左边的元素应该只需使用剩余的空间即可。
是这样的:
到目前为止没有问题。我设法通过将右边的 div
float 到右边并将左边的溢出设置为隐藏来做到这一点:
HTML:
<div class="frame">
<div class="right">
I adjust my with to my content but still need to know my boundaries if I'm floated right (max-width)
</div>
<div class="left">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam lectus. Sed sit amet ipsum mauris. Maecenas congue ligula ac quam viverra nec consectetur ante hendrerit. Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
</div>
</div>
CSS:
.frame {
background-color: lightgreen;
width: 80%;
padding: 12px;
overflow: hidden;
}
.left {
overflow: hidden;
background-color: #709670;
border: 1px solid black;
}
.right {
float: right;
max-width: 200px;
background-color: #127212;
color: white;
border: 1px solid black;
}
挑战在于,当页面显示在小屏幕(或小浏览器窗口,...)上时,我希望两个 div
垂直堆叠,如下所示:
基本上我认为这可以通过一个简单的媒体查询来完成:
@media screen and (max-width: 700px) {
.right {
float: none;
max-width: none;
}
}
唯一的问题是右侧的 div
(根据其内容调整大小的那个)需要在标记中的左侧创建之前创建,以使 float 的东西起作用。结果,它出现在“小”布局中左侧的上方:
这是一个工作示例(只需使用中间的缩放器在“小”和“大”布局之间切换):Example on JSFiddle
我已经尝试使用 display: table
和 table-cell
而不是 float 来完成布局,但这种方式我无法做出正确的选择列与其内容一样大,并且仍然有一个有效的最大宽度集。
最佳答案
如前所述in this stackoverflow post , Flexbox如果您不想求助于 jQuery,可以为您提供帮助,因为您只需要 to support单个现代浏览器:Mobile Safari(编辑 我还发现 IE 中 left
框的高度在我的 Fiddle 中不正确,我不知道为什么;它应该)。
对你来说是: This awesome fiddle!
编辑 由于您希望正确的div
调整其内容,您需要this example相反
CSS:
.frame {
position:relative;
background-color: lightgreen;
width: 80%;
padding: 12px;
overflow:hidden;
flex-flow:row wrap;
-webkit-justify-content: flex;
-moz-justify-content: -moz-flex;
justify-content: flex;
display: -webkit-box; /* Safari */
display: -moz-box; /* Firefox 21- */
display: -ms-flexbox; /* IE 10+ */
display: -webkit-flex; /* Chrome */
display: flex; /* Standard (Firefox 22+) */
-webkit-box-orient: horizontal;
-moz-box-orient: horizontal;
-webkit-box-direction: normal;
-moz-box-direction: normal;
-ms-flex-direction: row;
-webkit-flex-direction: row;
flex-direction: row;
}
.left {
-ms-flex-order: 1;
-webkit-order: 1;
order:1;
-webkit-box-flex: 2; /* OLD - iOS 6-, Safari 3.1-6 */
-moz-box-flex: 2; /* OLD - Firefox 19- */
-webkit-flex: 2; /* Chrome */
-ms-flex: 2; /* IE 10 */
flex: 2; /* NEW, Spec - Opera 12.1, Firefox 20+ */
background-color: #709670;
border: 1px solid black;
width: auto;
}
.right {
-ms-flex-order: 2;
-webkit-order: 2;
order:2;
-webkit-box-flex: 1; /* OLD - iOS 6-, Safari 3.1-6 */
-moz-box-flex: 1; /* OLD - Firefox 19- */
-webkit-flex: 1; /* Chrome */
-ms-flex: 1; /* IE 10 */
flex: 1; /* NEW, Spec - Opera 12.1, Firefox 20+ */
background-color: #127212;
color: white;
border: 1px solid black;
width: auto;
-webkit-box-ordinal-group: 2; /* OLD - iOS 6-, Safari 3.1-6 */
-moz-box-ordinal-group: 2; /* OLD - Firefox 19- */
-ms-flex-order: 2; /* TWEENER - IE 10 */
-webkit-order: 2; /* NEW - Chrome */
order: 2; /* NEW, Spec - Opera 12.1, Firefox 20+ */
}
@media screen and (max-width: 700px) {
.frame {
-webkit-box-orient: vertical;
-moz-box-orient: vertical;
-webkit-box-direction: normal;
-moz-box-direction: normal;
box-orient: vertical;
-ms-flex-direction: column;
-webkit-flex-direction: column;
flex-direction: column;
}
}
我对我的结果非常满意,我觉得你会的!我相信它几乎完全支持(Mobile Safari 不支持它)!
关于html - 占用剩余水平空间并在小屏幕上垂直堆叠的 Div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17408256/