我正在尝试在固定宽度容器中的多行文本旁边为颜色图例绘制一个正方形。但是,我遇到了一个问题,即使文本和正方形应该适合容器,但由于文本元素占用的水平空间比应有的更多,正方形会被挤压成矩形。有没有办法(最好没有硬编码的幻数)确保 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;
}
答案 0 :(得分:0)
使用 min-width
和 min-height
而不是使用 width
和 height
。这将确保正方形始终具有特定的宽度和高度。
.square {
min-width: 25px;
min-height: 25px;
background-color: red;
}