Burns
Burns

Reputation: 53

Responsive css grid - how to make first item 2 x width and responsive

I have made the following with CSS grid:

https://i.sstatic.net/rAtiA.jpg (animated gif showing current behaviour)

This the behaviour I want, except I want the first image to be 2 times the width (ie 3 cols with the first item spanning 2 cols, as in the mock below):

enter image description here

This is the code I have so far

.news-container {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(441px, 1fr));
    margin-top:2rem;
    column-gap: 2.33em;
    row-gap:  2.33em;

}
.news-item:first-child {
    grid-column:  1 2;
  }

.news-item {
position: relative;
border-radius: 10px;
border: 5px solid #fff;
color: #fff;
-webkit-box-shadow: 0px 0px 16px -1px rgba(0,0,0,0.11);
-moz-box-shadow: 0px 0px 16px -1px rgba(0,0,0,0.112);
box-shadow: 0px 0px 16px -1px rgba(0, 0, 0, 0.11);

}

Is it possble ?

Thanks

Upvotes: 0

Views: 634

Answers (2)

BobRodes
BobRodes

Reputation: 6165

If you have just the two photos, and you want the second one to wrap onto a new line when you shrink the screen, it's easier to use a flex box:

.container {
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
}

.container div {
  box-sizing: border-box;
  padding: 20px;
  background-color: green;
  color: white;
  font: 24px sans-serif;
}

.one {
  flex-basis: 441px;
  flex-grow: 2;
}

.two {
  flex-basis: 220px;
  flex-grow: 1;
}
<div class="container">
  <div class="one">One</div>
  <div class="two">Two</div>
</div>

You can experiment with flex-grow, flex-basis and max-width if you don't want your second picture to take up the whole width when it bumps to the next line.

As far as I know, the only way to get this behavior with a grid, unless the cells are all the same size, is to use media queries and respecify the grid rectangle directly for the smaller screen.

Upvotes: 1

Ozone
Ozone

Reputation: 1353

grid-area can do this. For example:

.grid-container {
  display: grid;
  grid-template-columns: auto auto auto auto;
  grid-gap: 10px;
  background-color: #2196F3;
  padding: 10px;
}

.grid-container > div {
  background-color: rgba(255, 255, 255, 0.8);
  text-align: center;
  padding: 20px 0;
  font-size: 30px;
  height: 50px;
}

.item1 {
  grid-area: 1 / 1 / span 2 / span 3;
}
<div class="grid-container">
  <div class="item1">1</div>
  <div class="item2">2</div>
</div>

Upvotes: 0

Related Questions