Search code examples
htmlcsscombinators

CSS + ~ > Combinators Not Working To Change Tab Label


The code below is being used to create tabs:

<ul class="tabs">
<li>
  <input type="radio" name="tabs" id="tab1" checked />
  <label for="tab1">Campaigns</label>
  <div id="tab-content1" class="tab-content">
  ...Tab Contents....

And the tab display & highlighting the selected tab is supposed to work through the following CSS:

.tabs [id^="tab"]:checked + label { 
   top: 0;
   padding-top: 17px; 
   background: fff; 
}
.tabs [id^="tab"]:checked ~ [id^="tab-content"] {
   display: block;
}

However, while the tab selection and display (2nd CSS declaration) works perfectly, the label background change (1st CSS declaration) is being completely ignored.

Why is the combinator working like a dream for the tab selection but not for the label formatting? Help please

Edit: The full CSS code is as follows:

.tabs {float: none;list-style: none;position: relative;margin-top: 20px;text-align: left;width: 100%;height: 274px;}
.tabs li {float: left;display: block}
.tabs input[type="radio"] {position: absolute;top: -9999px;left: -9999px}
.tabs label {display: block;padding: 14px 21px;border-radius: 10px 10px 0 0;font-size: 20px;font-weight: normal;background:#ccc;border:1px #fff solid;cursor: pointer;position: relative;top: 4px}
.tabs label:hover {background: #eee;border:1px #ccc solid;-webkit-transition-timing-function:ease-in;transition-timing-function:ease-in}
.tabs .tab-content{z-index: 2;display: none;overflow: hidden;width: 100%;font-size: 17px;line-height: 25px;padding: 25px;position: absolute;top: 53px;left: 0;background: #fff;border-top:1px #ccc solid}
//The Magic
.tabs [id^="tab"]:checked + label { 
    top: 0;
    padding-top: 17px; 
    background: fff; 
}
.tabs [id^="tab"]:checked ~ [id^="tab-content"] {
    display: block;
}

Solution

  • Changing //The Magic to CSS comments /*The Magic*/ and changing the background: fff to background: #fff. sorted it

    Thanks to @misterManSam.