Im using css grid.
I have 3 column which their total width is 100%
There is a button that clicking on it shows or hides the third column
I want that as soon as the third column is hidden, the width of the second column will be up to the end of the screen, the width of the second column and the width of the third column
<div class="container">
<div class="leftNav">left nav</div>
<div class="center">center</div>
<div id="right" class="rightNav">right nav</div>
</div>
<button onclick="myFunction()">Click Me</button>
function myFunction() {
var x = document.getElementById("right");
if (x.style.display === "none") {
x.style.display = "block";
} else {
x.style.display = "none";
}
}
.container {
display: grid;
grid-template-columns: 1fr auto 1fr;
}
.leftNav{
background-color:red;
}
.center{
background-color:yellow;
}
.rightNav{
background-color:green;
}
Im using css grid.
I have 3 column which their total width is 100%
There is a button that clicking on it shows or hides the third column
I want that as soon as the third column is hidden, the width of the second column will be up to the end of the screen, the width of the second column and the width of the third column
3
Answers
I believe this is what you are looking for. I’ve commented the changes.
Instead of hiding the button, toggle a class on the container then hide it using CSS and at the same time change the grid structure
You can try a simple fix of remixing one line of your CSS for the
.container
class, which is from this:to this:
This makes it so the second column is always trying to take up what’s available, and the other columns take up as much as is required by its content. If you want to make the left and right a certain width, you can do that by adding the styles to the
.leftNav
and.rightNav
classes respectively. e.g:the full code: