<meta name="viewport" content="width=device-width, initial-scale=1.0">
h1 {
font-size: 2rem;
@media(with >= 40em) {
// 640px
font-size: 3rem;
}
@media(with >= 60em) {
// 960px
font-size: 4rem;
}
}
img {
max-width: 100%; /* Prevent image being larger that its container. */
height: auto; /* Scale height automatically to preserve the aspect ratio */
}
<picture>
<!-- Use this pic if width is 40em(640px) -->
<source media="(min-width: 40em)" srcset="cat-1920.jpg">
<img src="crop.jpg" /><!-- Fallback image and also happens to be cropped image which give a zoom in effect. -->
</picture>
.cards {
display: grid,
grid-template-columns: repeat(4, 1fr); /* 4 cols */
gap: 12px
grid-auto-rows: 1fr;
}
.card { ... }
.cards {
display: grid,
grid-template-columns: 1fr; /* 1 cols */
gap: 14px
grid-auto-rows: 1fr;
@meda (width >=40em) {
grid-template-columns: repeat(2, 1fr); /* 2 cols */
}
@meda (width >=60em) {
grid-template-columns: repeat(4, 1fr); /* 2 cols */
}
}
/*Or, without media queries.
Let browser decide how many columns do you want at any given viewport
*/
.cards {
display: grid,
grid-template-columns: repeat(auto-fit, minmax(15rem, 1fr)); /* 15rem=240px */
gap: 14px
grid-auto-rows: 1fr;
}
flex: 1
on the items to make all items to have the same width..cards {
display: flex,
flex-wrap: wrap; /*Last item on last row will take full width.*/
}
.card {
flex: 1; /* flex: 0 0 240px -> min width is 240px */
}
.cards {
display: flex,
flex-wrap: wrap; /*Last item on last row will take full width.*/
gap: 14px;
}
.card {
flex: 100%; /* 1 column only */
@meda (width >=40em) {
/* 2 coluns. Logically it should be 50% but we need to factor in
the 14 px gap(14/2)=7
*/
flex: calc(50% - 7px);
}
@meda (width >=60em) {
flex: 23%; /* calc(25% - ?px); */
}
}
object-fit
has effect only if you let the browser the width and height of the image.
In my case, I used max-width
and height
.
.imgItself {
max-width: 100%; /* Prevent image being larger that its container. */
height: auto; /* Scale height automatically to preserve the aspect ratio */
object-fit: contain;
}