Reputation: 807
I want to have a css grid as follows.
A | 1 | 2 | 3 |
---|---|---|---|
4 | 5 | 6 | |
7 | 8 |
A is like the header and numbers (1 to 8) are data that comes from an API. I want to have A as the same height as numbers so I cannot use grid-row: 1/-1
. I don't want to add an empty cell in every 4th item because I'm using AlpineJS x-for and it only supports only one child element.
body {
max-width: 1280px;
margin: 2rem auto;
}
.tier-grid {
display: grid;
grid-template-columns: repeat(4, minmax(0, 1fr));
gap: 1rem;
}
.card {
display: flex;
flex-direction: column;
align-items: center;
background-color: #F1F1F1;
border-radius: 0.2rem;
padding: 2rem 1rem;
}
.card button {
margin-top: auto;
}
.btn {
cursor: pointer;
font-size: 1rem;
border: none;
padding: 0.5rem 2rem;
}
.btn:hover {
opacity: 0.8;
}
.btn-primary {
background: #6944CE;
color: white;
border-radius: 1rem;
}
.info {
border-radius: 0.2rem;
overflow: hidden;
display: flex;
flex-direction: column;
}
.info p {
margin: auto;
}
.info-footer {
margin-top: auto;
background: #6944CE;
color: white;
padding: 1rem;
text-align: center;
}
<div class="tier-grid">
<div class="card">
<h1>Tier 1</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolore veritatis, excepturi, laudantium optio,
molestiae quo cumque illo harum laborum dignissimos nisi ut et modi dolor minus ex dolores voluptate. Unde.</p>
<button class="btn btn-primary">Learn More</button>
</div>
<div class="info">
<img src="https://via.placeholder.com/150" alt="Placeholder Image" />
<span class="info-footer">Buy Now</span>
</div>
<div class="info">
<img src="https://via.placeholder.com/150" alt="Placeholder Image" />
<span class="info-footer">Buy Now</span>
</div>
<div class="info">
<img src="https://via.placeholder.com/150" alt="Placeholder Image" />
<span class="info-footer">Buy Now</span>
</div>
<div class="info">
<p>I want this to be an empty cell.</p>
<span class="info-footer">Buy Now</span>
</div>
<div class="info">
<img src="https://via.placeholder.com/150" alt="Placeholder Image" />
<span class="info-footer">Buy Now</span>
</div>
<div class="info">
<img src="https://via.placeholder.com/150" alt="Placeholder Image" />
<span class="info-footer">Buy Now</span>
</div>
<div class="info">
<img src="https://via.placeholder.com/150" alt="Placeholder Image" />
<span class="info-footer">Buy Now</span>
</div>
<div class="info">
<p>I want this to be an empty cell.</p>
<span class="info-footer">Buy Now</span>
</div>
<div class="info">
<img src="https://via.placeholder.com/150" alt="Placeholder Image" />
<span class="info-footer">Buy Now</span>
</div>
</div>
Upvotes: 0
Views: 611
Reputation: 1823
What about something like this:
.info:nth-child(3n) {
border: 3px solid red;
grid-column: 3;
}
.info:nth-child(3n+1) {
border: 3px solid blue;
grid-column: 4;
}
.info:nth-child(3n+2) {
border: 3px solid green;
grid-column: 2;
}
This will allow you to place the info items on your desired column without needing to add any extra "dummy" content element for the "empty cells"
body {
max-width: 1280px;
margin: 2rem auto;
}
.tier-grid {
display: grid;
grid-template-columns: repeat(4, minmax(0, 1fr));
gap: 1rem;
}
.card {
display: flex;
flex-direction: column;
align-items: center;
background-color: #F1F1F1;
border-radius: 0.2rem;
padding: 2rem 1rem;
}
.card button {
margin-top: auto;
}
.btn {
cursor: pointer;
font-size: 1rem;
border: none;
padding: 0.5rem 2rem;
}
.btn:hover {
opacity: 0.8;
}
.btn-primary {
background: #6944CE;
color: white;
border-radius: 1rem;
}
.info {
border-radius: 0.2rem;
overflow: hidden;
display: flex;
flex-direction: column;
}
.info:nth-child(3n) {
border: 3px solid red;
grid-column: 3;
}
.info:nth-child(3n+1) {
border: 3px solid blue;
grid-column: 4;
}
.info:nth-child(3n+2) {
border: 3px solid green;
grid-column: 2;
}
.info p {
margin: auto;
}
.info-footer {
margin-top: auto;
background: #6944CE;
color: white;
padding: 1rem;
text-align: center;
}
<div class="tier-grid">
<div class="card">
<h1>Tier 1</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolore veritatis, excepturi, laudantium optio, molestiae quo cumque illo harum laborum dignissimos nisi ut et modi dolor minus ex dolores voluptate. Unde.</p>
<button class="btn btn-primary">Learn More</button>
</div>
<div class="info">
<img src="https://via.placeholder.com/150" alt="Placeholder Image" />
<span class="info-footer">Buy Now</span>
</div>
<div class="info">
<img src="https://via.placeholder.com/150" alt="Placeholder Image" />
<span class="info-footer">Buy Now</span>
</div>
<div class="info">
<img src="https://via.placeholder.com/150" alt="Placeholder Image" />
<span class="info-footer">Buy Now</span>
</div>
<div class="info">
<img src="https://via.placeholder.com/150" alt="Placeholder Image" />
<span class="info-footer">Buy Now</span>
</div>
<div class="info">
<img src="https://via.placeholder.com/150" alt="Placeholder Image" />
<span class="info-footer">Buy Now</span>
</div>
<div class="info">
<img src="https://via.placeholder.com/150" alt="Placeholder Image" />
<span class="info-footer">Buy Now</span>
</div>
<div class="info">
<img src="https://via.placeholder.com/150" alt="Placeholder Image" />
<span class="info-footer">Buy Now</span>
</div>
<div class="info">
<img src="https://via.placeholder.com/150" alt="Placeholder Image" />
<span class="info-footer">Buy Now</span>
</div>
<div class="info">
<img src="https://via.placeholder.com/150" alt="Placeholder Image" />
<span class="info-footer">Buy Now</span>
</div>
<div class="info">
<img src="https://via.placeholder.com/150" alt="Placeholder Image" />
<span class="info-footer">Buy Now</span>
</div>
<div class="info">
<img src="https://via.placeholder.com/150" alt="Placeholder Image" />
<span class="info-footer">Buy Now</span>
</div>
<div class="info">
<img src="https://via.placeholder.com/150" alt="Placeholder Image" />
<span class="info-footer">Buy Now</span>
</div>
</div>
Upvotes: 2
Reputation: 366
Does adding
.card {
...
grid-row: 1 / span 3;
}
get what you want? I'm trying to see if you can make this dynamic and just fill all the columns instead of specifying how many you have but I want to check first if this is in the right direction.
Upvotes: 0