'CSS grid styling based on auto-fit/cell position
Is it possible to style a CSS grid cell based on auto-fit/cell position?
Here is the UI. The very first grid item has a border to the right to separate/indicate it is the selected time zone.
And here is the CSS for it. Each cell is assigned the .preview class and only the first cell is assigned the .selected-item class.
.date-time-grid {
display: grid;
grid-template-rows: repeat(1, 1fr);
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
gap: 1rem;
height: 100%;
text-align: center;
.preview {
padding: 0.3rem;
&.selected-item {
background-color: rgba(255, 255, 255, 0.09);
border-right: 2px solid white;
}
}
}
When the user resizes the screen the grid auto-fits the cells.
Is it possible to get rid of the right border by detecting that the first cell now sits alone on the first row?
Solution 1:[1]
You can set a cookie that expires at the end of the day when a user visits the site.
document.cookie = `visited=true; expires=${dateObjectForEndOfDay}`;
Then, when a client visits the page you can read the cookie and see if it is set. Expired cookies won't display, so if the last cookie has expired it will be as if the user had never visited the site. (The below code is pinched from w3Schools
function getCookie(cname) {
let name = cname + "=";
let decodedCookie = decodeURIComponent(document.cookie);
let ca = decodedCookie.split(';');
for(let i = 0; i <ca.length; i++) {
let c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
const siteWasVisited = getCookie('visited');
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Tom |


