Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Radio button group checked property css error

Tags:

html

css

I made the tabs using radio button trick like this

Html

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

then css:

.tabs label {
   margin-left: -1px;
   border: 1px solid;
   padding: 0px 80px 0px 80px;
   background: #ddd;
}

.tabs input[type=radio]:checked ~ .tabs label {
   background: #fff;
   border-bottom: 1px solid #fff;
}

The problem here is the style of label does not change when radio button is chosen. Can anyone help me to explain it, thanks

btw: I run code using IE 8. Does it support checked properties

like image 209
Tran Duy Linh Avatar asked May 03 '26 19:05

Tran Duy Linh


1 Answers

Check following is the solution. Replace ~ with +.

.tabs label {
   margin-left: -1px;
   border: 1px solid;
   padding: 0px 80px 0px 80px;
   background: #ddd;
}

input[type="radio"]:checked + label {
   background: #fff;
   border-bottom: 1px solid #fff;
}
<div class="tabs">
    <input type="radio" id="tab-1" name="tab-group-1" checked>
   <label for="tab-1">Tab One</label>
       <input type="radio" id="tab-2" name="tab-group-1"/>
   <label for="tab-2">Tab Two</label> 
</div>
like image 61
ketan Avatar answered May 05 '26 10:05

ketan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!