What is the functionality of CSS radio tabs?

Can anyone provide a breakdown of how the final section of this code operates? Specifically:

[type=radio]:checked  {
}

[type=radio]:checked ~ .content {
z-index: 1;
}

I am brand new to CSS and wanted to experiment with creating interactive CSS tabs, which led me to explore existing code. However, I am currently feeling quite perplexed.

Why is [type=radio]:checked required? The property z-index: 2; was originally present within the brackets but I removed it and the code continued to function normally; yet removing [type=radio]:checked entirely breaks the code. Why is this the case if it doesn't have any properties at the moment?

The selector [type=radio]:checked ~ .content previously included

[type=radio]:checked ~ label ~ .content
, however, omitting the label still results in proper functionality. When was the use of the label necessary in the first place?

HTML:

   <div class="tab">
       <input type="radio" id="tab-1" name="tab-group-1" checked>
       <label for="tab-1">Tab One</label>

       <div class="content">
           tab#1
       </div> 
   </div>

   <div class="tab">
       <input type="radio" id="tab-2" name="tab-group-1">
       <label for="tab-2">Tab Two</label>

       <div class="content">
           tab#2
       </div> 
   </div>

    <div class="tab">
       <input type="radio" id="tab-3" name="tab-group-1">
       <label for="tab-3">Tab Three</label>

       <div class="content">
           tab#3
       </div> 
   </div>

</div>
</html>

CSS:

.tabs {
  position: relative;   
  height: 200px; /* This part sucks */
  clear: both;
  margin: 25px 0;
}
.tab {
  float: left;

}
.tab label {
  background: #eee; 
  padding: 10px; 
  border: 1px solid #ccc; 
  margin-left: -1px; 
  position: relative;
  left: 1px; 
}
.tab [type=radio] {
  display: none;
}
.content {
  position: absolute;
  top: 28px;
  left: 0;
  background: white;
  right: 0;
  bottom: 0;
  padding: 20px;
  border: 1px solid #ccc; 
}

[type=radio]:checked  {
}

[type=radio]:checked ~ .content {
z-index: 1;
}

Answer №1

Within your CSS code, the final section looks like this:

[type=radio]:checked  {
}

[type=radio]:checked ~ .content {
  z-index: 1;
}

This snippet assigns a z-index to the content class. By giving a z-index value of 1 to the clicked radio button's corresponding content class, only that particular tab will be displayed. Other content classes are unaffected because they do not have a specified z-index.

To observe this behavior in action, try adding a z-index value of 10 to the content class in your CSS. You will notice that all tabs become jumbled up due to having the same high z-index. By setting z-index: 1;, each content class retains its distinct display when an individual radio button is selected.

If you are unfamiliar with the concept of [type=radio]:checked, it signifies the active or clicked state of a radio input element.

The segment of code

[type=radio]:checked ~ label ~ .content
allows for precise selection of a DOM element hierarchy. While it may function without this specific reference, it serves to target elements following a specific sequence: input radio > label > .content.

If you require further clarification on the purpose of z-index, feel free to ask and I can provide additional explanation.

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

Is there a way to enable scrolling on the page upon loading, without moving the embedded PDF object itself?

After embedding a PDF object on my webpage, I noticed that when loading the page and scrolling using the mouse wheel, it actually scrolls up and down inside the PDF instead of moving through the entire page. To fix this issue, I have to first click in a bl ...

JavaScript Tutorial: Extracting the text content of a drop event

Curious about extracting the text value from a drop event, I set out to find an answer. To my surprise, I discovered that it's not as straightforward as I thought. Check out the demo below: <script src="https://ajax.googleapis.com/ajax/libs/jquer ...

Having trouble with CSS styling not applying to the header tag

Take a look at this snippet of HTML code: <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <link href="css/test.css" rel="stylesheet"> </head> <p></p> <h2>Meeting the Old Guard ...

Unable to display a Google map within a webview when viewing a local HTML file

I am currently working with a local HTML file called basicmap.html that includes the following code: <!DOCTYPE html> <html> <head> </head> <body> <div id="map"></div> <script> ...

Opacity: When the value is set to 0, the element remains invisible even when hovered over

I'm encountering an issue with configuring an element to have absolute positioning and a width and height of 100% while setting its opacity to 1.0. To summarize, here is the HTML code I am working with: <ul class="properties"> <li style ...

Creating a Dynamic Slideshow on Autopilot

My JavaScript skills are not perfect and I'm feeling a bit lost. I have this code for a slideshow, but I want to make it automatic while also allowing users to navigate between images freely. I'm struggling to figure out how to implement this fun ...

How to insert an image into a placeholder in an .hbs Ember template file

I'm looking to enhance a .hbs template file in ember by incorporating an image. I am not a developer, but I'm attempting to customize the basic todo list app. <section class='todoapp'> <header id='header'> & ...

I am looking to transmit information from flask to React and dynamically generate HTML content based on it

index.py @app.route('/visuals', methods=["GET", "POST"]) def visuals(): global visclass return render_template('visframe.html', images=visclass.get_visuals()) visframe.html <div id='gallery'></div> { ...

Aligning a picture with a heading

I'm currently working on a website header design where I am struggling to align the logo and site title horizontally. As someone who is new to CSS, any guidance or tips would be greatly appreciated! The logo image has the following styling: .logo { ...

Adjusting the columns of two tables when one table is held in place

My dilemma involves managing two tables: In the scenario, Table 1 remains fixed in place serving as a header that is always visible. Meanwhile, Table 2 acts as the data table. I am not a fan of fixed widths for my tables; I prefer them to dynamically adj ...

Sending the selected option from a dropdown menu to populate a textbox on a separate webpage using PHP, jQuery, and AJAX

I am looking to dynamically update the value of a dropdown in index.php to a textbox in test.php using Ajax. The scenario involves having test.php embedded as an iframe within index.php. Upon changing the dropdown selection, the corresponding value should ...

What is the best way to ensure that `col-sm-4` takes up the full space previously occupied by `col-sm-5` when it is hidden

In tablet mode, I am concealing my col-sm-5 using the class hidden-sm. <div class="row"> <div class="col-sm-4"> </div> <div class="col-sm-5"> </div> <div class="col-sm-3"> </div> </div& ...

Aligning an SVG icon at the center of a button

I have elements that are currently being used as buttons, but I want to switch them out for actual button tags. This would improve accessibility and allow keyboard navigation using the tab key to focus on my buttons. Each button contains a centered SVG ima ...

Is there a way to prevent an inline SVG from appearing at a random height without specifying its height or the height of its parent element?

Displayed here is a simplified Bootstrap 3 layout. The middle column in the row features an inline SVG that transitions from a blue box to a green box. I've opted for styling SVG with browser CSS because I find it interesting! Additionally, this appr ...

Programmatically extracting a list of utilized CSS images from IE WebBrowser (IHTMLDocument2)

Exploring the IHTMLStyleSheetsCollection, IHTMLStyleSheet, IHTMLStyleSheetRulesCollection etc. of IHTMLDocument2 to compile a list of all styles in the current document is quite simple. Does anyone have any suggestions on how to generate a list of only th ...

Arranging the 1st element of the 1st row to be placed at the end using CSS Flex

One of my challenges involves choosing between two options: https://i.sstatic.net/JpWiGfW2.png In my project, I am utilizing a container with flex-wrap: wrap to showcase items of equal dimensions. However, the first item in this container stands out due t ...

Guide to modifying WordPress version 4.5 with HTML pages

I am encountering issues with my Wordpress website. I have installed it on Softcald in cPanel and now I want to edit my Wordpress using HTML. However, I am having trouble finding the editing option. My Wordpress version is 4.5 and I have searched online ...

Shadow border added to each div creates a unique navigation tab for easy access

#container { position: fixed; height: 100%; left: 0px; right: 0px; top: 0px; width: 10%; background-color: blue; } #container>div {} <html> <div id="container"> <div>up</div> <div>down</div> <d ...

Tips on how to connect with ngFor

I have been working on an application to display various events, but I am encountering an issue. Whenever I load the webpage, it appears empty and the console throws an error saying 'Can't bind to 'ngForEvent' since it isn't a know ...

Ensure that every closing curly brace is followed by a new line character

I'm utilizing a piece of code I stumbled upon online to condense my css files and it's working well, but there's a slight regex problem that's causing me some difficulty. $css = preg_replace('/([\\w#\\.\&b ...