我試圖使用this recepee來實現不使用JS或JQuery,純CSS的選項卡,但我被卡住了。 目標是在點擊某個標籤上顯示不同的內容,並更改標籤樣式。 現在它顯示我想要的標籤,但每個div的內容以某種方式顯示在標籤上,而不是在標籤下。另外,我不能讓它改變風格。任何人都可以幫忙嗎?謝謝。對不起,如果問題看起來很愚蠢 - 我只是從html開始。帶有css3的選項卡,點擊更改樣式
<div id="menu">
<label class="collapse" for="1">tab1</label> <input id="1" name="r" type="radio" />
<div>111111111111111111111</div>
<label class="collapse" for="2">tab2</label> <input id="2" name="r" type="radio"/>
<div>2222222222222222222222</div>
<label class="collapse" for="3">tab3</label> <input id="3" name="r" type="radio" />
<div>333333333333333333333</div>
</div>
我的CSS:
#menu {
padding-left: 20px;
}
#menu label {
height: 30px;
background-color: #d1ecee;
width: 75px;
color: #2cb674;
font-size: 12px;
line-height: 30px;
text-align: center;
float: left;
position: relative;
margin-left: 2px;
display:block;
margin-right: 20px;
}
#menu label:before {
content: "";
position: absolute;
left: -20px;
width: 0;
height: 0;
border-top: 30px solid #d1ecee;
border-left: 20px solid transparent;
display:block;
}
#menu label:after {
content: "";
position: absolute;
right: -20px;
width: 0;
height: 0;
border-bottom: 30px solid #d1ecee;
border-right: 20px solid transparent;
}
#menu input {
display: none !important;
}
#menu input+* {
display: none !important;
}
#menu input:checked+* {
display: block !important;
}
#menu input:checked+label {
background-color: #2cb674;
color: white;
}
#menu input:checked+label:before {
border-top: 30px solid #2cb674;
}
#menu input:checked+label:after {
border-bottom: 30px solid #2cb674;
}
UPDATE:
你爲什麼首先使用菜單+輸入?嗯 –
@SzymonDziewoński它似乎不影響結果。此外,我還添加了另一部分我以前錯過的CSS。 –
對於初學者來說,使用'#menu + input'的規則永遠不會起作用。 '+'告訴CSS找到一個兄弟。然而,你的輸入是#menu的孩子,而不是兄弟姐妹。 – tbernard