我想要一个两列div布局,其中每一个都可以有可变的宽度。

div { 浮:左; } 接着{ 背景:# ccc; } < div > < / div >树 < < div class = "第二”>视图/ div >

我想要“视图”div扩展到整个可用的宽度后,“树”div已经填补了所需的空间。

目前,我的“视图”div被调整为它所包含的内容 如果两个跳水都占据整个高度也会很好。


不可重复声明:

当设置float:left时,将div扩展到最大宽度 因为左边的宽度是固定的。 帮助div -使div适合剩余的宽度 因为我需要两列都向左对齐


当前回答

我不明白为什么人们愿意如此努力地为简单的柱状布局寻找一个纯css解决方案,而使用旧的TABLE标签是如此容易。

所有浏览器仍然有表布局逻辑…也许你可以叫我恐龙,但我说让它帮你。

<table WIDTH=100% border=0 cellspacing=0 cellpadding=2> < tr > <td WIDTH="1" NOWRAP bgcolor="#E0E0E0">Tree</td> . < td背景= " # F0F0F0 " >视图td > < / < / tr > 表> < /

在跨浏览器兼容性方面风险也小得多。

其他回答

我不确定这是否是你所期待的答案,但是,为什么不将树的宽度设置为“自动”和“视图”的宽度设置为100% ?

如果两个宽度都是可变长度,为什么不用一些脚本或服务器端计算宽度呢?

<div style=“width: <=% getTreeWidth() %>”>Tree</div>

<div style="width: <=% getViewWidth() %>">View</div>

.btnCont { display: table-layout; width: 500px; } .txtCont { display: table-cell; width: 70%; max-width: 80%; min-width: 20%; } .subCont { display: table-cell; width: 30%; max-width: 80%; min-width: 20%; } <div class="btnCont"> <div class="txtCont"> Long text that will auto adjust as it grows. The best part is that the width of the container would not go beyond 500px! </div> <div class="subCont"> This column as well as the entire container works like a table. Isn't Amazing!!! </div> </div>

你可以使用W3的CSS库,它包含了一个叫做rest的类:

<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css"> < div class = " w3-row”> <div class="w3-col " style="width:150px"> < p > 150 px < / p > < / div > <div class="w3-rest w3-green"> < p > w3-rest < / p > < / div > < / div >

不要忘记在页面的标题中链接CSS库:

<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">

这里是官方演示:W3学校Tryit编辑器

使用CSS Flexbox flex-grow属性填充剩余空间。 Html, body { 高度:100%; } 身体{ 显示:flex; } 接着{ flex-grow: 1; } < span style="background: #bef;"树> < / div > <div class="second" style="background: #ff9;"视图> < / div >