我有一個固定縱橫比的svg,我想把它放在另一個具有動態(tài)高度和寬度的元素旁邊。最終結(jié)果應(yīng)該類似于下面的代碼片段,但是容器元素(黃色背景)應(yīng)該正確地包含它的所有子元素。
* {
box-sizing: border-box;
}
.container {
padding: 10px;
background-color: yellow;
font-size: 24px;
display: inline-flex;
}
.pre {
background-color: blue;
animation: changePadding 1000ms infinite alternate;
}
.post {
background-color: red; // not visible because width is currently 0
width: fit-content; // doesn't work
}
.post svg {
height: 100%;
}
@keyframes changePadding {
from {padding: 0px 0px;}
to {padding: 10px 10px;}
}
<div class="container">
<div class="pre">Stuff</div>
<div class="post">
<svg viewBox="0 0 10 10">
<circle cx="5" cy="5" r="5" fill="purple"></circle>
<line x1="0" y1="0" x2="10" y2="10" stroke="red"></line>
</svg>
</div>
</div>