在父级中显示div元素并隐藏溢出

时间:2013-09-07 08:12:42

标签: html css

我有div个班级container。我在divs内还有3个.container。我想要的是显示内部div float: left,以便在.container内可以看到2个div标签,第三个标签是不可见的,并且放在可见的前2个div标签的右侧。我正在尝试以下代码,但它使所有标签始终可见。

jsfiddle

<div class="container">
        <div class="div"></div>
        <div class="div"></div>
        <div class="div"></div>
    </div>

CSS

.container {
            position: relative;
            width: 405px;
            height: 500px;
            background: red;
            margin: 0 auto;
            overflow: hidden;
        }
        .div {
            width: 200px;
            height: 200px;
            background: blue;
            float: left;
            border: 1px solid red;
        }

我希望上面看起来像这样

enter image description here

3 个答案:

答案 0 :(得分:3)

你可以通过其他方式这样做,

<强> HTML

<div class="container">
    <div class="innerContainer">
        <div class="div"></div>
        <div class="div"></div>
        <div class="div"></div>
    </div>
</div>

<强> CSS

.container {
    position: relative;
    width: 405px;
    height: 500px;
    background: red;
    margin: 0 auto;
    overflow: hidden;
}
.innerContainer {
    position: relative;
    width: 605px;
    height: 500px;
    overflow: hidden;
}
.div {
    width: 200px;
    height: 200px;
    background: blue;
    float: left;
    border: 1px solid red;
}

点击此处http://jsfiddle.net/nftp6/8/

答案 1 :(得分:2)

使用:

display: inline-block;
white-space: nowrap;

为此效果

小提琴:

http://jsfiddle.net/Hive7/nftp6/5/

答案 2 :(得分:1)

使用display:inline-block代替浮点数并将white-space:nowrap设置为容器:

.container {
    position: relative;
    width: 405px;
    height: 500px;
    background: red;
    margin: 0 auto;
    overflow: hidden;
    white-space: nowrap;
}
.div {
    width: 200px;
    height: 200px;
    background: blue;
    display: inline-block;
    border: 1px solid red;
}

<强> Demo fiddle

现在您很可能会面临一些空白问题,请阅读this answer以了解多种处理方法

相关问题