I am trying to create two buttons that each toggle a specific column when using CSS grid.
I have a simply grid that toggles the width of first and last columns. (See snippet)
I want to be able to toggle the column widths independantly (Without further javascript).
Because the
activeclass is applied to the container each column cannot infer the active state of the other.With flex the active class would be applied to the column (e.g
item1) directly making this a non issue.
QUESTION
Is there a CSS grid column width property that can be applied to a column (e.g item1) overiding the settings in grid-template-columns?
OR
Is there a CSS grid property like grid-template-columns that can target individual columns rather than all columns in one property?
$("#js-btn-left").on("click", function () {
$(".grid-container").toggleClass("active");
});
$("#js-btn-right").on("click", function () {
$(".grid-container").toggleClass("active");
});
.grid-container {
display: grid;
grid-template-columns: 50px auto 50px;
grid-gap: 10px;
}
.grid-container > div {
background-color: #b00;
border:2px solid #000;
text-align: center;
padding: 20px 0;
font-size: 30px;
}
.grid-container.active{
grid-template-columns: 100px auto 100px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id="js-btn-left" type="button" value="TOGGLE COLUMN 1"/>
<input id="js-btn-right" type="button" value="TOGGLE COLUMN 3"/>
<div class="grid-container">
<div class="item1">1</div>
<div class="item2">2</div>
<div class="item3">3</div>
</div>