0

I'm experimenting with a tabbed navigation made of radio buttons. My problem is that when I click on the tab the selected id's opacity isn't change so the content doesn't get visible.

My HTML code:

<div id="container">

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

<div id="content">

    <div id="content-1">
        <form>
            First name:<br>
            <input type="text" name="firstname"><br>
            Last name:<br>
            <input type="text" name="lastname">
        </form> 
    </div>

    <div id="content-2">
        <p class="column-left"><img src="http://ximg.es/200x150" alt="">Lorem ipsum dolor sit amet, consectetur adipisicing elit. </p>
        <p class="column-right"><img src="http://ximg.es/200x150" alt="">Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
    </div>

    <div id="content-3">
        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. </p>
        <ul>
            <li>Lorem ipsum dolor sit amet, consectetur adipisicing it.</li>
            <li>Lorem ipsum dolor sit amet, consectetur adipisicing it.</li>
            <li>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</li>
        </ul>
        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. </p>
    </div>
</div>

My CSS code:

* {
    font-family: Arial, sans;
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    -moz-box-sizing: border-box;
}


#container {
  padding-top: 6em;
  margin: 0 auto;
  width: 50%;
}

#tabs input {
  height: 2.5em;
  visibility: hidden;
}

#tabs label {
  background: #f9f9f9;
  border-radius: .25em .25em 0 0;
  color: red;
  cursor: pointer;
  display: block;
  float: left;
  font-size: 1em;
  height: 2.5em;
  line-height: 2.5em;
  margin-right: .25em;  /*space between tabs*/
  padding: 0 1.5em;
  text-align: center;
}

#tabs input:hover + label {
  background: #ddd;
  color: lawngreen;
}

#tabs input:checked + label {
  background: black;
  color: white;
  position: relative;
  z-index: 6;
}

#content {
  background: red;
  border-radius: 0 .25em .25em .25em;
  min-height: 20em;
  position: relative;
  width: 100%;
  z-index: 5;
}

#content div {
  opacity: 0;
  padding: 1.5em;
  position: absolute;
  z-index: -100;
  transition: all linear 0.1s;
}

#tabs input#tab-1:checked ~ #content #content-1,
#tabs input#tab-2:checked ~ #content #content-2,
#tabs input#tab-3:checked ~ #content #content-3 {
  opacity: 1;    
  z-index: 100;
}


input.visible {
  visibility: visible !important;
}

I think the problem lies with the 3 selector lines "#tabs input#tab-1:checked ~ #content #content-1"

I've tried to rewrite these line in different syntax bu nothing works.

Please help.

Flemming Lemche
  • 169
  • 1
  • 3
  • 23
  • as your HTML structure is now , you cannot set a relationship, with just CSS , between tab1 and content1 ( there is no relationship between them ) if you don't want to change the HTML structure you have to use, for example, javascript – Mihai T Jun 13 '17 at 09:25

2 Answers2

4

The div that matches #content is not a sibling of any of the inputs. It is a sibling of their parent.

My aunt is not my sister.

You can't draw a link from the inputs to #content-X with CSS selectors. There is no parent combinator.

You would need to use JavaScript for this.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
1

* {
    font-family: Arial, sans;
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    -moz-box-sizing: border-box;
}


#container {
  padding-top: 6em;
  margin: 0 auto;
  width: 50%;
}

#tabs input {
  height: 2.5em;
  visibility: hidden;
}

#tabs label {
  background: #f9f9f9;
  border-radius: .25em .25em 0 0;
  color: red;
  cursor: pointer;
  display: block;
  float: left;
  font-size: 1em;
  height: 2.5em;
  line-height: 2.5em;
  margin-right: .25em;  /*space between tabs*/
  padding: 0 1.5em;
  text-align: center;
}

#tabs input:hover + label {
  background: #ddd;
  color: lawngreen;
}

#tabs input:checked + label {
  background: black;
  color: white;
  position: relative;
  z-index: 6;
}

#content {
  background: red;
  border-radius: 0 .25em .25em .25em;
  min-height: 20em;
  position: relative;
  width: 100%;
  z-index: 5;
  overflow-y: auto;
}

#content div {
  opacity: 0;
  padding: 1.5em;
  position: absolute;
  z-index: -100;
  transition: all linear 0.1s;
  
}

#tabs input#tab-1:checked ~ #content div#content-1,
#tabs input#tab-2:checked ~ #content div#content-2,
#tabs input#tab-3:checked ~ #content div#content-3 {
  opacity: 1;    
  z-index: 1000;
}


input.visible {
  visibility: visible !important;
}
<div id="container">

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


<div id="content">

    <div id="content-1">
        <form>
            First name:<br>
            <input type="text" name="firstname"><br>
            Last name:<br>
            <input type="text" name="lastname">
        </form> 
    </div>

    <div id="content-2">
        <p class="column-left"><img src="http://ximg.es/200x150" alt="">Lorem ipsum dolor sit amet, consectetur adipisicing elit. </p>
        <p class="column-right"><img src="http://ximg.es/200x150" alt="">Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
    </div>

    <div id="content-3">
        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. </p>
        <ul>
            <li>Lorem ipsum dolor sit amet, consectetur adipisicing it.</li>
            <li>Lorem ipsum dolor sit amet, consectetur adipisicing it.</li>
            <li>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</li>
        </ul>
        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. </p>
    </div>
</div>

</div>

You can if you slightly change your html.

Basically, to make #content siblings #tabs, I moved #tabs closing div to the end. Hope this helps.

Check the fiddle here

devil_coder
  • 1,115
  • 1
  • 9
  • 12
Nineoclick
  • 804
  • 9
  • 17