Horizontal scroll in a parent div containing float

2019-08-09 01:12发布

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条回答
家丑人穷心不美
2楼-- · 2019-08-09 01:39

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;
}
查看更多
登录 后发表回答