【问题标题】:Text occupying more space than needed using flexbox使用 flexbox 占用比需要更多空间的文本
【发布时间】:2021-05-12 09:58:40
【问题描述】:

我正在尝试在固定宽度容器中的多行文本旁边绘制一个正方形,用于颜色图例。但是,我遇到了一个问题,即使文本和正方形应该适合容器,但由于文本元素占用的水平空间超出了应有的水平,正方形会被挤压成一个矩形。有没有办法(最好没有硬编码的幻数)确保 p 元素只占用显示文本所需的水平空间?

相关 MWE:

html:

<div class="div">
  <div class="square">
  </div>
  <p class="text">
    Lorumipsum  dolorsitamet
  </p>
</div>

css:

.div {
    width: 140px;
    display: flex;
    align-items: center;
    outline: 1px solid blue;
}

.square {
    width: 25px;
    height: 25px;
    background-color: red;
}

.text {
    margin-left: 2px;
    outline: 1px solid red;
}

小提琴: http://jsfiddle.net/6jxtp8k5/59/

【问题讨论】:

  • 你这里是flexbox环境,所以你需要禁止span收缩-flex-shrink: 0;

标签: css text flexbox fixed-width


【解决方案1】:

使用min-width 和min-height 而不是使用width 和height。这将确保正方形始终具有特定的宽度和高度。

.square {
    min-width: 25px;
    min-height: 25px;
    background-color: red;
}

【讨论】:

    猜你喜欢
    • 2020-10-29
    • 2013-05-14
    • 2011-09-07
    • 1970-01-01
    • 1970-01-01
    • 2017-03-21
    • 1970-01-01
    • 2015-09-05
    • 1970-01-01
    相关资源
    最近更新 更多