Sub topic 7
The bottom tab bar
A bottom tab bar is a fixed row of three to five links pinned to the bottom edge of the screen. It keeps the main destinations visible and within thumb reach. This chapter builds one and covers the errors that most often break it.
This chapter assumes you know semantic HTML, flexbox, and media queries. It also assumes you have read the earlier chapters on touch targets and semantic nav markup.
Why a tab bar works on phones
A hamburger menu hides every destination behind one tap. A tab bar shows them all the time. Users can see where they are and switch sections with one tap, and the bottom edge is the easiest part of a large phone to reach with a thumb.
The trade-off is space. The bar permanently uses part of the viewport, and it only fits a few items. If your site has more than five top-level destinations, a tab bar alone will not fit them.
The markup
Start with a nav element that has a label, and use real links. Each tab goes to a different page or section, so links are correct here. Buttons would not be.
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
<nav class="tab-bar" aria-label="Primary">
<a href="/home" aria-current="page">Home</a>
<a href="/search">Search</a>
<a href="/orders">Orders</a>
<a href="/account">Account</a>
</nav>
<style>
.tab-bar {
position: fixed;
left: 0;
right: 0;
bottom: 0;
display: flex;
background: #fff;
border-top: 1px solid #ccc;
padding-bottom: env(safe-area-inset-bottom);
}
.tab-bar a {
flex: 1;
min-height: 48px;
display: flex;
align-items: center;
justify-content: center;
}
.tab-bar a[aria-current="page"] {
font-weight: bold;
}
</style>How each part works
position: fixedwithleft,right, andbottomset to 0. This pins the bar to the bottom of the viewport and stretches it across the full width.display: flexon the bar andflex: 1on each link. Every tab gets an equal share of the width. Four tabs on a 360px screen are 90px wide each, which is enough for a comfortable tap.min-height: 48px. This keeps each target tall enough to tap reliably. The innerdisplay: flexwithalign-itemsandjustify-contentcenters the label in that area.aria-current="page". This tells screen readers which tab is the current page. The bold weight is the visual cue. Do not rely on color alone, because some users cannot tell the difference.env(safe-area-inset-bottom). Some phones have a home indicator or rounded corners at the bottom. This value adds padding so the tabs are not covered.
Common errors
Content hidden behind the bar
A fixed element is removed from the normal flow. The last part of your page ends up underneath the bar and cannot be scrolled into view. Reserve space at the bottom of the page:
body {
padding-bottom: calc(48px + env(safe-area-inset-bottom));
}Use the same height as the bar, including the safe-area padding.
Using tabs for actions
Tabs are for navigation between sections. Do not put actions such as "Add to cart" in the bar. Use a button elsewhere on the page for those.
Too many tabs
Five is a practical limit. At six tabs on a 360px screen, each is 60px wide and labels begin to wrap or truncate. If you need more destinations, put the less frequent ones in an "Account" or "More" section, or combine the tab bar with another pattern, which a later chapter covers.
Icons without labels
An icon alone is ambiguous. If you add icons, keep the text label visible. If you must hide the text visually, keep it available to screen readers.
Showing the bar on desktop
A fixed bar at the bottom of a wide window looks out of place and wastes space. Switch to a different layout at larger widths:
@media (min-width: 48em) {
.tab-bar {
position: static;
border-top: 0;
padding-bottom: 0;
}
.tab-bar a {
flex: 0 0 auto;
padding: 0 1rem;
}
body {
padding-bottom: 0;
}
}This turns the bar into a normal row in the page flow. A header placement is also common, and the pattern-combining chapter covers it.
How the pieces fit
Keyboard and focus
Because the tabs are links, they work with the keyboard without extra code. Make sure the focus style is visible. Do not remove the outline unless you replace it with something equally clear. Also check that the fixed bar does not cover a focused element when the browser scrolls it into view. The bottom padding on body usually prevents this.
Exercise
A tab bar has six links on a 360px-wide phone. The last paragraph of each page is hidden behind the bar, and on a phone with a home indicator the tab labels are partly covered. Name the fixes for these two problems and one change that addresses the six links.
Show answer
Add bottom padding to the body equal to the bar height plus the safe-area inset, so the last content can scroll above the bar. Add viewport-fit=cover to the viewport meta tag so env(safe-area-inset-bottom) returns a real value, and keep padding-bottom: env(safe-area-inset-bottom) on the bar. For the six links, cut the bar to five or fewer by moving the least-used destination into another section such as Account or More.
Exercise
Why is aria-current="page" used on the active tab instead of only making its text bold?
Show answer
Bold text is visible only to sighted users. aria-current="page" tells screen readers which link is the current page, so the state is available to everyone.