Flexbox父寬度應由child1中的文本決定。當父代增長時,child2中的圖像應該增長。父div應該增長到max-w idth,但是當我把我的圖像放進去時,它放大了我的父div。
#parent {
display: flex;
flex-direction: column;
min-width: 10px;
max-width: 240px;
}
<div id="parent">
<div id="child1">text here should decide parent width</div>
<div id="child2"><img src="https://picsum.photos/400/100" alt="Image" /></div>
</div>
很快,為了說明我的意見,如何防止圖像放大在flexbox CSS的細胞,但有關于這一點的重復
.parent,
#parent {
display: flex;
flex-direction: column;
min-width: 10px;
width:max-content;
max-width: 240px;
border: solid;
}
.parent img,
#parent img {
width: 0;
min-width: 100%;
}
<div id="parent">
<div id="child1">text here should decide parent width</div>
<div id="child2"><img src="https://picsum.photos/400/100" alt="Image" /></div>
</div>
<div class="parent">
<div>t</div>
<div><img src="https://picsum.photos/400/100" alt="Image" /></div>
</div>
<div class="parent">
<div>text</div>
<div><img src="https://picsum.photos/400/100" alt="Image" /></div>
</div>
<div class="parent">
<div>text here</div>
<div><img src="https://picsum.photos/400/100" alt="Image" /></div>
</div>
<div class="parent">
<div>text here should </div>
<div><img src="https://picsum.photos/400/100" alt="Image" /></div>
</div>
<div class="parent">
<div>text here should decide parent width . IS that clear?</div>
<div><img src="https://picsum.photos/400/100" alt="Image" /></div>
</div>
<style>
#parent {
display: flex;
flex-direction: column;
min-width: fit-content;
max-width: 240px;
}
#child1 {
word-wrap: break-word;
}
#child2 {
flex-grow: 1;
}
#child2 img {
width: 100%;
height: auto;
}
</style>
<div id="parent">
<div id="parent">
<div id="child1">text here should decide parent width</div>
<div id="child2"><img src="https://picsum.photos/400/100" alt="Image" /></div>
</div>
</div>