我有一个固定宽度的div。 它有两个子元素:
- 第一个包含文本,
- 第二个是类似“徽章”的东西(只是一个带有固定宽度和高度的div)。
所以我想实现类似这样的效果:
我想要第二个子元素紧贴着第一个子元素的文本位置
我尝试通过将display: flex
添加到父元素来解决此问题,但问题是第一个子元素的宽度超过其内容(在下面的代码段示例中)
我尝试将flex-basis: 0
添加到第一个子元素,但然后每个单词都会从新的一行开始,即使有足够的空间在一行中显示整个文本。
也许还有另一种解决方法? 我曾尝试使用绝对定位的子元素解决问题,但也失败了。
.parent {
width: 150px;
background: lightblue;
display: flex;
align-items: center;
}
.first-child {
outline: 1px solid red;
}
.first-child span {
outline: 1px solid white;
}
.second-child {
outline: 1px solid blue;
width: 20px;
height: 10px;
flex-shrink: 0;
}
<div class="parent">
<div class="first-child">
<span>Loooooooong teeeeeext</span>
</div>
<div class="second-child">
</div>
</div>
<br /><br />
<div class="parent">
<div class="first-child">
<span>Short text</span>
</div>
<div class="second-child">
</div>
</div>