我正在尝试使用单选按钮制作的标签式导航。 我的问题是,当我点击标签时,所选内容的不透明度不会改变,因此内容不会显示。
我的HTML代码:
<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>
我的CSS代码:
* {
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;
}
我认为问题在于3个选择器行“#tabs input#tab-1:checked~#content#content-1”
我试图用不同的语法重写这些行,但没有任何作用。
请帮忙。
答案 0 :(得分:4)
匹配#content
的div不是任何input
的兄弟。这是他们的父母的兄弟姐妹。
我的阿姨不是我的妹妹。
您无法使用CSS选择器从输入到#content-X
绘制链接。 There is no parent combinator
您需要使用JavaScript。
答案 1 :(得分: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;
}
&#13;
<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>
&#13;
如果你稍微更改你的HTML,你可以。
基本上,为了#content
兄弟姐妹#tabs
,我将#tabs
关闭div移到了最后。希望这会有所帮助。
检查小提琴here