【问题标题】:Unwanted Text Wrapping in HTMLHTML 中不需要的文本换行
【发布时间】:2018-04-01 16:11:07
【问题描述】:

我是 HTML 和 CSS 的新手。似乎 HTML 中的文本决定了它自己的宽度,不管我做什么容器。这会导致文本在仍有足够空间容纳一行时换行。如何将文本“锚定”到一行?

我已尝试手动将所有父元素的宽度设置为比文本更宽,但文本继续独立计算自己的宽度。

我尝试过使用 white-space: nowrap,但这样做是文本的宽度继续计算与以前相同(好像有文本换行),所以文本溢出偏离中心,因为文本如果有文本换行,则从它应该有的地方开始。

HTML:

<div class="title-wraper" style="

">
  <a><div class="title">A WITCH SCORNED</div></a>
</div>

<div class="title-wraper" style="
    white-space: nowrap;
">
  <a><div class="title">A WITCH SCORNED</div></a>
</div>

CSS:

.title {
    font-family:Baskerville, 'Times New Roman', Times, serif;
    font-size: 60px;
    transform: scale(.5,1);
    line-height: 45px;
  text-align: center;
  margin-right: auto;
  margin-left: auto;
}

这是我的问题的复制:https://jsfiddle.net/qb6fopdx/9/ 在缩小结果窗口之前,问题并不明显。

image demonstrating the issue

【问题讨论】:

  • 使用不间断空格 &amp;nbsp; 代替 html 中的空格
  • 制作元素display:inline-block; --> jsfiddle.net/qb6fopdx/14
  • 这是块元素的正常行为,占据其父容器的全部宽度
  • 是的,但元素不会占用容器的全宽!!!
  • @Adam 你可以添加更多属性 ;) 比如min-width:100%

标签: html css word-wrap


【解决方案1】:

transform: scale() 是导致此问题的原因 - 您正在使用它来更改字母的形状,但它也会更改容器的形状。

您可以通过将容器设置为正常宽度的两倍来覆盖它,因此缩放变换会将其恢复到 100%。您还需要设置 transform-origin 以使定位正确显示:

.title {
  font-family: Baskerville, 'Times New Roman', Times, serif;
  font-size: 90px; /* made it bigger so the difference is visible in a snippet */
  text-align: center;
}

.title-wrapper {
  transform: scale(0.5, 1); 
  white-space: nowrap;
  border: 1px solid; /* so you can see what's going on */
}

.title-wrapper.corrected {
  transform-origin: 0 0;
  width: 200%; 
}
<div class="title-wrapper">
  <a>
    <div class="title">A WITCH SCORNED</div>
  </a>
</div>
<div class="title-wrapper corrected">
  <a>
    <div class="title">A WITCH SCORNED</div>
  </a>
</div>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-08
    • 1970-01-01
    • 1970-01-01
    • 2014-10-05
    相关资源
    最近更新 更多