grow image to size of sibling

See: https://svelte.dev/repl/a8f8ff1c7384448d96e36d4a9b30cfea?version=4.2.11 Is it possible to make the icon grow to the height of its sibling?
image grow height • REPL • Svelte
Cybernetically enhanced web apps
6 Replies
MarkBoots
MarkBoots10mo ago
that will be difficult if the image grows in height, it will also grow in width that will make the sibling smaller in width and possibly make it higher as the text wraps to the next row. that in turn will make the image grow in height again, turning into a never ending loop
reddogg476
reddogg47610mo ago
this may be something to work with:
<script>
let name = 'world';
</script>

<div class="gift-box">
<div>
<img alt="gift" src="data:image/svg+xml,%3Csvg%20xmlns=%22http://www.w3.org/2000/svg%22%20class=%22icon%20icon-tabler%20icon-tabler-gift%22%20width=%2224%22%20height=%2224%22%20viewBox=%220%200%2024%2024%22%20stroke-width=%222%22%20stroke=%22currentColor%22%20fill=%22none%22%20stroke-linecap=%22round%22%20stroke-linejoin=%22round%22%3E%3Cpath%20stroke=%22none%22%20d=%22M0%200h24v24H0z%22%20fill=%22none%22/%3E%3Cpath%20d=%22M3%208m0%201a1%201%200%200%201%201%20-1h16a1%201%200%200%201%201%201v2a1%201%200%200%201%20-1%201h-16a1%201%200%200%201%20-1%20-1z%22%20/%3E%3Cpath%20d=%22M12%208l0%2013%22%20/%3E%3Cpath%20d=%22M19%2012v7a2%202%200%200%201%20-2%202h-10a2%202%200%200%201%20-2%20-2v-7%22%20/%3E%3Cpath%20d=%22M7.5%208a2.5%202.5%200%200%201%200%20-5a4.8%208%200%200%201%204.5%205a4.8%208%200%200%201%204.5%20-5a2.5%202.5%200%200%201%200%205%22%20/%3E%3C/svg%3E" />

</div>
<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor
invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.consetetur sadipscing elitr, sed diam nonumy eirmod tempor
invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.</p>
</div>

<style>
.gift-box {
background-color: green;
display: flex;
align-items: stretch;
gap: 8px;
color: #fff;
padding: 8px;
}
.gift-box img {
display: block;
height: 100%;
}
</style>
<script>
let name = 'world';
</script>

<div class="gift-box">
<div>
<img alt="gift" src="data:image/svg+xml,%3Csvg%20xmlns=%22http://www.w3.org/2000/svg%22%20class=%22icon%20icon-tabler%20icon-tabler-gift%22%20width=%2224%22%20height=%2224%22%20viewBox=%220%200%2024%2024%22%20stroke-width=%222%22%20stroke=%22currentColor%22%20fill=%22none%22%20stroke-linecap=%22round%22%20stroke-linejoin=%22round%22%3E%3Cpath%20stroke=%22none%22%20d=%22M0%200h24v24H0z%22%20fill=%22none%22/%3E%3Cpath%20d=%22M3%208m0%201a1%201%200%200%201%201%20-1h16a1%201%200%200%201%201%201v2a1%201%200%200%201%20-1%201h-16a1%201%200%200%201%20-1%20-1z%22%20/%3E%3Cpath%20d=%22M12%208l0%2013%22%20/%3E%3Cpath%20d=%22M19%2012v7a2%202%200%200%201%20-2%202h-10a2%202%200%200%201%20-2%20-2v-7%22%20/%3E%3Cpath%20d=%22M7.5%208a2.5%202.5%200%200%201%200%20-5a4.8%208%200%200%201%204.5%205a4.8%208%200%200%201%204.5%20-5a2.5%202.5%200%200%201%200%205%22%20/%3E%3C/svg%3E" />

</div>
<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor
invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.consetetur sadipscing elitr, sed diam nonumy eirmod tempor
invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.</p>
</div>

<style>
.gift-box {
background-color: green;
display: flex;
align-items: stretch;
gap: 8px;
color: #fff;
padding: 8px;
}
.gift-box img {
display: block;
height: 100%;
}
</style>
it presents the same issue markboots describes, then paragraph needs adjusted. Also img may not be best in a nested div, but the example is there
EIO
EIO10mo ago
@Wonderbear There's thing with intrinsic sizing where unconsciously, we ask css to try and perform some magic without giving it enough stardust ✨ 🫠 So here, you have a div, whose width is pretty much 100% of the available parent. It is a flex element, which means all its children would by default have their width fitting their content. Hence, the width of your img would be the intrinsic width of the image (as shown in the svg url -> 24px) and same thing goes for the height. For the div which is the sibling in question, the content overflows the available width of the parent, hence the wrapping. Which means that it will fill the whole parent, leaving space for only the img This wrapping changes the height of the parent. Which also means that the smaller the width available to the div, the longer the height of the parent would be. Now, with a height of 100% on the img, you'd be shocked to hear this (maybe not 🌚 ), the img actually fills the 100% height. This is pretty much what align-items: stretch on the parent is already doing to the image. You can test this out by putting an outline on the image... outline: 2px dashed red However, what you are really looking for is for the image to visually fill the area its height covers (100%) You won't notice that because by default, img has object-fit: contain If you change this to any other thing, like object-fit: cover, you would see it fill the 100% height. Yet, this still isn't what you are going for (I presume). I believe you would want a case where you keep the initial style but the width of the image would expand proportionally to the height. By default css would try to make that work out for you. However, that is a lot of intrinsic sizing calculations to handle without a reference (Consider the loop scenario @MarkBoots talked about). So, css would give you the best possible option based on what you have defined. TLDR You have to define a height for the parent.
reddogg476
reddogg47610mo ago
this sort of works:
<script>
let name = 'world';
</script>

<div class="gift-box">
<div>
<img alt="gift" src="data:image/svg+xml,%3Csvg%20xmlns=%22http://www.w3.org/2000/svg%22%20class=%22icon%20icon-tabler%20icon-tabler-gift%22%20width=%2224%22%20height=%2224%22%20viewBox=%220%200%2024%2024%22%20stroke-width=%222%22%20stroke=%22currentColor%22%20fill=%22none%22%20stroke-linecap=%22round%22%20stroke-linejoin=%22round%22%3E%3Cpath%20stroke=%22none%22%20d=%22M0%200h24v24H0z%22%20fill=%22none%22/%3E%3Cpath%20d=%22M3%208m0%201a1%201%200%200%201%201%20-1h16a1%201%200%200%201%201%201v2a1%201%200%200%201%20-1%201h-16a1%201%200%200%201%20-1%20-1z%22%20/%3E%3Cpath%20d=%22M12%208l0%2013%22%20/%3E%3Cpath%20d=%22M19%2012v7a2%202%200%200%201%20-2%202h-10a2%202%200%200%201%20-2%20-2v-7%22%20/%3E%3Cpath%20d=%22M7.5%208a2.5%202.5%200%200%201%200%20-5a4.8%208%200%200%201%204.5%205a4.8%208%200%200%201%204.5%20-5a2.5%202.5%200%200%201%200%205%22%20/%3E%3C/svg%3E" />

</div>
<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor
invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.consetetur sadipscing elitr, sed diam nonumy eirmod tempor
invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.</p>
</div>

<style>
.gift-box {
background-color: green;
display: flex;
/* align-items: stretch; */
justify-content: space-between;
gap: 8px;
color: #fff;
padding: 8px;
}
.gift-box img {
display: block;
height: 100%;
/* z-index: 999; */
}
.gift-box p {
width: 70%
}
</style>
<script>
let name = 'world';
</script>

<div class="gift-box">
<div>
<img alt="gift" src="data:image/svg+xml,%3Csvg%20xmlns=%22http://www.w3.org/2000/svg%22%20class=%22icon%20icon-tabler%20icon-tabler-gift%22%20width=%2224%22%20height=%2224%22%20viewBox=%220%200%2024%2024%22%20stroke-width=%222%22%20stroke=%22currentColor%22%20fill=%22none%22%20stroke-linecap=%22round%22%20stroke-linejoin=%22round%22%3E%3Cpath%20stroke=%22none%22%20d=%22M0%200h24v24H0z%22%20fill=%22none%22/%3E%3Cpath%20d=%22M3%208m0%201a1%201%200%200%201%201%20-1h16a1%201%200%200%201%201%201v2a1%201%200%200%201%20-1%201h-16a1%201%200%200%201%20-1%20-1z%22%20/%3E%3Cpath%20d=%22M12%208l0%2013%22%20/%3E%3Cpath%20d=%22M19%2012v7a2%202%200%200%201%20-2%202h-10a2%202%200%200%201%20-2%20-2v-7%22%20/%3E%3Cpath%20d=%22M7.5%208a2.5%202.5%200%200%201%200%20-5a4.8%208%200%200%201%204.5%205a4.8%208%200%200%201%204.5%20-5a2.5%202.5%200%200%201%200%205%22%20/%3E%3C/svg%3E" />

</div>
<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor
invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.consetetur sadipscing elitr, sed diam nonumy eirmod tempor
invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.</p>
</div>

<style>
.gift-box {
background-color: green;
display: flex;
/* align-items: stretch; */
justify-content: space-between;
gap: 8px;
color: #fff;
padding: 8px;
}
.gift-box img {
display: block;
height: 100%;
/* z-index: 999; */
}
.gift-box p {
width: 70%
}
</style>
Though, does not fit with all para widths, the image creeps under wider. Looks good para width 50% thru 70%. ++ seeing this isn't responsive.... that one's tough 😄
MarkBoots
MarkBoots10mo ago
just tried to do it with a JS resize observer https://codepen.io/MarkBoots/pen/ExMJLBw?editors=1111 But i dont think you should go this route, it's asking for issues
EIO
EIO10mo ago
Exactly!
Want results from more Discord servers?
Add your server