css - 即使可以放在一行中,如何在新行中显示文本?

标签 css word-wrap

我正在尝试实现这样的布局:

enter image description here

所以副标题总是至少在第二行,即使它可以放在标题旁边的第一行。如果标题很长并且分成两行或更多行,则副标题应跟在标题后面,不要换行。

这是我目前所做的,但这个解决方案并不完美:

div.data {
  background:white;
  position:relative;
  min-height: 2em;
  float:left;
  max-width:150px;
  margin: 50px;
}

.title {
  position:relative;
  overflow:hidden;
  background: white;
  z-index: 10;
}

.title span {
  position:relative;
}

.title span span {
  position:absolute;
  right:0px;
  opacity:1;
  top: 18px;
  display: block;
  transform: translateX(100%);
  padding-left: 5px;
  color: red;
}

span.subtitle {
  position:absolute;
  bottom:0;
  left:0;
  z-index: 5;
  display:block;
  color: red;
}
<div class="data">
  <div class="title"><span>title  <span>subtitle</span></span></div>
  <span class="subtitle">subtitle</span>
</div>


<div class="data">
  <div class="title"><span>reaallllyyyy loooooong title  <span>subtitle</span></span></div>
  <span class="subtitle">subtitle</span>
</div>

抱歉问题的标题,这是我能想到的最好的。

最佳答案

我已经简化了你的 HTML,我相信你真的不想要你现在的那个。

添加一个向右浮动的伪元素似乎可以满足您的要求:仅当副标题位于第一行时才将其强制换行:

伪元素有一个用于演示目的的颜色,在生产中将其移除

.data {
  display: inline-block;
  background-color: antiquewhite;
  max-width: 150px;
  margin: 10px;
}

.title {
  color: blue;
}

.subtitle {
  color: gray;
}

.title:before {
  content: " ";
  float: right;
  width: 150px;
  height: 1px;
  background-color: green;
}
<div class="data">
  <span class="title">title</span>
  <span class="subtitle">subtitle</span>
</div>
<div class="data">
  <span class="title">reaallllyyyy loooooong title</span>
  <span class="subtitle">subtitle</span>
</div>

关于css - 即使可以放在一行中,如何在新行中显示文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45110295/

相关文章:

html - 针对 Windows Phone 诺基亚 Lumia 的 CSS 媒体查询

jquery - 如何使用 jQuery 创建导航动画

responsive-design - 带有子显示的子内联 block 的 CSS div 没有保持高度

javascript - 如何创建一个固定在屏幕底部但当用户向下滚动时它不再是固定页脚的页脚?

java - 是否有模拟 Collections.singleton()/Collections.singletonList()/Collections.singletonMap() 来获取可变集合?

android - 防止 Android 的 TextView 断开链接

c - c 中的文本换行程序无法正常工作

frontend - 使用边界半径的不同半径的 CSS3PIE 元素 Angular ?

html - 对齐表中的数据

html - 包裹在 div 容器内的表格