我有一个<h3>
内的跨度,一旦文本太长,就会换行到下一行。我需要说明文字全部留在.title
和overflow:hidden
的第一行。描述应该对齐,标题应该左对齐。
此设计模式在整个应用程序中使用,因此更改html元素本身是最后的选择。如果可能的话,我想用一个简单的CSS解决方案来解决这个问题。我一直在尝试overflow
,text-overflow
和white-space
的所有组合,我能想到没有运气。谢谢!
.title {
background-color: lightblue;
color: green;
}
.description {
float: right;
font-weight: bold;
color: black;
}
Long:
<div class="container">
<h3 class="title">
title
<span class="description">descriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescription</span>
</h3>
</div>
<br>
Short:
<div class="container">
<h3 class="title">
title
<span class="description">description</span>
</h3>
</div>
答案 0 :(得分:2)
您可以将.title
设置为white-space:nowrap
+ display:table
。
.title {
white-space: nowrap;
display: table;
}
.title {
background-color: lightblue;
color: green;
white-space: nowrap;
display: table;
}
.description {
font-weight: bold;
color: black;
}
&#13;
Long:
<div class="container">
<h3 class="title">
title
<span class="description">descriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescription</span>
</h3>
</div>
<br>
Short:
<div class="container">
<h3 class="title">
title
<span class="description">description</span>
</h3>
</div>
&#13;
修改1:如果您想要隐藏额外的文字,可以使用white-space:nowrap
+ overflow:hidden
。
.title {
background-color: lightblue;
color: green;
white-space: nowrap;
overflow: hidden;
}
.description {
font-weight: bold;
color: black;
}
&#13;
Long:
<div class="container">
<h3 class="title">
title
<span class="description">descriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescription</span>
</h3>
</div>
<br>
Short:
<div class="container">
<h3 class="title">
title
<span class="description">description</span>
</h3>
</div>
&#13;
编辑2 :如果您需要缩短文字并与长文本匹配而没有隐藏文字,则可以使用white-space:nowrap
+ display:table-row
。注意,需要稍微调整标记 - 将两个容器合并为一个。
.title {
background-color: lightblue;
color: green;
white-space: nowrap;
display: table-row;
}
.description {
font-weight: bold;
color: black;
}
&#13;
<div class="container">
<h3 class="title">
title
<span class="description">descriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescriptiondescription</span>
</h3>
<h3 class="title">
title
<span class="description">description</span>
</h3>
</div>
&#13;
答案 1 :(得分:0)
overflow: hidden;
仅在div具有设定宽度时才有效。您可以将div上的宽度设置为100vw或设置px金额,然后您可以同时使用white-space: nowrap;
和'overflow:hidden;`。
.title {
background-color: lightblue;
color: green;
width: 100vw;
overflow: hidden;
white-space: nowrap;
}
.description {
font-weight: bold;
color: black;
}
答案 2 :(得分:0)
Pangloss让我走上正轨,这是最终为我工作的CSS。
.description {
text-align: right;
width:100%;
overflow:hidden;
font-weight: bold;
color:black;
}
.container {
}
.title {
background-color:lightblue;
color:green;
display:flex;
white-space:nowrap;
}