I'm creating a navbar made with CSS grid.
I decided making it with grid so I can re-arrange the sections (items) without modifying the html (just modify the CSS).
Then I created the grid with 3 areas: logo, menus, toggler.
And I added a bit of gap so each items won't stick together.
So far so good, until I tried to remove one/some sections, the gap still there even if the section was gone.
Then I tried to remove the gap and replace with a margin to each section. But the margin won't collapse at the beginning/ending of the grid. It behave differently than on regular block element.
I know it's more practical using a flexbox rather than a grid, but I prefer the grid because the section can be re-arranged without modifying the html. It's possible to move the logo at the top or somewhere else. Something impossible with flex.
Anyone can solve my grid-gap problem? Or maybe you've a different approach for creating the navbar?
See my sandbox:
.navbar {
background: pink;
display: grid;
grid-template-rows: auto;
grid-template-columns: max-content auto max-content;
grid-template-areas: "logo menus toggler";
justify-items: stretch;
align-items: stretch;
column-gap: 20px;
}
.logo {
background: green;
grid-area: logo;
width: 60px;
}
.menus {
background: lightsalmon;
grid-area: menus;
display: flex;
flex-direction: row;
justify-content: start;
}
.menus * {
padding: 5px;
}
.toggler {
background: lightskyblue;
grid-area: toggler;
}
<p>navbar with complete sections:</p>
<nav class="navbar">
<div class="logo">
LoGo
</div>
<div class="menus">
<div>menu-1</div>
<div>menu-2</div>
<div>menu-3</div>
</div>
<div class="toggler">
X
</div>
</nav>
<hr>
<p>navbar without logo:</p>
<nav class="navbar">
<div class="menus">
<div>menu-1</div>
<div>menu-2</div>
<div>menu-3</div>
</div>
<div class="toggler">
X
</div>
</nav>
<hr>
<p>navbar without toggler:</p>
<nav class="navbar">
<div class="logo">
LoGo
</div>
<div class="menus">
<div>menu-1</div>
<div>menu-2</div>
<div>menu-3</div>
</div>
</nav>
