I want to design News/Blog page using grid display:grid
and i am showing only 1 news item in first row and 3 or 2 or 1 in other rows based on screen size which works fine but issue i am facing is that when i want to give first image a max-height:200px then it squeezed, i want to achieve something live we do with background image where image shows without stretching or squeezing.
can i do something like that in this case. codepen link
or only way to do it is to wrap image inside a div and use image as background.
body {
background: #001628;
color: #777;
padding: 4em;
font-family: sans-serif;
line-height: 1.15;
}
hr { /*dummy content*/
height: 6px;
border: none;
background: rgba(0, 0, 0, 0.1);
}
hr:last-child { /*dummy content*/
margin-right: 60%;
}
hr.image { /*dummy content*/
padding-bottom: 50%;
}
.image {position:relative; width:100%;}
.page {
padding: 2em;
background: #cecece;
max-width: 1100px;
}
.archive {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
grid-gap: 1em;
grid-auto-flow: dense;
}
.article {
padding: 1em;
background: #fff;
box-shadow:
0 5px 10px rgba(0, 0, 0, 0.1),
0 20px 20px rgba(0, 0, 0, 0.05);
}
.article:nth-child(1) .image{
max-height:200px;
}
.article:nth-child(1) {
grid-column: 1 / -1;
background: rgba(200,200,255,0.3)
}
.article:nth-child(1)>hr:first-child {
padding-bottom: 30%
}
.article:nth-child(1)>hr:last-child {
margin-right: 20%;
}
/*
.article:nth-child(1):after {
content: "grid-column: 1 / -1";
}
.article:nth-child(8) {
grid-column: -3 / -1;
background: rgba(255,200,255,0.3)
}
.article:nth-child(8):after {
content: "grid-column: -3 / -1";
}
.article:nth-child(12) {
grid-column: 1 / -2;
background: rgba(255,200,255,0.3)
}
.article:nth-child(12)>hr:first-child {
padding-bottom: 30%
}
.article:nth-child(12):after {
content: "grid-column: 1 / -2";
}
*/
.article:after {
color: #808;
font-weight: bold;
font-family: courier;
}
@media (max-width: 680px) {
.archive {
display: flex;
flex-direction: column;
}
.article {
margin-bottom: 1em;
}
}
<div class="page">
<h1>NEWS</h1>
<div class="archive">
<article class="article">1
<img src="https://dummyimage.com/600x400/ebf52c/ff0000" class="image">
<hr>
<hr>
</article>
<article class="article">2
<img src="https://dummyimage.com/600x400/ebf52c/ff0000" class="image">
<hr>
<hr>
<hr>
</article>
<article class="article">3
<img src="https://dummyimage.com/600x400/ebf52c/ff0000" class="image">
<hr>
<hr>
<hr>
</article>
<article class="article">4
<img src="https://dummyimage.com/600x400/ebf52c/ff0000" class="image"><hr>
<hr>
<hr>
<hr>
</article>
<article class="article">5
<img src="https://dummyimage.com/600x400/ebf52c/ff0000" class="image">
<hr>
<hr>
<hr>
</article>
<article class="article">6
<img src="https://dummyimage.com/600x400/ebf52c/ff0000" class="image">
<hr>
<hr>
<hr>
</article>
<article class="article">7
<img src="https://dummyimage.com/600x400/ebf52c/ff0000" class="image">
<hr>
<hr>
<hr>
</article>
<article class="article">8
<img src="https://dummyimage.com/600x400/ebf52c/ff0000" class="image">
<hr>
<hr>
</article>
<article class="article">9
<img src="https://dummyimage.com/600x400/ebf52c/ff0000" class="image">
<hr>
</article>
<article class="article">10
<img src="https://dummyimage.com/600x400/ebf52c/ff0000" class="image">
<hr>
<hr>
</article>
<article class="article">11
<img src="https://dummyimage.com/600x400/ebf52c/ff0000" class="image">
<hr>
<hr>
<hr>
</article>
<article class="article">12
<img src="https://dummyimage.com/600x400/ebf52c/ff0000" class="image">
<hr>
<hr>
</article>
<article class="article">13
<img src="https://dummyimage.com/600x400/ebf52c/ff0000" class="image">
<hr>
</article>
<article class="article">14
<img src="https://dummyimage.com/600x400/ebf52c/ff0000" class="image">
<hr>
<hr>
<hr>
<hr>
</article>
<article class="article">15
<img src="https://dummyimage.com/600x400/ebf52c/ff0000" class="image">
<hr>
</article>
</div>
</div>