为什么负边距会影响我的页面宽度? [英] Why do negative margins affect my page width?

查看:176
本文介绍了为什么负边距会影响我的页面宽度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请参考以下示例

在这里,一个外部div宽度200px是为了建立我们的页面宽度。它包含一个内部宽度为400像素,但左/右负边距为-100像素。

In it, an outer div 200px wide is meant to establish our page width. It contains an inner div 400px wide, but with left/right negative margins of -100px.

我的预期最终结果是浏览器注册的总内容宽度为200像素,而不是400像素,但水平滚动条在窗口调整为小于400像素时显示。这是怎么回事?

My intended end result is that the browser register total content width at 200px, not 400px, but horizontal scrollbars show up as soon as the window is resized to less than 400px. What is going on here?

推荐答案

Gareth的回答是正确的。即使有负边距,div仍然是标准页面流的一部分,并且不会因布局而被忽略。

Gareth's answer is correct. Even with negative margin, the div is still part of the standard page flow and will not be ignored with respect to layout. Genuine page content cannot be ignored for scrolling purposes.

但是,如果你这样做是为了美观,比如在页面的两侧有一个阴影,超出您的最大宽度,这可以通过背景实现 - 应该有帮助。

However, if you're doing this for an aesthetic, such as having a shadow down the sides of the page that extends beyond your max width, this can be achieved with a background - this question should help.

这篇关于为什么负边距会影响我的页面宽度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆