如何在父DIV中对齐水平DIV?

时间:2013-07-15 00:12:00

标签: html css

我想在父div #main中对齐水平DIVS并隐藏水平滚动 我试着这样做:http://jsfiddle.net/Ty9kg/30/

<div id="main">
    <div>1</div>
    <div>2</div>
    <div>3</div>
    <div>4</div>
    <div>5</div>
    <div>6</div>
</div>

#main {
    border:1px solid black;
    width:250px;
    height:150px;
    overflow-y:hidden;
    overflow-x:scroll;
}

    #main div {
        width:165px;
        height:120px;
        display:inline-block;
        float:left;
        background:#ccc;
        border:1px solid #ccc
    }

3 个答案:

答案 0 :(得分:1)

你想要the following吗?

#main {
    border:1px solid black;
    width:250px;
    height:150px;
    overflow-y:hidden;
    overflow-x:scroll;
    white-space: nowrap;
}

#main div {
    width:165px;
    height:120px;
    display:inline-block;
    background:#ccc;
    border:1px solid #ccc;    
    white-space: normal;
}

如果是这样,float在您的代码中是多余的。

答案 1 :(得分:0)

如果我理解正确,你想让灰色的盒子连续相邻吗?目前你已经给你的主div宽度为250px,内部div宽度为165px。由于显而易见的原因,他们不适合。

试试这个:

#main {
    border:1px solid black;
    overflow-y:visible;
    width:990px;
}

#main div {
    width:165px;
    height:120px;
    display:inline-block;
    background:#ccc;
    margin-right:-4px;
    padding:0;
}

http://jsfiddle.net/Ty9kg/32/

答案 2 :(得分:0)

我想你想要这个:

http://jsfiddle.net/umQ32/

HTML:

<div id="mainContainer">
    <div id="main">
        <div class="child"></div>
        <div class="child"></div>
        <div class="child"></div>
    </div>
</div>

的CSS:

#mainContainer{
    width:300px;
    height:120px;
    overflow-x:scroll;
}

#main{
    width:1200px;
    height:100px;
    background-color:blue;
}

.child{
    width:200px;
    height:100px;
    margin:0 5px 0 0;
    float:left;
    background-color:red;
}
相关问题