gpt4 book ai didi

html - 仅 CSS 条件切换

转载 作者:行者123 更新时间:2023-12-04 08:33:21 27 4
gpt4 key购买 nike

*, *::after {
box-sizing: border-box;
transition-property: all;
transition-duration: 0.5s;
}
html, body {
margin: 0;
font-family: Arial;
text-transform: capitalize;
}
input { display: none; }
hr, label, label::after {
display: block;
width: 6rem;
height: 6rem;
border-style: none;
background-color: #444;
border-radius: 1rem;
}
div, section {
display: flex;
justify-content: center;
align-items: center;
margin: 0 1rem;
}
label, label::after {
position: relative;
width: 3rem;
height: 2rem;
margin: 1rem;
background-color: #eee;
content: '';
}
<style>
label {
cursor: pointer;
}
label::after {
transform: scale( 0.6 );
left: 0rem;
width: 2rem;
margin: 0;
background-color: #fff;
}
label:hover { background-color: #ddd; }
#blue:checked ~ div [ for='blue' ],
#red:checked ~ div [ for='red' ] {
background-color: #222;
}
#blue:checked ~ div [ for='blue' ]::after,
#red:checked ~ div [ for='red' ]::after {
left: 1rem;
}
#blue:checked ~ div [ for='blue' ]:hover,
#red:checked ~ div [ for='red' ]:hover {
background-color: #000;
}
#blue:checked ~ hr { background-color: #0be; }
#red:checked ~ hr { background-color: #d02; }
</style>
<input type='checkbox' id='blue'> <input type='checkbox' id='red'> <hr>
<div>
<section> <h2>blue</h2> <label for='blue'></label> </section>
<section> <h2>red</h2> <label for='red'></label> </section>
</div>

checkbox hack可用于创建仅 CSS 切换,如下面的开/关开关所示。此功能可以扩展到文档中的其他元素,利用 ~ 兄弟选择器,当下面的框根据是否选中开关更改颜色时会发生什么。
问题是: 如果两个切换都被选中,有没有办法让 CSS 将框着色为紫色?
这似乎是一种条件问题,例如如果只选中第一个框,然后正常着色,但是如果同时选中两个框,则为框着色。
也许只有当两个开关都打开时,使用几种叠加和混合模式才能使框变成紫色?我正在寻找仅使用 CSS 的解决方案。任何帮助深表感谢。

最佳答案

是的,通过分配 #blue:checked ~ #red:checked ~ hr { background-color: #d2d; } .请注意,CSS 顺序必须遵循 HTML 文档中输入的顺序:

*, *::after {
box-sizing: border-box;
transition-property: all;
transition-duration: 0.5s;
}
html, body {
margin: 0;
font-family: Arial;
text-transform: capitalize;
}
input { display: none; }
hr, label, label::after {
display: block;
width: 6rem;
height: 6rem;
border-style: none;
background-color: #444;
border-radius: 1rem;
}
div, section {
display: flex;
justify-content: center;
align-items: center;
margin: 0 1rem;
}
label, label::after {
position: relative;
width: 3rem;
height: 2rem;
margin: 1rem;
background-color: #eee;
content: '';
}
<style>
label::after {
transform: scale( 0.6 );
left: 0rem;
width: 2rem;
margin: 0;
background-color: #fff;
}
label:hover { background-color: #ddd; }
#blue:checked ~ div [ for='blue' ],
#red:checked ~ div [ for='red' ] { background-color: #222; }
#blue:checked ~ div [ for='blue' ]::after,
#red:checked ~ div [ for='red' ]::after {
left: 1rem;
}
#blue:checked ~ div [ for='blue' ]:hover,
#red:checked ~ div [ for='red' ]:hover {
background-color: #000;
}
#blue:checked ~ hr { background-color: #0be; }
#red:checked ~ hr { background-color: #d02; }
#blue:checked ~ #red:checked ~ hr { background-color: #d2d; }
</style>
<input type='checkbox' id='blue'> <input type='checkbox' id='red'> <hr>
<div>
<section>
<h2>blue</h2> <label for='blue'></label>
</section>
<section>
<h2>red</h2> <label for='red'></label>
</section>
</div>

关于html - 仅 CSS 条件切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64924774/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com