[英]How to make <span> text wrap nicely below start of first line
I am trying to make a simple 我试图做一个简单的
label: value
pairing look nice. 配对看起来不错。 The problem I have is that when the value is so long that it wraps around and goes on to a second line, the second line starts below the label text. 我的问题是,当值太长以至于可以环绕并继续到第二行时,第二行从标签文本下方开始。 I would like it to start on the same horizontal position as the first line of text. 我希望它从与文本第一行相同的水平位置开始。
The HTML looks like this: HTML看起来像这样:
<div class="status-container">
<span class="status-label">Status:</span>
<span class="status-text">This is some rather long text that I would like to the second line of text to start at the same horizontal position as the first line. At the moment it wraps beneath the 'status:' label which is annoying</span>
</div>
The CSS looks like this: CSS看起来像这样:
.status-container {
margin-top: 100px;
}
.status-text {
font-weight: bold;
margin-left: 10px;
}
I have create a PLNKR to show how it looks: https://plnkr.co/edit/qbFj4bwEpPf7aUldvjBU 我创建了一个PLNKR以显示它的外观: https ://plnkr.co/edit/qbFj4bwEpPf7aUldvjBU
I am sure I am forgetting something extremely simple and obvious, but it is a while since I did this CSS stuff..... All help much appreciated. 我确定我会忘记一些非常简单和明显的内容,但是距离我编写CSS东西已有一段时间了.....非常感谢所有帮助。
Probably one of the best/quickest ways with the best browser support is to use display:table
: 拥有最佳浏览器支持的最佳/快速方法之一可能是使用display:table
:
.status-container { margin-top: 100px; display: table; } .status-text { font-weight: bold; padding-left: 10px; display: table-cell; } .status-label { display: table-cell; }
<div class="status-container"> <span class="status-label">Status:</span> <span class="status-text">This is some rather long text that I would like to the second line of text to start at the same horizontal position as the first line. At the moment it wraps beneath the 'status:' label which is annoying</span> </div>
Otherwise, if you don't need IE9 support, use a flexbox layout : 否则,如果不需要IE9支持,请使用flexbox布局 :
.status-container { margin-top: 100px; display: flex; } .status-text { font-weight: bold; padding-left: 10px; }
<div class="status-container"> <span class="status-label">Status:</span> <span class="status-text">This is some rather long text that I would like to the second line of text to start at the same horizontal position as the first line. At the moment it wraps beneath the 'status:' label which is annoying</span> </div>
And don't forget the overflow:hidden
trick: 并且不要忘记overflow:hidden
技巧:
.status-label { float:left; padding-right:10px; } .status-text { overflow:hidden; display:block; }
<div class="status-container"> <span class="status-label">Status:</span> <span class="status-text">This is some rather long text that I would like to the second line of text to start at the same horizontal position as the first line. At the moment it wraps beneath the 'status:' label which is annoying</span> </div>
You can always display them table-like 您总是可以像桌子一样显示它们
.status-label{
display:table-cell;
}
.status-text{
display: table-cell;
padding-left:10px;
}
Use inline block in your css classes to make them line up horizontally. 在CSS类中使用内联块将它们水平对齐。
.status-label, .status-text {
display: inline-block;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.