简体   繁体   English

防止使用CSS包装浮动的span元素

[英]Prevent wrapping of floated span element with CSS

I have a span inside an <h3> that is wrapping to the next line once the text gets too long. 我在<h3>中有一个跨度,一旦文本太长,该跨度将换行到下一行。 I need the description text to all stay on the first line with the .title and overflow:hidden . 我需要描述文字全部都放在.titleoverflow:hidden的第一行。 The description should align right, the title should align left. 说明应右对齐,标题应左对齐。

This design pattern is used throughout the application, so changing the html elements themselves is a last resort. 在整个应用程序中都使用了这种设计模式,因此更改html元素本身是不得已的选择。 I'd like to solve this with a simple CSS solution if possible. 如果可能的话,我想用一个简单的CSS解决方案来解决这个问题。 I've been trying every combination of overflow , text-overflow , and white-space that I can think of with no luck. 我一直在尝试overflowtext-overflowwhite-space所有组合,这些都是我想不到的。 Thanks! 谢谢!

JSFiddle 的jsfiddle

 .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"></span> </h3> </div> <br> Short: <div class="container"> <h3 class="title"> title <span class="description">description</span> </h3> </div> 

You can set .title to white-space:nowrap + display:table . 您可以将.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; } 
 Long: <div class="container"> <h3 class="title"> title <span class="description"></span> </h3> </div> <br> Short: <div class="container"> <h3 class="title"> title <span class="description">description</span> </h3> </div> 


Edit 1: if you want to hide the extra text, you can use white-space:nowrap + overflow:hidden . 编辑1:如果要隐藏多余的文本,可以使用white-space:nowrap + overflow:hidden

 .title { background-color: lightblue; color: green; white-space: nowrap; overflow: hidden; } .description { font-weight: bold; color: black; } 
 Long: <div class="container"> <h3 class="title"> title <span class="description"></span> </h3> </div> <br> Short: <div class="container"> <h3 class="title"> title <span class="description">description</span> </h3> </div> 


Edit 2: if you need the short text to expand and match the long text with no hidden text, you can use white-space:nowrap + display:table-row . 编辑2:如果您需要短文本来扩展和匹配长文本而没有隐藏文本,则可以使用white-space:nowrap + display:table-row Note, need to adjust the markup slightly - combine the two containers into one. 请注意,需要稍微调整标记-将两个容器合并为一个。

 .title { background-color: lightblue; color: green; white-space: nowrap; display: table-row; } .description { font-weight: bold; color: black; } 
 <div class="container"> <h3 class="title"> title <span class="description"></span> </h3> <h3 class="title"> title <span class="description">description</span> </h3> </div> 

overflow: hidden; will only work if the div has a set width. 仅当div具有设定的宽度时才起作用。 You can set a width on the div to be 100vw or a set px amount and then you can use your white-space: nowrap; 您可以将div上的宽度设置为100vw或设置px数量,然后可以使用white-space: nowrap; and 'overflow: hidden;` together. 和“溢出:隐藏;”在一起。

.title {
  background-color: lightblue;
  color: green;
  width: 100vw;
  overflow: hidden;
  white-space: nowrap;
}

.description {
  font-weight: bold;
  color: black;
}

Pangloss put me on the right track, here is the CSS that ended up working for me. 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM