I'm trying to make a card component in CSS, I thought it would be straightforward, but I'm struggling with CSS grid properties. Also, I can't use flexbox in that case, for reasons not worth explaining :)
The layout is dead simple: an image on the left and some text on the right.
<div class="card">
<figure card="card__img">
<img src="https://via.placeholder.com/600" width="600" height="600">
</figure>
<p class="card__txt">Lorem ipsum tempus fugit.</p>
</div>
Here's the tricky part: the image is optional, and when present, it should not be wider than 50%.
When I put a max-width to the image, then the text doesn't expand if the image is absent.
Here's what I have at the moment:
.card {
display: grid;
column-gap: 1em;
grid-template-columns: minmax(0, 50%) 1fr;
}
I tried to mess width grid-template-columns
, but I'm not familiar enought yet with it.
You can see it in action on this Codepen: https://codepen.io/tcharlss/pen/gVvOjY
And here's the code snippet:
/* Layout stuff I'm strugling width*/
.card {
display: grid;
column-gap: 1em;
grid-template-columns: minmax(0, 50%) 1fr;
}
/* Decoration & miscellaneous */
body { font-family: sans; }
.container {
display: grid;
column-gap: 1em;
grid-template-columns: repeat(auto-fill, 35em);
}
.container + .container {
margin-top: 2em;
}
.explication {
color: hsl(230, 100%, 45%);
font-family: monospace;
font-style: italic;
}
.card {
border: 1px solid hsl(230, 100%, 45%);
min-height: 18em;
}
.card__txt {
background-color: hsl(0, 0%, 90%);
margin: 0;
}
figure {
margin: 0;
}
img {
max-width: 100%;
height: auto;
}
<div class="container">
<div class="col">
<p class="explication">If an image is present, it can't be wider then 50%</p>
<div class="card">
<figure card="card__img">
<img src="https://via.placeholder.com/600" width="600" height="600">
</figure>
<p class="card__txt">Lorem ipsum tempus fugit. Aut rerum nostrum qui adipisci est aut. Est error omnis sit velit.</p>
</div>
</div>
<div class="col">
<p class="explication">Without image, the text should expand to fill all the available space</p>
<div class="card">
<p class="card__txt">Lorem ipsum tempus fugit. Aut rerum nostrum qui adipisci est aut. Est error omnis sit velit.</p>
</div>
</div>
</div>