r/csshelp • u/trollmeannakendrick • Jul 26 '23
CSS - Grid Help
*Copied from Codepen
I have the grid format how I would like. The issue I'm running into is with images. I have a banner image that I set to span the width of the grid but am running into a few errors:
1) It's not spanning the width of the grid
2) It's impacting the grid item directly below it.
I put border around both issues to highlight.
<body>
<div class="grid-container">
<!--Row 1-->
<div class="header">
<span>Header</span>
</div>
<!--Row 2-->
<div class="banner">
<img src="https://hips.hearstapps.com/hmg-prod/images/dog-puppy-on-garden-royalty-free-image-1586966191.jpg?crop=1.00xw:0.669xh;0,0.190xh&resize=1200:\*">
</div>
<!--Row 3-->
<div class="img-1">1</div>
<div class="img-2">2</div>
<div class="img-3">3</div>
<div class="img-4">4</div>
<!--Row 4-->
<div class="boilerplate">
<span>Boilerplate</span>
</div>
</div>
</body>
html {
box-sizing: border-box;
}
.grid-container {
display: grid;
grid-template: 40px 220px 150px 40px / repeat(12, 1fr);
column-gap: 10px;
row-gap: 5px;
border: 1px solid black;
}
.header {
grid-area: 1 / 1 / 1 / span 12;
background-color: yellow;
}
.banner img {
grid-area: 2 / 1 / 2 / span 12;
max-height: 100%;
border: 5px solid red;
}
.img-1 {
grid-area: 3 / 1 / 3 / span 3;
background-color: gray;
color: #fff;
border: 5px solid blue;
}
.img-2 {
grid-area: 3 / 4 / 3 / span 3;
background-color: pink;
}
.img-3 {
grid-area: 3 / 7 / 3 / span 3;
background-color: red;
color: #fff;
}
.img-4 {
grid-area: 3 / 10 / 3 / span 3;
background-color: lime;
}
.boilerplate {
grid-area: 4 / 1 / 4 / span 12;
background-color: black;
color: #fff;
}
View all comments
Show parent comment
1
u/be_my_plaything Jul 26 '23
How are you wanting to get the image to fit?
The image isn't much wider than it is tall, but the banner (from your grid set-up) has a width of 100% (
span 12ofrepeat(12, 1fr)) and a fixed height of 220px. So in my monitor it is either stretched super wide to point it looks ridiculous(not using object-fit), or shrunk to fit the height but not the width then is just a small image on one size (contain) or has the height cropped to fill the width without losing aspect-ratio, but then only leaves a stripe across the middle (cover)... Basically these choices: https://codepen.io/NeilSchulz/pen/KKrGdRgAlternatively where you have...
...you could change 220px (the height of the banner) to 'auto' which would give it height dictated by the content (how tall the image is when at 100% width) although with the aspect ratio of the image this made the banner taller than my screen!