我试图让状态栏的高度为容器的100%。我尝试给它固定的高度,但即使高度增加......新行上的文字显示在状态栏下方而不是旁边。
代码:
.list {
padding: 0;
border: 1px #666666 solid;
width: 250px;
}
.list > .status-bar {
background-color: red;
width: 20px;
padding: 20px;
display: inline-block;
height: 100%;
}

<div class="list">
<span class="status-bar"> </span>Test 1 Test 1Test 1 Test 1 Test 1 Test 1
</div>
&#13;
答案 0 :(得分:2)
我建议使用flexbox
,它会为您提供更多灵活性和控制的布局。
.list {
padding: 0;
border: 1px #666666 solid;
width: 250px;
display: flex;
}
.list .status-bar {
background-color: red;
width: 20px;
padding: 20px;
}
.list .status-text {
flex: 1;
}
&#13;
<div class="list">
<div class="status-bar"></div>
<div class="status-text">Test 1 Test 1Test 1 Test 1 Test 1 Test 1 Test 1 Test 1 Test 1 Test 1 Test 1 Test 1 Test 1 Test 1 Test 1 Test 1 Test 1 Test 1 Test 1 Test 1 Test 1 Test 1 Test 1 Test 1 Test 1 Test 1</div>
</div>
&#13;
垂直居中的文字
.list {
padding: 0;
border: 1px #666666 solid;
width: 250px;
display: flex;
}
.list .status-bar {
background-color: red;
width: 20px;
padding: 40px 20px;
}
.list .status-text {
flex: 1;
display: flex;
align-items: center;
}
&#13;
<div class="list">
<div class="status-bar"></div>
<div class="status-text">Test 1 Test 1</div>
</div>
&#13;
答案 1 :(得分:1)
你可以试试这个:
将status-bar
浮动到左侧以允许文本环绕它。
然后通过使用以下方法清除浮动来应用clearfix for float:
.list:after{
clear: both;
content: '';
display: block;
}
让我知道您对此的反馈。谢谢!
.list {
padding:0;
border: 1px #666666 solid;
width: 250px;
}
.list > .status-bar {
background-color: red;
width: 20px;
padding:20px;
display: inline-block;
height:100%;
float: left;
}
.list:after{
clear: both;
content: '';
display: block;
}
<div class="list">
<span class="status-bar"> </span>Test 1 Test 1Test 1 Test 1 Test 1 Test 1
</div>
答案 2 :(得分:1)
发生这种情况是因为文本长于容器的宽度。它正在包装并位于状态栏下方。您可以通过将CSS white-space: nowrap;
添加到.list
课程来解决此问题。
.list {
padding:0;
border: 1px #666666 solid;
width: 250px;
white-space: nowrap; /* added */
}
.list > .status-bar {
background-color: red;
width: 20px;
padding:20px;
display: inline-block;
height:100%;
}
&#13;
<div class="list">
<span class="status-bar"> </span>Test 1 Test 1Test 1 Test 1 Test 1 Test 1
</div>
&#13;
文本将向右溢出(您可能希望以某种方式处理 - 也许overflow:hidden
),但它确实解决了您的问题。
答案 3 :(得分:1)
快速决定:
.list {
display: table;
}
.list > .status-bar {
display: table-cell;
}