Creating tabbed content with a classless CSS design by utilizing radio buttons instead of the traditional unordered

I am attempting to transform the classless CSS and HTML provided below from a list format to using radio buttons and labels for tabbed content.

My goal is to make it concise and avoid redundancy. Here is the setup of what I am aiming for:

<nav>
    <input type="radio" name="tabs" checked>
    <label for="tab1">One</label>
    <input type="radio" name="tabs">
    <label for="tab2">Two</label>
    <input type="radio" name="tabs">
    <label for="tab3">Three</label>
    <section id="tab2">Tab 2</section>
    <section id="tab3">Tab 3</section>
    <section id="tab1">Tab 1</section>
</nav>

Below is the existing tabbed content code that currently uses an unordered list. No additional styling has been added for simplicity:

nav>ul~section:last-of-type{display:block;}
nav>ul~section:target~section:last-of-type{display:none;}
section:not(:target) {display:none;}
<nav>
    <ul>
        <li><a href="#tab1">One</a>
        <li><a href="#tab2">Two</a>
        <li><a href="#tab3">Three</a>
    </ul>
    <section id="tab2">Tab 2</section>
    <section id="tab3">Tab 3</section>
    <section id="tab1">Tab 1</section>
</nav>

Answer №1

If you desire to maintain the existing HTML structure, one way to achieve this without adding more classes or ids is as follows:

section {
  display: none;
}

input:checked+label[for="tab1"]~#content1 {
  display: block;
}

input:checked+label[for="tab2"]~#content2 {
  display: block;
}

input:checked+label[for="tab3"]~#content3 {
  display: block;
}
<nav>
  <input type="radio" name="tabs" checked>
  <label for="tab1">One</label>
  <input type="radio" name="tabs">
  <label for="tab2">Two</label>
  <input type="radio" name="tabs">
  <label for="tab3">Three</label>
  <section id="content2">Tab 2</section>
  <section id="content3">Tab 3</section>
  <section id="content1">Tab 1</section>
</nav>

However, this method is not very flexible (requires CSS update when adding more tabs) and the labels are not directly associated with their inputs (making it difficult for users to click on a specific label like 'One'). To enable that functionality, the id="tab1" should be added to the corresponding input element.

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

Looking to adjust the height of a foreignObject element within an SVG?

Looking to dynamically change the height of a foreignObject within an SVG, while also needing HTML elements inside it (working with ngx-graph). <foreignObject x="1" y="1" width="335" [height]="foreignObjHeight(node.Dat ...

JavaScript code altered link to redirect to previous link

I added a date field to my HTML form. <input type="date" name="match_date" id="matchDate" onchange="filterMatchByDate(event)" min="2021-01-01" max="2021-12-31"> There is also an anchor tag ...

JavaScript and jQuery experiencing difficulty rendering proper style and image in output display

I am currently working on code that extracts information from a JSON variable and displays it on a map. The code looks like this: marker.info_window_content = place.image + '<br/>' +"<h4>" + place.name + "</h4> ...

Looking to replace a background image using javascript?

(apologies for any language mistakes) I have multiple divs with a common background image. I assigned them the same class and set the background image using CSS in style.css which worked perfectly fine. Now, I want to change the background image dynamical ...

Creating a responsive design with dual backgrounds using Tailwind CSS

I have a design in Figma that I want to convert to React using Tailwind CSS while maintaining 100% pixel-perfect accuracy. The design is created for a Macbook Pro 16 inch, which has a width of 1728px. However, I want to center the content within a customiz ...

Are you looking for a demonstration of "Creative Loading Effects" that triggers when the page is loaded?

I came across this demo for a preloader on my website called Creative Loading Effects, specifically the "3D Bar Bottom" effect, which I find very exciting. However, I noticed that it only loads when we press the button, and not automatically when the page ...

Custom CSS for the Google Maps Circle Object

Currently, I am utilizing the Google Maps Javascript API v3 Circle object to display circles on the map. I am interested in customizing the CSS of this circle by incorporating some CSS animations. Although I am aware that custom overlays can be used for t ...

Unable to display the complete JSON data using ng-repeat in AngularJS

Utilize ng-repeat to display data retrieved from a web service. Below is my controller JS (GetAllCtrl.js): https://i.stack.imgur.com/GAelG.jpg I have received JSON data: https://i.stack.imgur.com/0xkAU.jpg My goal now is to extract only company informati ...

Discovering unmarked content using Jsoup

I have been struggling to find a solution to my problem, but so far I have not found any clear answers in my search results. I have spent a considerable amount of time trying different suggestions from various posts that seemed somewhat related. Now, let&a ...

Using jQuery within MediaWiki pages beyond just Monobook.js or extensions is a powerful tool

Currently, I am experimenting with some jQuery functionalities in a MediaWiki content page to enhance various features. I have referred to the following resources: http://www.mediawiki.org/wiki/Manual:$wgRawHtml (particularly for incorporating Paypal but ...

How to customize the hover and active properties of a checked checkbox in Vue 3 using Tailwind CSS and PUG?

It seems that checkboxes have a level of sophistication that I never anticipated, or perhaps my brain just can't quite grasp the nuances of checkboxes. After spending a significant amount of time researching and experimenting with checkboxes in a Vue ...

issue with padding-bottom in Firefox and Internet Explorer 11

JsFiddle CSS body, html { background: violet } * { margin: 0; padding: 0 } .fixed { height: 100%; width: 300px; background: #fff; right: 0; position: absolute; top: 0; display: -webkit-box; display: -moz-box; display: -ms-flexbox; ...

Having trouble with the JQuery scrollTop animation? Getting the error message "Cannot read property 'top' of undefined"?

I am having trouble with my jquery animation scrollTop function. Whenever I click on <a>, it takes me to the anchor without any animation. Can someone please provide a solution for this issue? Upon running the webpage, I encountered an error message ...

How can I align an input to the right using Bootstrap?

I found an example of a navbar that I liked and decided to copy it. Here is the code snippet: <div class="container"> <h3>Nawigacja zamknieta w kontenerze</h3> <nav class="navbar navbar-toggleable-md navbar-light bg-faded"> < ...

Delay the loading of HTML elements to improve website performance

While working on my portfolio, I noticed that the page load time is quite long due to all the images needing to load before the page is fully loaded. I started thinking if there is a way to delay or prevent the loading of images and then have them load la ...

It's impossible for me to align two blocks at the same level

Check out the following code snippet. I am trying to group mid_left and mid_right within a single mid div tag, but struggling with positioning mid_right correctly - either outside of the mid tag or not at the same level as mid_left. I have experimented wit ...

What is causing the data in the hierarchy table to be invisible?

I am attempting to build a hierarchical table using Vue.js as the foundation. However, I want the table to remain hidden with no data displayed. The issue seems to be within this section of code: <tr v-if="item.childrenVisible" v-for="chi ...

Unable to accommodate additional space, InputGroup is not utilizing the

Using react-bootstrap in my project, I have a component with the following code. I want the input and button to display together and fill up the entire space allocated by the Col. Although the input and button are displaying side by side, they are not ta ...

Obtain pictures from MongoDB for a website using Angular6

I'm currently in the process of developing my website and I want to showcase an image from my database on the header. Each customer is assigned a unique logo based on their ID, but I'm unsure how to use Angular to display it. Below is my code s ...

What is the process for linking functionality to buttons in Embedded Elixir?

When it comes to rendering templates, I follow this method: <%= for country <- @countries do %> <div> <div>A compilation of countries you have visited</div> <a href="/countries/<%= String.downcase(country) %& ...