【问题标题】:How to preserve sibling element position when one sibling is absolutely positioned?当一个兄弟姐妹绝对定位时,如何保留兄弟元素位置?
【发布时间】: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。

标签: html css


【解决方案1】:

实际上,我认为您实际上根本不需要使用定位。这一切都可以通过display:none实现

JSfiddle Demo

CSS

.container, .child, button {
    display:inline-block;
}

.child {
    vertical-align: middle;
    width: 100px; 
    height: 35px;
}

.child:hover {
    background: gray;

    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;
}

【讨论】:

  • 这不起作用,当您将鼠标悬停在孩子(图像)上时,两个兄弟姐妹会移动。
  • 兄弟姐妹在悬停时仍然会垂直移动一点(使用 Chrome)
  • 这是一个小问题......这只是确保高度相同的问题。尝试将.child 更改为 36px 高;
【解决方案2】:

因为图像元素是不悬停时唯一可见的元素,为了在悬停时保持兄弟的自然定位,您需要将该元素保持在绝对位置之外。

换句话说,将其他所有内容都包装在一个容器中,并使其成为绝对的。

<div class="child">
    <span class="d4"></span>
    <div class="child-inner">
        <label><input type="radio" name="radio" value="1"/>One</label>
        <label><input type="radio" name="radio" value="2"/>Two</label>
    </div>
</div>

为了隐藏.d4,使其 opacity=0.01。

.child {
    vertical-align: middle;
    width: 35px;
    height: 35px;
}

.child:hover .child-inner {
    background: gray;
    position:absolute;
    width: 100px;
    height: auto;
    top:0; left:0;
}

.child:hover > .d4 {
    opacity: 0.01;
}

在使用绝对位置时,使用显式topleft(或右或下)总是一个好主意,否则在旧的非标准浏览器中可能会有所不同。这意味着你需要得到你的.container 亲戚。 (作为.child绝对定位的参考点)

.container {
    position:relative;
}

演示:http://jsfiddle.net/cpctZ/15/

【讨论】:

  • 这行得通,并且在技术上回答了我的问题。虽然我担心我过于简化了我的问题.. 请参阅我编辑的问题。
  • 你知道如何在不使用另一个内部 div 的情况下完成这项工作,从而使 .d4 和无线电保持兄弟元素吗?
【解决方案3】:

我建议将按钮和文本输入都向右浮动(您必须交换顺序,请参阅 float: right 文档了解原因):

<button class="fr" name="sibling2">Button</button>
<input class="fr" type="text" name="sibling"/>

使用 CSS:

.container .fr{
float: right;
}

并用 CSS 将孩子向左浮动:

.child {
vertical-align: middle;
width: 35px;
height: 35px;
float:left;
}

我认为这是你想要的: JSFiddle:http://jsfiddle.net/cpctZ/32/

编辑:要让它在 Firefox 上运行,请使用

给容器一个宽度
.container{
width 250px;
}

更新小提琴:http://jsfiddle.net/cpctZ/54/

【讨论】:

  • 这个工作,即使我编辑的更复杂的例子,见jsfiddle.net/cpctZ/49
  • 我觉得我在这里修补漏洞,但我之前从未给出堆栈交换的答案,我渴望你选择它!我发现如果你给容器一个宽度: .container{ width:250px;它仍然适用于 Firefox。我会更新我的答案,但看到这个小提琴:jsfiddle.net/cpctZ/54
猜你喜欢
  • 1970-01-01
  • 2014-07-12
  • 2018-09-08
  • 1970-01-01
  • 2019-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-12
相关资源
最近更新 更多