Horizontal scroll in a parent div containing floated child divs

痞子三分冷 提交于 2019-12-11 11:18:37

问题


I have the following code:

HTML

<div id="parent">
    <div class="child">2001</div>
    <div class="child">2O02</div>
    <div class="child">2003</div>
    <div class="child">2004</div>
    <div class="child">2005</div>
</div>

CSS

#parent{
width:100%;
overflow:auto;
}

.Child{
float:left;
width:20px;
}

The child content is always overflowing, and this is good. But I want to be able to scroll horizontally in the parent div, to see the last child divs.

Here is a screenshot of the result as I have it:

Screenshot

How can I do that to make is scroll horizontally?


回答1:


If you want all childs in a single row, you have to add second "parent" DIV and set it's width (combined widths of all children for a single row):

HTML:

<div id="parent">
    <div id="parent2">
        <div class="child">2001</div>
        <div class="child">2002</div>
        <div class="child">2003</div>
        <div class="child">2004</div>
        <div class="child">2005</div>
    </div>
</div>

CSS:

#parent{
    width:100%;
    overflow:auto;
}


#parent2{
    width:1000px;
}

.child{
    float:left;
    width:200px;
}


来源:https://stackoverflow.com/questions/24190204/horizontal-scroll-in-a-parent-div-containing-floated-child-divs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!