css 溢出和 margin-left

标签 css overflow

我在父 div 中有三个 div,溢出:隐藏;我想展示第三个。所以我想,我可以给第一个 div 一个 margin-left,而另外两个将移到左边。

<div style="width:1000px;background:red;overflow:hidden;height:50px;">
    <div style="width:1000px;height:50px;float:left;margin-left:-2000px;">
    1
    </div>
    <div style="width:1000px;height:50px;float:left;">
    2
    </div>
    <div style="width:1000px;height:50px;float:left;">
    3
    </div>
</div>

但是它显示了第二个 div。但是如果我添加 margin-left:-1000px;到第二个 div 并替换 margin-left:-2000px;到第一个 div 上的 -1000px 它将正常工作。我不明白为什么。

最佳答案

当你设置 -1000px 到 div 时,这个 div 不再处于相对位置,所以另一个 div 假定相对于父级的位置。

用于测试的 fiddle 示例: http://jsfiddle.net/FvBwC/5/

<div class="parent">    
    <div class="one">1</div>
    <div class="two">2</div>
    <div class="three">3</div>
</div>

.parent { width:1000px; background:red; overflow:hidden; height:50px;  }
.one, .two, .three { width: 1000px; height: 50px; float: left; }
.one { background: blue; margin-left: -1000px; } 
.two { background: yellow; margin-left: -1000px; }
.three { background: green; }

但为了更好的解决方案,我会选择 display: none in CSS 或 .hide() in jQuery。

关于css 溢出和 margin-left,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20382777/

相关文章:

html - 水平滚动CSS?

css - 正文溢出

php - MPDF - 第一页的不同背景

html - Bootstrap pills 子菜单不起作用

javascript - 将 CSS/CSS3/JS 链接到 Google App Engine?

webkit - css 位置、边界半径、溢出在 webkit 中不受尊重 - 其他解决方案?

java - 对点间隔执行二进制搜索

javascript - 在 CSS 中传递引用?

html - 单击按钮时显示元素

具有 100% 溢出滚动的 HTML 表格,仅适用于表格