简体   繁体   中英

Why dont my Divs span 100% of the width?

I have a really dumb question to ask.

I am trying to make a div span 100% of the width of a webpage, but it doesn't reach. I want it to be dynamic (not specify the width in px) and I definitely don't want it to make a horizontal scroll bar appear.

I'm trying to make something similar to Stack Overflow's 100% page width 'alerts' which tell you when you've earned a new badge.

Screenshot of my site: 我的网站截图。请注意粉红色横幅和绿色横幅如何在显示天蓝色背景的地方留下空隙。

Code for the pink banner div

<div width='100%' style="padding:0px; background-color:FF0099; background-image:url('pics/pink_bg.png'); ">
  &nbsp;
</div>

your html body tag might have padding or margin css. you should set those to zero (0). I hope it will help.

Looks like you have padding on your body, which is preventing the div from expanding all the way.

In your css file, ensure that you don't have any padding on the body. If you don't have anything you can try adding this:

body {
    padding: 0;
    margin: 0;
}

Just add a css for the body to remove the padding and margin:

body {
padding: 0px;
margin: 0px;
}

Also you can apply just to left, right top and bottom margins:

body {
padding-top:0px;
padding-right:0px;
padding-bottom: 10px;
padding-left: 0px;
}

Actually it's quite easy. Make sure the parent container for pink banner div has 0 padding and 0 margin. In this case I'm assuming the container for your pink banner is just the body tag. Now copy the following snippet inside your head section of the page.

<style type="text/css">
    body
    {
        margin:0px;
        padding:0px;
        }
</style>

The html for your pink banner is also not correct. Replace

<div width='100%' style="padding:0px; background-color:FF0099; background-image:url('pics/pink_bg.png'); ">
  &nbsp;
</div>

with

<div style="padding:0px; margin:0px; width=100%; height:25px; background-color:#FF0099; background-image:url('pics/pink_bg.png');" >
  &nbsp;
</div>

Type

body {
    min-height:100%;
    height:auto;
    margin:0;
    padding:0;
}

at the beginning of your CSS file.

I was having the same issue, I had a great big white line on the right hand side of my page. I found the following which was a life saver:

html, body {
  width: 100%;
  padding: 0px;
  margin: 0px;
  overflow-x: hidden;
}

A the beginning of each CSS document I always type:

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

I never have any problems with unwanted outer margins or padding with that code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM