Creating Tabbed Navigation with CSS - A Step-by-Step Guide

I am looking to create tab contents using CSS. I have managed to create a custom tab style, but I am struggling to style its content with CSS. Can anyone provide guidance on how I can achieve this?

CSS:

.tabs {
    list-style: none;
    margin: 0;
    padding: 0;
    position: relative;
}
.tabs li {
    margin: 0 2px;
    padding: 10px;
    cursor: pointer;
    background: white;
    display: inline-block;
}
.tabs li:not(.active):hover
{
    background: #ccc;
    border-top-right-radius: 4px;
    border-top-left-radius: 4px;
}
.tabs li > a
{   
    text-decoration: none;
    color: gray
}
.tabs li.active
{
    z-index: 1000;
    border-top-right-radius: 4px;
    border-top-left-radius: 4px;
    border: 1px solid #ccc;
    border-bottom-color: #fff;
    color: grey;
    cursor: default;
}
.tabs:after
{
    position: absolute;
    content: "";
    width: 100%;
    z-index: 1;
    bottom: 0;
    left:0;
    border-bottom: 1px solid #ddd;
}
.tabs:before
{
    z-index: 1;
}

HTML:

<ul class="tabs">
            <li class="active"><a href="#tab1">Home</a></li>
            <li><a href="#tab2">Contact</a></li>
            <li><a href="#tab3">Services</a></li>
        </ul>

Fiddle

Answer №1

Here's what you need:

body {
margin: 0;
background-color: #5E6EBF;
}
.tab-menu {
margin: 20px auto;
padding: 0 76px;
}
.tabs {
width: 150px;
display: inline-block;
position: relative;
height: 45px;
}
.tab-content {
display: none;
}
.tabs input {
margin: 0;
position: absolute;
width: 100%;
height: 100%;
z-index: 1;
cursor: pointer;
opacity: 0;
}
.tabs label {
position: absolute;
left: 0;
right: 0;
bottom: 0;
top: 0;
text-align: center;
line-height: 40px;
transition: all 0.3s ease;
background-color: #ffffff;
border-top-right-radius: 5px;
border-top-left-radius: 5px;
}
.tab-content p {
line-height: 29px;
font-style: italic;
}
.tabs input:checked + label {
background-color: #4B5898;
color: #ffffff;
font-weight: 600;
}
.tabs input:checked ~ .tab-content {
display: block;
position: fixed;
top: 65px;
width: 1200px;
padding: 10px;
box-sizing: border-box;
margin: 0 auto;
left: 76px;
background-color: #4B5898;
color: #ffffff;
}
@media screen and (max-width:1300px){
.tabs input:checked ~ .tab-content {
max-width: 800px;
}
}
@media screen and (max-width: 991px){
.tabs input:checked ~ .tab-content {
max-width: 500px;
}
}

@media screen and (max-width: 650px){
.tab-menu {
padding: 0 10px;
}
.tabs input:checked ~ .tab-content {
left: 10px;
}
}

@media screen and (max-width: 525px){
.tabs {
width: 80px;
height: 40px;
}
.tabs input:checked ~ .tab-content {
left: 10px;
top: 60px;
max-width: 270px;
}
}
<div class="tabs-container">
<div class="tab-menu">
<div class="tabs">
<input type="radio" name="myradio" checked>
<label>TAB1</label>
</input>
<div class="tab-content">
<h3>One Content</h3>
<p>Is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publ</p>
</div>
</div>
<div class="tabs">
<input type="radio" name="myradio">
<label>TAB2</label>
</input>
<div class="tab-content">
<h3>Two Content</h3>
<p>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage,</p>
</div>
</div>
<div class="tabs">
<input type="radio" name="myradio">
<label>TAB3</label>
</input>
<div class="tab-content">
<h3>Three Content</h3>
<p>The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form,</p>
</div>
</div>
</div>
</div>

https://codepen.io/Vikaspatel/pen/zMNZWv

Answer №2

Implementing a "pure CSS" tab control can be done in various ways. I took inspiration from this sample and made modifications to the HTML structure in your JSFiddle to incorporate <div> content inside <li> elements. I then applied some styling to hide inactive tabs.

/* Specific CSS for tab content */
.tabs li div {
    display: none;
}

/* Specific CSS for active tab */
.tabs li.active div {
    display: block;
    position:absolute;
    left:0px;
}

In addition, I updated the header links from <a> to radio inputs to ensure only one tab is active at a time in "pure CSS". This change required adjusting the active tab's content CSS selector to:

.tabs input[name=tabs]:checked ~ div

This selects divs that are siblings of input tags named tabs which are checked.

Your initial implementation was close, but the key point to remember is that maintaining state in pure CSS necessitates the use of radio or checkbox inputs.

Cheers! :-)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Create styles for each component based on their specific props when designing a customized Material-UI theme

I am having trouble styling the notchedOutline of a disabled <OutlinedInput /> in my custom MUI theme. My goal is to make the border color lighter than the default color when the input is disabled. Here is what I have attempted so far: const theme = ...

Recording Node pathways that begin with the hash symbol #

How can I configure my Node/Express routing to intercept a route like /#/about and send it to /about Is there a way for node to handle routes that start with "/#"? It appears to only cut off at the hash mark. ...

The Vue Swiper does not support inline containers in my code

I am facing an issue with the layout of my simple swiper in my vue app. The containers are not inline as expected, causing the second one to appear under the first. Additionally, I am struggling with making the slider element visible only within the viewpo ...

Using Jquery for a Second Timer

I have a jQuery function that looks like the one below. The result is displayed in a span, but when the page is reloaded, this span briefly disappears and then reappears. Is there a way to prevent this from happening? I need it to stay visible at all tim ...

Content towering over the footer

Can anyone help me figure out how to create a footer that appears behind the content when you scroll towards the end of a website? I've tried looking for tutorials online, but haven't found exactly what I'm looking for. Most of what I'v ...

Exploring Ways to Utilize Multiple Access Levels with PHP and MySQL

Currently, I am developing a PHP & MySQL management system for a college level institution. In this system, each employee and student will have their own account and I aim to display personalized pages for each user. In my scenario, there are 3 levels o ...

Preventing box shadows from blending together

I'm struggling with a design issue on my site where I have four divs in the center, each represented by a box with a box-shadow. Unfortunately, the colors of the shadows are getting mixed up and creating an unwanted effect. https://i.sstatic.net/NdAUB ...

A Step-by-Step Guide to Downloading Images by Clicking

Having an issue with my image download code. When I try to download an image, the process starts but doesn't complete and no file is received. Instead, a type error is encountered. <html> <head> <script type="text/javascript"> funct ...

Monitoring user logins based on user identification

How can I effectively monitor the activity of users who have logged into a web application that is managed by an external company? Despite my extensive research efforts, as a non-technical individual, I am struggling to understand how to utilize Google Ana ...

Can a font size be adjusted dynamically based on the width of its containing class?

I have encountered this issue before, but the previous solution did not work for me. I am now seeking a viable alternative. Currently, I am developing a website and have an event announcement block that needs to display the event date spanning the entire ...

Implement a smooth transition effect when changing the image source

I am currently working on enhancing a Squarespace website by adding a new image fade-in/fade-out effect when the mouse hovers over one of three buttons. The challenge I'm facing is that the main static image is embedded as an img element in the HTML r ...

A guide to implementing hover and mouse click effects on ImageList using Material UI version 5.11.11

I have successfully created a list of images using MaterialUI, but now I would like to enhance the user experience by adding a mouse hover effect to highlight the image under the cursor. Below is the code snippet for my image list. How can I accomplish t ...

Utilizing JavaScript to dynamically resize an element within a slide as soon as the slider transitions to the following slide

RESOLVED! ISSUE FIXED! While working on my personal website using WordPress and the Smart Slider 3 plugin, I encountered a problem with the positioning and size of an element in the second slide. Despite finding a tutorial explaining how to manually trigg ...

What is the most effective method for generating dial images through programming?

Currently, I am in the process of creating a website that makes use of variously sized and styled dials to indicate progress. The filled portion of the dial represents how close an item is to being 100% complete. I am seeking a universal solution that wil ...

One cool CSS trick: altering a div's background color with hover!

I'm working on creating a div that acts as a link to another page. My goal is to have the entire background color of the div change to blue when someone hovers over it. I want to achieve this effect using CSS only, as I am not sure if javascript will ...

Centered vertically on the right side of a div, a CSS arrow is positioned

Hello, I am seeking a way to vertically center a CSS triangle/arrow on the right side of a div using pure CSS. Unfortunately, I can't include images due to my low reputation. Currently, my code is not functioning properly and lacks cross-browser comp ...

Effective URL structure with nested ui-view in AngularJS

It is common knowledge that Angular functions as a SPA (Single Page Application). I am seeking the most effective approach to display a main left-side menu selector page, where clicking on any menu item will load the content in the right-side view div. Th ...

Help with guiding and redirecting links

Here's the code snippet: <script> if(document.location.href.indexOf('https://thedomain.com/collections/all?sort_by=best-selling') > -1) { document.location.href = 'https://thedomain.com/pages/bestsellers'; } </script& ...

Organize a list using custom span values with JavaScript

<ul id="CoreWebsiteTopHeader_6_list"><li><a class="navigation" href="/seller"><span class="editableLinks" data-id="32638" data-sort="110">Seller</span></a></li><li><a class="navigation" href="/about">&l ...

"Styling a form input with YAML and implementing a jQuery date picker field

Seeking help with a CSS question related to YAML. I've been struggling all day trying to position a jQuery date field next to a YAML CSS field, but so far I can only manage to have it display underneath the input field... Is there a way for the date ...