html - 更改链接到 radio 的选定 CSS 选项卡的背景图像

标签 html css web tabs

我正在尝试使用来自 css-tricks.com 的 CSS 选项卡技术并根据我的需要对其进行调整。 http://css-tricks.com/functional-css-tabs-revisited/

我想让每个选项卡显示它自己的背景图像,并且在选择特定选项卡时显示该选项卡独有的不同背景图像。

HTML:

<div class="tabs">                  
    <div class="productTab">
       <input type="radio" id="tab1" name="tabgroup1" checked>
       <label for="tab1" id="productTabLabel1">Tab One</label>   
       <div class="productTabContent">
           stuff0
       </div> 
    </div>  
    <div class="productTab">
       <input type="radio" id="tab2" name="tabgroup1">
       <label for="tab2" id="productTabLabel2">Tab Two</label>     
       <div class="productTabContent">
           stuff1
       </div> 
    </div>
</div>

CSS:

.tabs {
    position: relative;
    min-height:200px;
    clear: both;
}

.productTab {
    float: left;
    width: 132px;
    height: 164px;
}

.productTab [type=radio] {
    display: none;   
}

.productTab label{
    /*background: #eee;*/
    border: 1px solid #ccc; 
    position: relative;
    left: 1px; 
    margin-left: -1px; 

}

.productTabContent {
    background: #229FE8;
    border: 1px solid #ccc;
    position: absolute;
    top: 28px;
    right: 0;
    bottom: 0;
    left: 0;
}

/*Changes the style of the selected Label and brings it to the front*/
[type=radio]:checked ~ label {
  background: white;
  border-bottom: 1px solid white;
  z-index: 2;
}

/*Brings the content adjacent to the label to the front*/
[type=radio]:checked ~ label ~ .productTabContent {
  z-index: 1;
}

#productTabLabel1 {
    background-image: url('../img/products1.jpg');
}

#productTabLabel2 {
    background-image: url('../img/products2.jpg');
}

我遇到的另一个问题是,即使我指定了标签的尺寸,标签也不会增长以显示背景图像,尽管我稍后会意识到这一点很明显。

最佳答案

我认为您对“制表符”的实际形式存在误解。

这不是 label...它是类为 .productTab 的 div。

我已经清除了一些额外的 CSS 并更改了一些颜色,以便您可以看到发生了什么。

JSfiddle Demo

CSS

.tabs {
    position: relative;
    min-height:200px;
    clear: both;
}

.productTab {
    float: left;
    width: 132px;
    height: 164px;
    //background-color: red;
    border-radius:5px;
    padding:5px;
}

.productTab [type=radio] {
    display: none;   
}

.productTab label{
    /*background: #eee;*/
    //border: 1px solid #ccc; 
    position: relative;
    left: 1px; 
    margin-left: -1px;

}

.productTabContent {
    background: #229FE8;
    //border: 1px solid #ccc;
    position: absolute;
    top: 28px;
    right: 0;
    bottom: 0;
    left: 0;
}

/*Changes the style of the selected Label and brings it to the front*/
[type=radio]:checked ~ label {
  //background: white;
  //border-bottom: 1px solid white;
  z-index: 2;
}

/*Brings the content adjacent to the label to the front*/
[type=radio]:checked ~ label ~ .productTabContent {
  z-index: 1;
}

.productTabLabel1 {
    //background-image: url('../img/products1.jpg');
    background-color: #bada55;
}

.productTabLabel2 {
    //background-image: url('../img/products2.jpg');
    background-color: #663399;
}

关于html - 更改链接到 radio 的选定 CSS 选项卡的背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24699881/

相关文章:

html - 使显示的 div 的整个高度锚定为 :block

Javascript 在代码中显示 var

javascript - 如何在 JavaScript 中将图像转换为 base64

javascript - 我尝试将所有黑色代码更改为白色代码

css - Vue-cli 导入 css 不起作用

python - 如何使用mechanize进入yelp主页,找到搜索栏,输入 'Dinner'返回结果?

java - 如何使用具有 POST 请求的 JSOUP 重定向到主页?

css - :after doesn't scope by relative of parent

PHP 函数传递包含文件

Chrome 中的 CSS 显示问题 : background-image & position