Reputation: 17
I am creating a card where it uses grid to displa data. I need to be able to allow a maximum of 8 columns but also allow the grid fill the space available if there is less than 8 columns.
Currently, I'm using grid-template-columns: repeat(8, auto); which fills the space nicely with 8 columns but if I have 6, for example, it leaves a space to the right.
*,
body,
html {
margin: 0;
box-sizing: border-box;
}
.card {
width: 100%;
height: 150px;
min-height: 100px;
background-color: #333333;
border-radius: 15px;
padding: 10px 10px;
overflow: hidden;
position: relative;
}
.gridWrapper {
display: grid;
grid-template-columns: repeat(8, auto);
grid-gap: 5px;
margin-bottom: 10px;
}
.gridItem {
display: flex;
flex-direction: column;
justify-content: center;
padding: 6px;
white-space: nowrap;
text-overflow: ellipsis;
overflow: hidden;
}
label {
font-family: Helvetica;
font-size: 0.8em;
color: #76a8b0;
font-weight: 100;
}
p {
color: #ffffff;
font-family: Helvetica;
font-weight: 100;
}
.cardFooter {
display: flex;
flex-direction: row;
justify-content: center;
padding: 10px 0 5px 0;
position: absolute;
background: #333333;
bottom: 0;
width: 100%;
}
<div class="card" id="card">
<div class="gridWrapper">
<div class="gridItem">
<label for="title">Label</label>
<p>ContentContentContent</p>
</div>
<div class="gridItem">
<label for="title">Label</label>
<p>Content</p>
</div>
<div class="gridItem">
<label for="title">Label</label>
<p>Content</p>
</div>
<div class="gridItem">
<label for="title">Label</label>
<p>Content</p>
</div>
<div class="gridItem">
<label for="title">Label</label>
<p>Content</p>
</div>
<div class="gridItem">
<label for="title">Label</label>
<p>Content</p>
</div>
<!-- <div class="gridItem">
<label for="title">Label</label>
<p>Content</p>
</div>
<div class="gridItem">
<label for="title">Label</label>
<p>Content</p>
</div> -->
</div>
</div>
I've created a jsfiddle at https://jsfiddle.net/eh7g8Lx1/1/
Upvotes: 0
Views: 1295
Reputation: 17
I managed to fix this with;
grid-template-columns: repeat(auto-fit, 12%);
Upvotes: 0
Reputation: 334
Replace grid-template-columns: repeat(8, auto);
line with
grid-template-columns: repeat(auto-fit, minmax(1rem,1fr));
auto-fit value wil solve your problem
Upvotes: 2