来自不同 div 的 CSS 文本流动

标签 css

假设我有以下 div:JSFIDDLE

<div>
  <div class="first">Hello My name is </div>
  <div class="second">Something else</div>
</div>

在最大宽度下,first 部分和second 部分分开到下一行。有没有办法让单词彼此相邻?

最佳答案

查看下面的解决方案

只需将 display:table; 添加到 .genraldisplay:table-cell;white-space:nowrap; .first, .second

.general {
    background: #ebeff1;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
    margin-bottom: 16px;
    max-width: 205px;
    display: table; /*Changed*/
}
.first {
    margin-right: 7px;
}

/*New Added*/
.first, .second{
   display: table-cell;
   white-space: nowrap;
}
   
p.pp {
    font-size: 24px;
}
<p class="pp">
   When overflow, the words go to next line: Not good.
</p>
    <div class="first_sample general">
      <div class="first">Hello My name is </div>
      <div class="second">something else</div>
    </div>
    
<p class="pp">
   If there is a space, then words simply flow to next each other: Good.
</p>
    <div class="second_sample general">
      <div class="first">Hello My name is </div>
      <div class="second">something</div>
    </div>
    
<p class="pp">
    The look I am trying to achieve:
</p>
    <div class="third_sample general">
      <div class="first">Hello My name is something else </div>      
    </div>

关于来自不同 div 的 CSS 文本流动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44538305/

相关文章:

javascript - 如何实现自定义游标

css - 是否可以完全抑制 css 规则?

javascript - 如何设置 SVG USE 元素的样式/操作内容

javascript - jQuery load() 函数更改输入文本的高度

css - 带有左、中或右对齐元素的 Bootstrap 导航栏

html - CSS 中的标签索引

css - 如何在不影响容器的情况下添加填充?

css - 在溢出的 div 中滚动背景

html - 按表格设计时事通讯的最佳方法是什么?

javascript - 使用 jquerymobile 滚动标题文本