开发者

Why is a horizontal scroll bar appearing if none of the elements are wider than the 960px container?

开发者 https://www.devze.com 2023-03-21 04:57 出处:网络
Everything is wrapped in a div with an id=\"main_wrap\" the main wrap has this style: #main_wrap{ margin:0 auto;

Everything is wrapped in a div with an id="main_wrap"

the main wrap has this style:

#main_wrap{
  margin:0 auto;
  width:960px;
  position:relative;
}

S开发者_开发技巧ince everything is wrapped inside of this div, I don't understand why a horizontal scroll bar would appear at the bottom.


If any of the children have borders, padding (or sometimes margins) applied to them, that adds to the width and height. Using overflow: hidden; would avoid the scroll bars. But, if something has too much width, you probably want to find where it is coming from to avoid possible problems in the future.

Note that you may be able to use box-sizing: border-box to prevent borders and padding from adding additional width (or height). This is particularly useful when you are using something like width: 100%, but width: 100% also isn't necessary in most cases - elements with display: block applied will fill the width by default AND keep padding and borders from adding additional width.

For example:

html, body { margin: 0; padding: 0; }

div {
  background: #111;
  color: #eee;
  padding: 1em; /* This doesn't add to width - still at 100% width */
  border: 2px solid #5e5;  /* This also doesn't add to width - still at 100% width */
}
<div>Test</div>


Try add overflow hidden:

#main_wrap{
    margin:0 auto;
    width:960px;
    position:relative;
    overflow: hidden;
}

That should work (but is hacky).


If the elements are side by side and have a combined width or , as BDawg says margins or paddings, that cause it to exceed 960px a scroll bar could appear. Post your entire code and we can figure it out very quickly. Hiding the overflow should work but it would not really explain why the scroll bar is appearing. Giving us your entire markup will help find the root of the problem.


Somewhere you've left any DOM elements unseen which occupies the extra width. it's better to find and fix the children than to use overflow:hidden . overflow:hidden will hide the scroll bar if user zooms in the page.


Working 100%

if you are using bootstrap it takes margin-left & right 15px (or) -15px


you need to change 15px to 0px

Example

.main_wrap .row{
  margin-left:0px;
  margin-right:0px;
}


If you change width to 100% it helped you, but if you don't want,
just try add external <div style="width:100%"></div>
like this:

    <div style="width:100%">
       <div class="main_wrap">
          %your content%
       </div>
    </div>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号