How to set a property value for the active state of another element in CSS?
P粉878510551
P粉878510551 2023-09-14 18:54:27
0
1
567

I'm currently developing a JavaScript-free switch. One problem I'm having is that I don't know how to set a property of one element while another element is in the:activestate. I tried a few things but none worked. I have a CodePen link here. Also, here is some code:

body { height: 100vh; display: flex; justify-content: center; align-items: center; background-color: gray; overflow: hidden; filter: brightness(100%); } .wall-switch { background-color: #ebebeb; width: 300px; height: 500px; box-shadow: inset -10px -10px 22px 0px rgba(255, 255, 255, 1), inset 10px 10px 22px 0px rgba(0, 0, 0, 0.30); display: flex; align-items: center; flex-direction: column; padding: 25px; border-radius: 4px; } .screw { padding: 7.5px; border-radius: 20px; display: flex; align-items: center; justify-content: center; box-shadow: inset 2.5px 2.5px 10px 0px rgba(255, 255, 255, 1), inset -2.5px -2.5px 10px 0px rgba(0, 0, 0, 0.30), 0px 0px 2px 1px rgba(0, 0, 0, 0.40); } .icon { transition-duration: 10s; transition-timing-function: linear; } .icon:active { transform: rotate(-3600deg); } .padding { padding: 55px; } .switch { width: 105px; height: 200px; box-shadow: 0px 0px 2px 1px rgba(0, 0, 0, 0.40), inset 0px 95px 20px 0px #ebebeb, inset 0px -95px 20px 0px rgba(0, 0, 0, 0.20); border-radius: 2.5px; } .switch:active { box-shadow: 0px 0px 2px 1px rgba(0, 0, 0, 0.40), inset 0px -95px 20px 0px #6b6b6b, inset 0px 95px 20px 0px rgba(0, 0, 0, 0.20); } .switch:active .wall-switch { background-color: #6b6b6b; }

Does anyone know the solution? I've been working on this for a while.

P粉878510551
P粉878510551

reply all (1)
P粉262113569

I checked and it turns out this is a new feature in CSS. The > character is used for promotion (from child element to parent element, which is new).

.wall-switch:has(> .switch:active){ background-color: #6b6b6b; }

Reference:Is there a CSS parent selector?

    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!