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;
}