【问题标题】:How to prevent moving text down inside div? [duplicate]如何防止在 div 中向下移动文本? [复制]
【发布时间】:2021-02-04 15:52:22
【问题描述】:

.left {
      float: left;
      width: 75%;
      padding: 1px;
      border: 2px solid #eff0f0;
      font-weight: 450;
      font-size: 14px;
      border-right: none;
      text-align: center;  
    }
<div class="left">https://temo.derocitycapul.com/?code=1234567</div>

它在浏览器上的显示如下:

以及它在小窗口浏览器上的外观:

我的问题是如何防止将文本滑动到另一行并使 div 内的文本在任何浏览器窗口大小下都在一行中。

【问题讨论】:

  • 如果文本不适合屏幕怎么办?
  • @Paulie_D 它应该是可滚动的
  • 'white-space: nowrap'...

标签: html css


【解决方案1】:

white-space:nowrap 应该可以解决问题。使用overflow-x:auto 使其可滚动:

.left {
  float: left;
  width: 75%;
  padding: 1px;
  border: 2px solid #eff0f0;
  font-weight: 450;
  font-size: 14px;
  border-right: none;
  text-align: center;  
  white-space:nowrap;
  overflow-x:auto;
}
div{
  width:100px !important;
}
<p>With white-space:nowrap: </p><div class="left">https://temo.derocitycapul.com/?code=1234567</div><br/>
<p>Without: </p><div class="left" style="white-space:initial">https://temo.derocitycapul.com/?code=1234567</div>

【讨论】:

  • 谢谢,但它不能滚动
  • @Michael 你什么意思?
【解决方案2】:

你可以试试这个方法

.left {
  float: left;
  width: 200px;
  padding: 1px;
  border: 2px solid #eff0f0;
  font-weight: 450;
  font-size: 14px;
  border-right: none;
  text-align: center;  
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
&lt;div class="left"&gt;https://temo.derocitycapul.com/?code=1234567&lt;/div&gt;

【讨论】:

    猜你喜欢
    • 2020-12-18
    • 1970-01-01
    • 2015-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多