比方说你有一对夫妇的 <div>
在2个嵌套的flex中(一个用于列,另一个用于行),以得到类似的东西。
你可以调整父节点的大小 <div>
在任何方向上,一切都能如期进行。
注 <div>
被定义为
{
/* flex: 1 1 0px */
flex-grow: 1
flex-shrink: 1
flex-basis: 0px /* i.e. independant of content */
}
<html>
<style type="text/css">
html,
body {
height: 100%;
margin: 0px;
}
body {
padding-left: 30px
}
span {
display: block;
margin-top: 30px
}
#dut {
background-color: red;
}
</style>
<body>
<span id="info"></span>
<div style="
background-color: gray;
height: 200px;
width: 50%;
resize: both;
overflow: auto;" onresize="onResize()">
<div style="
height:100%;
display:flex;
flex-direction: column;
">
<div style="
flex: 1 1 0px;
display:flex;
flex-direction: row;
">
<div style="flex: 1 1 0px; background-color: chocolate;"></div>
<div style="flex: 1 1 0px; background-color: gray;"></div>
<div style="flex: 1 1 0px; background-color: chocolate;"></div>
<div style="flex: 1 1 0px; background-color: gray;"></div>
</div>
<div style="
flex: 1 1 0px;
display:flex;
flex-direction: row;
">
<div style="flex: 1 1 0px; background-color: gray;"></div>
<div id="dut" style="flex: 1 1 0px;"></div>
<div style="flex: 1 1 0px; background-color: gray;"></div>
<div style="flex: 1 1 0px; background-color: chocolate;"></div>
</div>
</div>
</div>
<script>
const dut = window.document.getElementById("dut");
const info = window.document.getElementById("info");
const cb = () => info.innerHTML = `size of RED element: ${dut.offsetWidth} x ${dut.offsetHeight}`
new ResizeObserver(cb).observe(dut)
</script>
</body>
</html>
如果你需要一个 <div>
元素 (红色的那个贴着 "dut "标签)替换成 <canvas>
你会得到。
换句话说,红色的元素不能在收缩的情况下收缩。"300x150" 此处
我们应该可以用 min-height
和 min-width
(挠性计算之前):
{
background-color: red;
min-width: 0;
min-height: 0;
}
结果:
width
现在是正确的,但为什么 不高 ?!
<html>
<style type="text/css">
html,
body {
height: 100%;
margin: 0px;
}
body {
padding-left: 30px
}
span {
display: block;
margin-top: 30px
}
#dut {
background-color: red;
min-width: 0;
min-height: 0;
}
</style>
<body>
<span id="info"></span>
<div style="
background-color: gray;
height: 200px;
width: 50%;
resize: both;
overflow: auto;" onresize="onResize()">
<div style="
height:100%;
display:flex;
flex-direction: column;
">
<div style="
flex: 1 1 0px;
display:flex;
flex-direction: row;
">
<div style="flex: 1 1 0px; background-color: chocolate;"></div>
<div style="flex: 1 1 0px; background-color: gray;"></div>
<div style="flex: 1 1 0px; background-color: chocolate;"></div>
<div style="flex: 1 1 0px; background-color: gray;"></div>
</div>
<div style="
flex: 1 1 0px;
display:flex;
flex-direction: row;
">
<div style="flex: 1 1 0px; background-color: gray;"></div>
<canvas id="dut" style="flex: 1 1 0px;"></canvas>
<div style="flex: 1 1 0px; background-color: gray;"></div>
<div style="flex: 1 1 0px; background-color: chocolate;"></div>
</div>
</div>
</div>
<script>
const dut = window.document.getElementById("dut");
const info = window.document.getElementById("info");
const cb = () => info.innerHTML = `size of RED element: ${dut.offsetWidth} x ${dut.offsetHeight}`
new ResizeObserver(cb).observe(dut)
</script>
</body>
</html>
我想了很多办法(或多或少都很难看),但我的问题是这种行为的根本原因是什么?在那里有另一个css标签,如 min-min-height
?!?
我注意到 min-min-height
在我的情况下等于150像素,这实际上取决于你的浏览器,也取决于画布的 height
属性 (并将是一些关于 viewBox
属性为 <svg>
)
谢谢!
如果你检查 MDN 你可以阅读。
身高
坐标空间的高度,单位为CSS像素。默认值为150。
和
宽度
坐标空间的宽度,单位为CSS像素。默认值为300。
你已经找到了抵消宽度的方法,因为在flexbox世界中,元素可以缩小以适应它们在主轴上的父尺寸(使用 flex-shrink
并加入 min-width
)但在横轴上没有收缩效果,你的代码相当于下面。
html,
body {
height: 100%;
margin: 0px;
}
body {
padding-left: 30px
}
span {
display: block;
margin-top: 30px
}
#dut {
background-color: red;
min-width: 0;
min-height: 0;
height:150px;
width:300px;
}
<span id="info"></span>
<div style="
background-color: gray;
height: 200px;
width: 50%;
resize: both;
overflow: auto;" onresize="onResize()">
<div style="
height:100%;
display:flex;
flex-direction: column;
">
<div style="
flex: 1 1 0px;
display:flex;
flex-direction: row;
">
<div style="flex: 1 1 0px; background-color: chocolate;"></div>
<div style="flex: 1 1 0px; background-color: gray;"></div>
<div style="flex: 1 1 0px; background-color: chocolate;"></div>
<div style="flex: 1 1 0px; background-color: gray;"></div>
</div>
<div style="
flex: 1 1 0px;
display:flex;
flex-direction: row;
">
<div style="flex: 1 1 0px; background-color: gray;"></div>
<div id="dut" style="flex: 1 1 0px;"></div>
<div style="flex: 1 1 0px; background-color: gray;"></div>
<div style="flex: 1 1 0px; background-color: chocolate;"></div>
</div>
</div>
</div>
<script>
const dut = window.document.getElementById("dut");
const info = window.document.getElementById("info");
const cb = () => info.innerHTML = `size of RED element: ${dut.offsetWidth} x ${dut.offsetHeight}`
new ResizeObserver(cb).observe(dut)
</script>
</body>
</html>
我用一个尺寸为300x150的简单div替换了画布。宽度可以收缩,高度只能触发溢出,永远不会收缩。
如果你把高度设为0,你就会得到你想要的东西,因为我们去掉了高度约束,现在它的行为就像一个div(默认情况下,一个空div没有高度)。
html,
body {
height: 100%;
margin: 0px;
}
body {
padding-left: 30px
}
span {
display: block;
margin-top: 30px
}
#dut {
background-color: red;
min-width: 0;
min-height: 0;
}
<span id="info"></span>
<div style="
background-color: gray;
height: 200px;
width: 50%;
resize: both;
overflow: auto;" onresize="onResize()">
<div style="
height:100%;
display:flex;
flex-direction: column;
">
<div style="
flex: 1 1 0px;
display:flex;
flex-direction: row;
">
<div style="flex: 1 1 0px; background-color: chocolate;"></div>
<div style="flex: 1 1 0px; background-color: gray;"></div>
<div style="flex: 1 1 0px; background-color: chocolate;"></div>
<div style="flex: 1 1 0px; background-color: gray;"></div>
</div>
<div style="
flex: 1 1 0px;
display:flex;
flex-direction: row;
">
<div style="flex: 1 1 0px; background-color: gray;"></div>
<canvas id="dut" style="flex: 1 1 0px;" height=0></canvas>
<div style="flex: 1 1 0px; background-color: gray;"></div>
<div style="flex: 1 1 0px; background-color: chocolate;"></div>
</div>
</div>
</div>
<script>
const dut = window.document.getElementById("dut");
const info = window.document.getElementById("info");
const cb = () => info.innerHTML = `size of RED element: ${dut.offsetWidth} x ${dut.offsetHeight}`
new ResizeObserver(cb).observe(dut)
</script>
</body>
</html>
或 height:0;min-height:100%
并保持内在尺寸不变,并能在画布内进行绘制。
html,
body {
height: 100%;
margin: 0px;
}
body {
padding-left: 30px
}
span {
display: block;
margin-top: 30px
}
#dut {
background-color: red;
min-width: 0;
height: 0;
min-height:100%;
}
<span id="info"></span>
<div style="
background-color: gray;
height: 200px;
width: 50%;
resize: both;
overflow: auto;" onresize="onResize()">
<div style="
height:100%;
display:flex;
flex-direction: column;
">
<div style="
flex: 1 1 0px;
display:flex;
flex-direction: row;
">
<div style="flex: 1 1 0px; background-color: chocolate;"></div>
<div style="flex: 1 1 0px; background-color: gray;"></div>
<div style="flex: 1 1 0px; background-color: chocolate;"></div>
<div style="flex: 1 1 0px; background-color: gray;"></div>
</div>
<div style="
flex: 1 1 0px;
display:flex;
flex-direction: row;
">
<div style="flex: 1 1 0px; background-color: gray;"></div>
<canvas id="dut" style="flex: 1 1 0px;" ></canvas>
<div style="flex: 1 1 0px; background-color: gray;"></div>
<div style="flex: 1 1 0px; background-color: chocolate;"></div>
</div>
</div>
</div>
<script>
const dut = window.document.getElementById("dut");
const info = window.document.getElementById("info");
const cb = () => info.innerHTML = `size of RED element: ${dut.offsetWidth} x ${dut.offsetHeight}`
new ResizeObserver(cb).observe(dut)
</script>
</body>
</html>
你甚至可以根据自己的需要更新属性,它的表现也是一样的。
html,
body {
height: 100%;
margin: 0px;
}
body {
padding-left: 30px
}
span {
display: block;
margin-top: 30px
}
#dut {
background-color: red;
min-width: 0;
height: 0;
min-height:100%;
}
<span id="info"></span>
<div style="
background-color: gray;
height: 200px;
width: 50%;
resize: both;
overflow: auto;" onresize="onResize()">
<div style="
height:100%;
display:flex;
flex-direction: column;
">
<div style="
flex: 1 1 0px;
display:flex;
flex-direction: row;
">
<div style="flex: 1 1 0px; background-color: chocolate;"></div>
<div style="flex: 1 1 0px; background-color: gray;"></div>
<div style="flex: 1 1 0px; background-color: chocolate;"></div>
<div style="flex: 1 1 0px; background-color: gray;"></div>
</div>
<div style="
flex: 1 1 0px;
display:flex;
flex-direction: row;
">
<div style="flex: 1 1 0px; background-color: gray;"></div>
<canvas id="dut" style="flex: 1 1 0px;" heght="600" width="600"></canvas>
<div style="flex: 1 1 0px; background-color: gray;"></div>
<div style="flex: 1 1 0px; background-color: chocolate;"></div>
</div>
</div>
</div>
<script>
const dut = window.document.getElementById("dut");
const info = window.document.getElementById("info");
const cb = () => info.innerHTML = `size of RED element: ${dut.offsetWidth} x ${dut.offsetHeight}`
new ResizeObserver(cb).observe(dut)
</script>
</body>
</html>
下面是一个基于前面答案的完整片段(svg和canvas)。
引自@Termani Afif:
{ height:0; min-height:100% }
两者都是用来删除反义词的 它将迫使元素有一个height:0
那么min-height:100%
将再次迫使它占用所有可用空间。所以现在容器将决定高度
<html>
<style type="text/css">
html,
body {
height: 100%;
margin: 0px;
}
body {
padding-left: 30px
}
span {
display: block;
margin-top: 30px
}
#dut {
background-color: red;
min-width: 0;
height: 0;
min-height: 100%;
}
#dut2 {
background-color: red;
min-width: 0;
height: 0;
min-height: 100%;
}
</style>
<body>
<span id="info"></span>
<div style="
background-color: gray;
height: 200px;
width: 50%;
resize: both;
overflow: auto;" onresize="onResize()">
<div style="
height:100%;
display:flex;
flex-direction: column;
">
<div style="
flex: 1 1 0px;
display:flex;
flex-direction: row;
">
<div style="flex: 1 1 0px; background-color: chocolate;"></div>
<div style="flex: 1 1 0px; background-color: gray;"></div>
<div style="flex: 1 1 0px; background-color: chocolate;"></div>
<div style="flex: 1 1 0px; background-color: gray;"></div>
</div>
<div style="
flex: 1 1 0px;
display:flex;
flex-direction: row;
">
<div style="flex: 1 1 0px; background-color: gray;"></div>
<canvas id="dut" style="flex: 1 1 0px;" width=100 height=100></canvas>
<div style="flex: 1 1 0px; background-color: gray;"></div>
<svg id="dut2" style="flex: 1 1 0px;" viewBox="0 0 100 100" preserveAspectRatio="none">
<circle cx="0" cy="0" r="100" stroke="black" stroke-width="1" fill="#a00" />
</svg>
</div>
</div>
</div>
<script>
const dut = window.document.getElementById("dut");
const info = window.document.getElementById("info");
const cb = () => info.innerHTML = `size of RED element: ${dut.offsetWidth} x ${dut.offsetHeight}`
new ResizeObserver(cb).observe(dut)
const ctx = dut.getContext('2d')
ctx.beginPath();
ctx.arc(0, 0, 100, 0, 2 * Math.PI);
ctx.fillStyle = "#a00";
ctx.fill()
ctx.stroke();
</script>
</body>
</html>
</body>
</html>