【问题标题】:How to shrink div to fit word-wrapped text如何缩小 div 以适应自动换行的文本
【发布时间】:2017-02-08 00:13:27
【问题描述】:

我有一个 flex 容器,旁边有图片和文字。此容器以页面为中心。当我缩小页面导致文本自动换行时,容器不再看起来居中,因为文本 div 不会缩小到自动换行文本的大小。

发生自动换行时是否可以缩小文本div?

请参阅 JSFiddle here

可能很难看到我在用 sn-p 谈论什么,因为您可能无法将页面缩小到足以看到换行的程度。最好看看 jsfiddle。

.container,
.content {
  display: flex;
  justify-content: center;
}
.picture {
  height: 130px;
  width: 130px;
  background-color: blue;
}
.text {
  display: flex;
  flex-direction: column;
  justify-content: center;
  margin-left: 10px;
}
.name {
  font-size: 22px;
}
<div class="container">
  <div class="content">
    <div class="picture"></div>
    <div class="text">
      <div class="name">John Jacobjingleheimer</div>
    </div>
  </div>
</div>

【问题讨论】:

  • text-align: center 添加到name,我猜它看起来更好对齐......你觉得呢?
  • @kukkuz 谢谢,但不是我真正想要的。实际上甚至可能都不可能。自动换行后,容器不再居中,因为右侧的边距似乎比左侧多。

标签: html css flexbox


【解决方案1】:

我不确定这是否是您正在寻找的答案,但通过阅读您所写的内容,我认为您正在寻找

margin-left: auto;
margin-right: auto;

上面的margin-left:和margin-right:加上auto值会导致浏览器窗口自动计算左右margin的

.text {
  display: flex;
  margin-left: auto;
  margin-right: auto;
  flex-direction: column;
  justify-content: center;
}

【讨论】:

  • 不完全是。当您缩小页面浏览器宽度并发生自动换行时,左边距约为 8 像素,但右边看起来像 30 像素。它实际上不是 30,但看起来像这样,因为名称所在的 div 并没有缩小到姓氏的边缘。
猜你喜欢
  • 2015-04-20
  • 2017-12-24
  • 2018-05-19
  • 2011-11-24
  • 2012-04-12
  • 1970-01-01
  • 1970-01-01
  • 2020-12-21
  • 1970-01-01
相关资源
最近更新 更多