I’m trying to make tabs at the top of a div, and I’m trying to make whichever tab has the .selected class be taller than the rest, but for some reason the selected tab is leaving the container div instead of using the space it has, which I’m pretty sure is the same height as the button including padding and margin.
CSS:
.tabContainer {
padding: 0;
padding-left: 10px;
margin-bottom: 0;
height: 35px;
}
.tab {
border: 0;
background: #88B7B5;
border-top-left-radius: 5px;
border-top-right-radius: 5px;
padding: 6px;
font-size: 15px;
height: 30px;
margin-top: 5px;
margin-bottom: 0px;
}
.tab.selected {
height: 35px;
margin-top: 0px !important;
transition: height 0.2s, margin-top 0.2s;
}
.tab:hover {
background: #98cdca;
transition: background 0.2s;
}
.tabContentsContainer { margin-top: 0; }
.tabContents {
padding: 10px;
background: #A7CAB1;
border-radius: 10px;
}
HTML:
<div class="tabContainer">
<button class="tab selected" data-tabgroup="main" data-tab="inventory">Inventory</button>
<button class="tab" data-tabgroup="main" data-tab="location">Location</button>
<button class="tab locked" data-tabgroup="main" data-tab="research">???</button>
<button class="tab locked" data-tabgroup="main" data-tab="recruits">???</button>
<button class="tab locked" data-tabgroup="main" data-tab="training">???</button>
</div>
<div class="tabContentsContainer">
<div class="tabContents" data-tabgroup="main" data-tab="inventory">
<h1>Inventory</h1>
</div>
</div>
3
Answers
Try using flex-box or grid (if not familiar, check out some of the tutorials) it will make your coding much easier and much more responsive. Also, don’t use static units like Pixels, instead use em or rem. One of the reasons your tab is going outside of the div is because you didn’t specify the width of it, therefore it is inheriting (default).
You just need to use the hover selector & transition property properly 🙂
But first add a space between the CSS classes
to change the properties of the tab class on hover the transition property must be set in the original CSS selector & the attribute being transitioned in the
:hover
selector