简体   繁体   English

HTML:元素周围的空白,如何去除?

[英]HTML: white space around elements, how to remove?

My webpage contains several divs.我的网页包含几个 div。 Some are set to width: 100%, so they fill the whole page width.有些设置为 width: 100%,因此它们会填满整个页面宽度。 But at the top of the page there is a small whitespace before the first element shows up.但是在页面的顶部,在第一个元素出现之前有一个小的空白。 Moreover, that same whitespace is visible left and right from elements spanning the whole page width.此外,相同的空白在跨越整个页面宽度的元素的左右可见。

I cannot figure out why.我不知道为什么。 If I set:如果我设置:

html, body {
width: 100%;
}

then the whitespace remains but the page is just stretched out to fit the image width of a div.然后空白仍然存在,但页面只是被拉伸以适应 div 的图像宽度。

Can anyone help?谁能帮忙? It's probably pretty simple but I must be overlooking something.这可能很简单,但我必须忽略一些东西。 Thank you.谢谢你。

EDIT: I must mention I'm using a parallax element.编辑:我必须提到我正在使用视差元素。 This uses a padding, so the image does fills the whole div and does not leave a black area on top.这使用了填充,因此图像确实填充了整个 div,并且不会在顶部留下黑色区域。 The HTML is: HTML 是:

<div class="js-background-1 container">
</div>

The CSS: CSS:

.container { 
    padding-top: 200px;
}

.js-background-1 { 
    background: transparent url(url/to/image) center 0 no-repeat;
}

And the javascript:和 javascript:

<script type="text/javascript">
var $window = $(window); 
var velocity = 0.4;
function update(){ 
    var pos = $window.scrollTop(); 

    $('.container').each(function() { 
        var $element = $(this); 
        var height = $element.height(); 

        $(this).css('backgroundPosition', '50% ' + Math.round((height - pos) * velocity) + 'px');
    });
}; 

$window.bind('scroll', update);

</script>

I used the tutorial from http://www.webdesign.org/how-to-create-a-parallax-scrolling-website.22336.html , so there is where it is from.我使用了来自http://www.webdesign.org/how-to-create-a-parallax-scrolling-website.22336.html的教程,所以它来自哪里。 I changed the HTML a bit for my website, but the rest is the same.我为我的网站稍微更改了 HTML,但 rest 是相同的。

I saw the comment about the margin and padding set to 0, but that leads to my div to have a blank space if you don't scroll far enough.我看到关于 margin 和 padding 设置为 0 的评论,但是如果你滚动得不够远,这会导致我的 div 有一个空白区域。

You must remove the margin on body : 您必须删除body上的边距:

body {
    padding:0;
    margin:0;
}

You can also remove padding and margin on html and body 您还可以删除htmlbody上的填充和边距

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

See it on jsfiddle 在jsfiddle上看到它

But I would not advise to use * (the universal selector) 但我不建议使用* (通用选择器)

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

This would remove padding and margins on all elements. 这将删除所有元素的填充和边距。

The good method is to always use at the begining of the file (I forgot to metion this): 好的方法是始终在文件的开头使用(我忘了这个):

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

This two line's at the begining of main CSS file fix many problem's that you can encounter. 这两行在主CSS文件的开头修复了你可以遇到的许多问题。 Hope it'll help you. 希望它能帮到你。

padding-bottom: 20px;
border: 1px solid red !important;
margin: 0;
padding: 0;
width: 100%;

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

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