For the task at hand, my goal is to showcase the final 3 elements of a container in a horizontal alignment. In this setup, the first two items are allotted fixed space while the third element expands to fill the remaining columns.
To accomplish this layout, I have been experimenting with CSS Grid properties.
Currently, I've managed to create a functional prototype when the grid contains more than 3 elements.
However, as illustrated in the code snippet below, there is an issue if the number of elements is less than 3. The last column reaches the right end of the grid but leaves an empty space.
I attempted solving this by using grid-column: auto / -1;
instead of grid-column-end
, but without success.
This problem was encountered on Chrome with testing also conducted on Firefox.
What could be causing this gap and is there a CSS-only solution to rectify it?
EDIT : Completed the scenario and added expected result
.demo {
max-width: 500px;
background-color: #abcdef;
border-radius: 3px;
padding: 5px;
margin-bottom: 5px;
}
.demo>.demo {
background-color: #fedcba;
}
.main {
display: grid;
grid-template-columns: repeat(2, 100px) 1fr;
grid-gap: 1px;
}
.main>.column:last-child {
grid-column-end: -1;
}
.main>.column:nth-last-child(n+4) {
display: none;
}
<div class="demo main">
<div class="demo column">1</div>
<div class="demo column">2</div>
<div class="demo column">3</div>
<div class="demo column">4</div>
</div>
<div class="demo main">
<div class="demo column">1</div>
<div class="demo column">2</div>
<div class="demo column">3</div>
</div>
<div class="demo main">
<div class="demo column">1</div>
<div class="demo column">2</div>
</div>
<div class="demo main">
<div class="demo column">1</div>
</div>
Expected outcome :