首页 > css伪类选择器问题

css伪类选择器问题

问题修正

我最后还是通过改变dom元素的结构解决了这个问题,如果有哪位大神可以通过css定位到我那个元素,请告诉我!

我的问题

我的问题是如何使用css嵌套使用伪类选择器

.cards input:nth-child(1):checked:root {
    background: #fff;
}

我的想法的当我第一个单选框被选择时,页面发生变化。完全用css实现怎么实现?我这样写是不行的。

完整demo

我是想做一个纯css实现的选项卡

html

<div class="input-container">
    <input class="input-box" type="text" placeholder="点我就变大了">
</div>
<div class="display-box">
    <div class="slider-container">
        <div class="item"></div>
        <div class="item"></div>
        <div class="item"></div>
    </div>
</div>
<div class="num">
    <span>1</span>
    <span>2</span>
    <span>3</span>
</div>
<div class="cards">
    <input type="radio" name="slider" value="1" />
    <input type="radio" name="slider" value="2" />
    <input type="radio" name="slider" value="3" />
</div>

css

.display-box {
    width: 219px;
    overflow: hidden;
}

.slider-container {
    font-size: 0;
    width: 657px;
    height: 80px;
}

.item {
    width: 219px;
    height: 80px;
    display: inline-block;
    margin: 0;
    padding: 0;
}

.item:nth-child(1) {
    background: #f00;
}

.item:nth-child(2) {
    background: #0f0;
}

.item:nth-child(3) {
    background: #00f;
}

.num {
    position: absolute;
    margin-top: -20px;
    margin-left: 145px;
}

.num span {
    z-index: -1;
    display: inline-block;
    width: 20px;
    height: 20px;
    margin: 0 1px;
    padding: 0;
    line-height: 20px;
    text-align: center;
    color: #fff;
    background-color: rgba(255, 255, 255, 0.7);
}

.cards {
    position: absolute;
    margin-top: -20px;
    margin-left: 145px;
}

.cards input {
    width: 20px;
    height: 20px;
    margin: 0 1px;
    padding: 0;
    opacity: 0.5;
}

.cards input:nth-child(1):checked:root {
    background: #fff;
}

仔细看了一下,你这一段

<input type="radio" name="slider" value="1" />
.cards input:nth-child(1):checked {
    background: #fff;
}

控制的是第一个input它被选择时的样式改变,而你是想改变每一个card的背景色吧
我举个方案:可以这样将每个card作为每个input的兄弟节点

    <div class="cards">
        <input type="radio" name="slider" value="1" class="radio" />
        <div class="cont"></div>
        <input type="radio" name="slider" value="2" class="radio" />
        <div class="cont"></div>
        <input type="radio" name="slider" value="3" class="radio" />
        <div class="cont"></div>
    </div>
    .radio:checked + .cont {
        background: #fff;
    }
    .cont{
        height: 50px;
        width: 50px;
        background-color: #eee;
    }

然后通过调整position属性去调整位置,这里我就不详写了

【热门文章】
【热门文章】