简体   繁体   English

为什么我的 Divs 没有跨越 100% 的宽度?

[英]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.我试图让一个 div 跨越网页宽度的 100%,但它没有达到。 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.我正在尝试制作类似于 Stack Overflow 的 100% 页面宽度“警报”的东西,它会在您获得新徽章时告诉您。

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

Code for the pink banner div粉红色横幅 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).您的html 正文标签可能有填充或边距css。您应该将它们设置为零(0)。 I hope it will help.我希望它会有所帮助。

Looks like you have padding on your body, which is preventing the div from expanding all the way.看起来你的身体上有填充物,这阻止了 div 一直扩展。

In your css file, ensure that you don't have any padding on the body.在您的 css 文件中,确保正文中没有任何填充。 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:只需为主体添加一个 css 即可删除填充和边距:

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.确保粉红色横幅 div 的父容器具有 0 内边距和 0 外边距。 In this case I'm assuming the container for your pink banner is just the body tag.在这种情况下,我假设您的粉红色横幅的容器只是 body 标签。 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.粉红色横幅的 html 也不正确。 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.在 CSS 文件的开头。

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:我总是键入每个 CSS 文档的开头:

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

I never have any problems with unwanted outer margins or padding with that code.对于不需要的外边距或使用该代码进行填充,我从来没有遇到过任何问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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