Change parent div on input[type=checkbox]:checked with css [duplicate]

本小妞迷上赌 提交于 2019-12-06 17:27:28

问题


I can figure out how to make the parent div change when checkbox is checked :( Changing the following paragraph works fine.

Tried this approach without luck in Chrome:

HTML

​<div>
    <input type="checkbox​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​" checked>
    <p>Test</p>
</div>​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

CSS

div {
    background: pink;
    width: 50px;
    height:50px;
}
div + input[type=checkbox]:checked {
  background: green;
}

input[type=checkbox]:checked + p {
    background: blue;
}

​ http://jsfiddle.net/lajlev/3xUac/


回答1:


No way to select a parent with CSS only (until CSS4), so you must use JS..

See this post that talking about it here.




回答2:


try this :

html

<input type="checkbox" checked>
<div>
   <p>Test</p>
</div>

css

div {
    background: pink;
    width: 50px;
    height:50px;
}
input[type=checkbox]:checked + div {
  background: green;
}

input[type=checkbox]:checked + div p {
    background: blue;
}

demo



来源:https://stackoverflow.com/questions/10846127/change-parent-div-on-inputtype-checkboxchecked-with-css

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!