100% width div gets cut off when I scroll right if

2019-02-16 20:21发布

I had a problem on my own site with a <div> that is supposed to have 100% width. The code for it is shown below.

#myDiv {
    width: 100%;
    height: 160px;
    background: #f48;
}

This works great except for the following issue:

When my browser window is smaller than the <div>'s content, and I scroll to the right then my <div> will move left with the rest of the page rather than occupying 100% of the width of the page's scrollable area.

This issue has been bugging me for a few hours and just recently I realized that many other sites have the same issue. Including Facebook.com and Stackoverflow.com just take a look at my snapshots below.

Facebook's Bug:

Facebook's 100% width bug

Stackoverflow's Bug:

Stackoverflow's 100% width bug

4条回答
Luminary・发光体
2楼-- · 2019-02-16 20:31

I've solved this issue for myself with display: inline-block. Here is example: http://jsfiddle.net/z01g0r2d/1/ . Note if you try to set width of the long content as, for example, 200% the solution stops working.

查看更多
爷的心禁止访问
3楼-- · 2019-02-16 20:32

How you tried this? Tested it in IE7:

html, body {
    position:relative;
    overflow:hidden;
    margin:0 auto !important;
}
查看更多
【Aperson】
4楼-- · 2019-02-16 20:37

I've tried width: auto; and it's helped.

查看更多
forever°为你锁心
5楼-- · 2019-02-16 20:47

Ok, solved it! I just have to set the min-width property to the width of that <div>'s content.

Thanks anyways guys!

查看更多
登录 后发表回答