我有以下模板:
<div>
<div style="display:inline-block;background-color:#ccc;">
Data
</div>
<div style="display:inline-block;background-color:#ccc;width:80px;">
<span>Work: 22</span>
<span>Todos: 33</span>
</div>
</div>
但是第一个嵌套的div
并没有跨越所有可用的高度。这样两个div都匹配高度。
另外,我想强制文本数据位于左上角。
如何做同样的事?
答案 0 :(得分:3)
.warp{
display: flex;
}
<div class="warp">
<div style="background-color:#ccc;">
Data
</div>
<div style="background-color:#ccc;width:80px;">
<span>Work: 22</span>
<span>Todos: 33</span>
</div>
</div>
margin-right
.warp{
display: flex;
}
.data{
margin-right: 7px;
}
<div class="warp">
<div style="background-color:#ccc;" class="data">
Data
</div>
<div style="background-color:#ccc;width:80px;">
<span>Work: 22</span>
<span>Todos: 33</span>
</div>
</div>
答案 1 :(得分:1)
只需将display: flex
设为父div
<div style="display: flex;">
<div style="display:inline-block;background-color:#ccc;">
Data
</div>
<div style="display:inline-block;background-color:#ccc;width:80px;">
<span>Work: 22</span>
<span>Todos: 33</span>
</div>
</div>
&#13;
答案 2 :(得分:1)
假设您仍然需要两个div之间的空格:
.container {
display: flex;
}
.data {
background-color: #ccc;
width: 40px;
padding-left: 2px;
margin-right: 5px;
}
.input {
background-color: #ccc;
width: 80px;
padding-left: 2px;
}
<div class="container">
<div class="data">
Data
</div>
<div class="input">
<span>Work: 22</span>
<span>Todos: 33</span>
</div>
</div>
答案 3 :(得分:1)
以下是您需要做的更改:
<div style="display: flex;">
<div style="background-color:#ccc;width:40px;">
Data
</div>
<div style="background-color:#ccc;width:80px;">
<span>Work: 22</span>
<span>Todos: 33</span>
</div>
</div>
答案 4 :(得分:0)
如果您不想修复高度,或使用flex - 表可以帮助您
<div style="display: table; ">
<div style="display:table-cell;background-color:#ccc; vertical-align: middle;">
Data
</div>
<div style="display:table-cell;background-color:#ccc;width:80px; vertical-align: middle;">
<span>Work: 22</span>
<span>Todos: 33</span>
</div>
</div>
答案 5 :(得分:0)
您可以尝试此代码:
.parent {
display: table;
}
.parent .child {
display: table-cell;
vertical-align: text-top;
background: #ccc;
border-left: 2px solid #fff;
}
.parent .child span { display: block; }
<div class="parent">
<div class="child">
Data
</div>
<div class="child">
<span>Work: 22</span>
<span>Todos: 33</span>
</div>
</div>