【发布时间】:2014-05-30 21:09:38
【问题描述】:
在下面的 sn-p 中,child div 正常定位,直到它变为绝对定位时 :hovered 。此标记背后的原因是在我无法使用<select>(以及其他限制)的有限环境中模拟弹出样式。
当child 悬停时,兄弟元素会跳来跳去,这是意料之中的,因为块的内容发生了变化。
但是我怎样才能保持他们的定位呢?也就是说,当child 悬停时,我可以添加什么CSS来防止兄弟姐妹跳来跳去。
Javascript 也是不允许的,所以请不要使用 JS 回答。
跳到下面的编辑
HTML:
<div class="container">
<div class="child">
<span class="d4"></span>
<label><input type="radio" name="radio" value="1"/>One</label>
<label><input type="radio" name="radio" value="2"/>Two</label>
</div>
<input type="text" name="sibling"/>
<button name="sibling2">Button</button>
</div>
CSS:
.container, .child, button {
display:inline-block;
}
.child {
vertical-align: middle;
width: 35px;
height: 35px;
}
.child:hover {
background: gray;
position:absolute;
width: 100px;
height: auto;
}
.child:hover > .d4 {
display: none;
}
.child label {
display:none;
}
.child:hover label {
display: inline-block;
}
.d4 {
background-position: -411px -1px;
width: 35px;
height: 35px;
background-image: url("https://i.imgur.com/zkgyBOi.png");
background-repeat: no-repeat;
color: transparent;
display: inline-block;
}
这是一个小提琴:http://jsfiddle.net/cpctZ/1/
编辑
新小提琴:http://jsfiddle.net/cpctZ/48/
我过于简化了我原来的问题。事实上,child 中有多个子级代表各种下拉选项。
图像和收音机必须是兄弟元素,以便根据所选收音机有条件地显示正确的图像:
.child:not(:hover) input[name="radio"][value="1"]:checked ~ .d4 {
display: block;
}
.child:not(:hover) input[name="radio"][value="2"]:checked ~ .d8 {
display: block;
}
注意兄弟选择器。如果图像放置在比收音机更外的 div 中,则无法说 “如果选中了相应的收音机,则显示图像”。
更新的 HTML
<div class="container">
<div class="child">
<input type="radio" name="radio" value="1" checked="true" />
<label>d4</label>
<input type="radio" name="radio" value="2" />
<label>d8</label>
<div class="d4"></div>
<div class="d8"></div>
</div>
<input type="text" name="sibling" />
<button name="sibling2">Button</button>
</div>
所以问题仍然存在:\ 当孩子悬停时,如何保持这些兄弟元素不移动,同时保持上述功能?
【问题讨论】:
-
可以给当前的html添加html吗?
-
CSS3 是允许的,是的,我可以添加标记和样式,只是没有 javascript。