我需要水平地并排堆叠两个div
元素,其中右边的应该始终自动调整其大小以适应其内容(直到给定的最大宽度),而左边的应该只使用左边的空间。
如下所示:
到目前为止,没有问题。我已经设法做到了这一点,方法是将右侧的div
浮动到右侧,并将左侧的溢出设置为隐藏:
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
(根据其内容调整大小),然后再创建左边的work,这样浮动的东西才能工作。因此,它在“小”布局中出现在左侧布局的上方:
下面是一个有效的示例(只需使用中间的调整大小在“小”和“大”布局之间切换):Example on JSFiddle
我已经尝试使用display: table
和table-cell
来完成布局,而不是浮点,但是这样我无法使正确的列与其内容一样大,并且仍然有一个有效的最大宽度设置。
发布于 2013-07-01 15:50:53
正如in this stackoverflow post所说,如果你不想求助于jQuery,Flexbox可以帮助你,因为你只需要一个现代浏览器: Mobile Safari (编辑我也发现IE中left
框的高度在我的提琴中不能正常工作,我不知道为什么;它应该)。
对于您来说,应该是: 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不支持它)!
发布于 2013-07-01 16:23:45
一种基于CSS/jQuery的方法
假设HTML和内容可以按如下方式进行调整,其中.left
元素出现在.right
元素之前:
<div class="frame">
<div class="left">Lorem ipsum dolor sit amet, c...</div>
<div class="right">
<div class="color-wrap">I adjust my width ...</div>
</div>
</div>
对于宽屏幕,您可以使用以下CSS:
.frame {
background-color: lightgreen;
width: 80%;
padding: 12px;
overflow: auto;
}
.left {
background-color: #709670;
border: 1px solid black;
display: table-cell;
}
.left .color-wrap {
}
.right {
display: table-cell;
width: 200px;
vertical-align: top;
}
.right .color-wrap {
background-color: #127212;
color: white;
border: 1px solid black;
display: inline-block;
}
对于较小的屏幕:
@media screen and (max-width: 700px) {
.left {
display: block;
}
.right {
display: block;
width: auto;
}
.right .color-wrap {
display: block;
width: auto;
color: yellow;
}
}
对于大屏幕,您可以使用display: table-cell
并设置.right
元素的宽度,或者使用最大宽度和最小宽度的组合,这取决于您希望如何处理单行文本的结尾大小写。
如果您需要背景颜色来仅绘制文本区域,则使用包装元素,如.color-wrap
,并将背景颜色应用于该元素,而不是应用于表格单元格父元素。
对于较小的屏幕,将display属性重置为block;对于.right
,将display属性设置为width: auto
。
这是一种相对简单的方法,只有一个约束与处理.right
具有单行文本的情况有关。
处理右侧的短线-调整宽度
要允许.right
块收缩以适应单行内容,您需要调整.right
表格单元格的宽度。
这可以使用以下jQuery来完成:
var rightCellMaxWidth = parseInt($(".right").css("width"));
function fixRightCellWidth() {
$(".right .color-wrap").each(function () {
var rightCellWidth = $(this).outerWidth();
var rightCellType = $(this).css("display");
if (rightCellWidth < rightCellMaxWidth
&& rightCellType == "inline-block") {
$(this).parent().width(rightCellWidth);
} else {
$(this).parent().removeAttr("style");
}
});
}
$(window).resize(function () {
fixRightCellWidth();
});
fixRightCellWidth();
诀窍是获得元素.right .color-wrap
的宽度。您还需要检查显示类型以查看您所处的媒体模式,对于大屏幕使用inline-block
,对于较小屏幕使用block
。
对于较大的屏幕,将右侧表格单元格元素宽度设置为.color-wrap
子级的宽度。对于单行文本,宽度将小于200px;对于多行文本,宽度将为最大宽度值200px。
您还需要删除内联样式,以清除多行情况或小屏幕情况的宽度设置。
演示小提琴:http://jsfiddle.net/audetwebdesign/N4KE2/
设计注释
如果没有JavaScript/jQuery,页面将优雅地降级为固定宽度的右元素,并且页面仍将可用。
发布于 2013-07-01 15:44:11
我想不出任何纯粹的CSS解决方案,但是如果你倾向于使用jquery,你可以像这样使用append方法:
$('.frame').append($('.right'));
这只会将你的“右”块移动到“frame”子元素的末尾。
获取屏幕/窗口/框架宽度有许多解决方案,具体取决于您的浏览器。
https://stackoverflow.com/questions/17408256
复制