html - 设置 `width: auto` 在 flex-direction: column;
问题描述
我div1
有flex-direction: column
。在那个 div1 里面我想要另一个div2
.
我将用不同长度的单词填充该 div2,并且我希望它width
是auto
(可自我调整的)。但width: auto
不适用于flex-direction: column
. 而且我想div2
向左对齐。
.parent {
background: lightblue;
padding: 10px;
display: flex;
flex-direction: column;
}
.child {
display: flex;
border: 2px solid green;
border-radius: 4px;
padding: 10px;
///////
width: auto;
}
<div class='parent'>
<div class='child'>
<span>
Some text
</span>
</div>
</div>
flex-direction
我想要与设置为相同的行为row
。像这样:
.parent {
background: lightblue;
padding: 10px;
display: flex;
flex-direction: row;
}
.child {
display: flex;
border: 2px solid green;
border-radius: 4px;
padding: 10px;
}
<div class='parent'>
<div class='child'>
<span>
Some text
</span>
</div>
</div>
PS 不能使用width: max-content;
和width: min-content;
解决方案
放在align-items:flex-start
父母身上。
.parent {
background: lightblue;
padding: 10px;
display: flex;
flex-direction: column;
align-items: flex-start;
}
.child {
display: flex;
border: 2px solid green;
border-radius: 4px;
padding: 10px;
}
<div class='parent'>
<div class='child'>
<span>
Some text
</span>
</div>
</div>
或者,align-self:start
在孩子身上
.parent {
background: lightblue;
padding: 10px;
display: flex;
flex-direction: column;
}
.child {
display: flex;
border: 2px solid green;
border-radius: 4px;
padding: 10px;
align-self: start;
}
<div class='parent'>
<div class='child'>
<span>
Some text
</span>
</div>
</div>
推荐阅读
- c++ - 为什么 std::priority_queue::emplace 是对数的?
- apache-spark - Apache Ignite:即使是 200 条记录,我们也没有得到结果
- node.js - next.js with-mongodb 构建错误。`下一个构建`的错误?
- node.js - 无法使用 execFileSync 获取标准输出或标准错误
- python - 将具有 ~= 不等于条件的 MATLAB sum 函数转换为 Python
- python - 是否有任何命令可以直接将用户输入的文本放在 python 的不同窗口中?
- javascript - 如何合并两个 onChange 事件?
- firebase - Flutter 和 Firestore:如何从 add 函数中获取文档 ID?
- plsql - 如何在 LOB 列中创建 XLS
- c++ - 字符串和开关语句有问题??C++评分系统