No JS: Tabs That Scale Down to Menu

Radio Toggles

In this demo, labels for hidden radios toggle the content. This is based on the behavior in which clicked labels for a radio or checkbox input will check that input.

<input id="radio-1" type="radio" name="demo-radios">
<input id="radio-2" type="radio" name="demo-radios">
#radio-1: #radio-2:
<label for="radio-1">Toggle #radio-1</label>
<label for="radio-2">Toggle #radio-2</label>

Click one of the labels above and see its effect on the radios above it.

The radios for this pen's tabs are displayed semi-transparently at the top of this demo page.

Input :checked

In CSS, you can query based on the :checked selector for radios and checkboxes to style siblings down the DOM scope. To do this, we can use the ~. It will select same-level siblings after the given selector. Because the tab labels in this demo are nested and not immediate siblings, we will need to select their topmost parent that is at the same level as our input.

To demonstrate, we will do a simplified version of this with a checkbox:

<!-- invisible input and its label -->
<input id="demo-child-toggle" type="checkbox">
<label for="demo-child-toggle">Toggle #demo-child</label>

<-- parent to select first via "~" -->
<div id="demo-parent">
  <-- child to select through parent -->
  <div id="demo-child">#demo-child</div>
</div>

and in our CSS:

/* hiding our checkbox */
#demo-child-toggle {
  display: none;  
}
/* selecting the child */
#demo-child-toggle:checked ~ #demo-parent #demo-child {
  color: #c0392b;
  font-weight: bold;
  text-transform: uppercase;
}

#demo-child

As you can see, we can control the style of content that comes after a hidden input by toggling it via its label.

At this point you can probably get the picture for how we can conditionally display the tabbed panel content in this pen.

The Tabs

Here is the basic form of a tab in this demo:

<li id="li-for-panel-1">
  <label class="panel-label" for="panel-1-ctrl">CSS Radio Toggles</label>
</li>

For the "active" tab to cover the bottom border, the child label gets an additional 2 pixels of padding-top while its parent li gets a translateY(1px). This not only covers the bottom border, but gives an ever-so-subtle "moving toward you" effect by shifting the title down 1px.

#panel-1-ctrl:checked ~ #tabs-list #li-for-panel-1 {
  transform: translate(0, 1px);
}
#panel-1-ctrl:checked ~ #tabs-list #li-for-panel-1 label.panel-label {
  padding-top: 26px; /* instead of "24px" */
}

Tab :hover

When designing the :hover and "active" states I had a dilemma.

<li id="li-for-panel-1">
  <label class="panel-label" for="panel-1-ctrl">CSS Radio Toggles</label>
</li>

Each tab li has a border-right. But when the additional border-top appears, we dont want the lighter border-right to be shown all the way to the top. The fix for this is to cancel the border-right on both the :hover and "active" state as well as style the li's next sibling's border-left.

To do this, we can use a combination of the siblings after ~ and sibling next + selectors:

/* remove the right border on "active" state */
#panel-1-ctrl:checked ~ #tabs-list #li-for-panel-1 {
  border-right: none;
}
/* override that if the last child (we still want the border) */
#panel-1-ctrl:checked ~ #tabs-list #li-for-panel-1:last-child {
  border-right: 1px solid #dfdfdf;
}
/* add left to next sibling */
#panel-1-ctrl:checked ~ #tabs-list #li-for-panel-1 + li {
  border-left: 1px solid #dfdfdf;
}

Menu

On small screens, the tabs fold down into an expandable menu. To trigger the menu, I use a checkbox (note that it appears at the top of the screen on smaller screen sizes). There are two labels that trigger this checkbox. One opens and the other closes the menu. The one that opens is absolutely positioned invisibly over the "active" menu item. The closing label is at the bottom of the open menu.

The best way I have found to show and hide content without using absolute positioning is to use a combination of max-height and opacity. When "inactive", the content has a max-height: 0 and opacity: 0.

It also has a transition: opacity when I don't know the future height (this panel's content for example) and transition: opacity, max-height when I do know the future height (like the menu). When "active", the max-height and opacity get positive values and the content will transition in. I'm sure flexbox could get me around this hack, but this works for now.