为什么在< body>上存在默认边距元素? [英] Why is there a default margin on the <body> element?

查看:164
本文介绍了为什么在< body>上存在默认边距元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的大部分开发生涯中,我只需要涉足基于Web的前端Web界面开发.一直困扰我的一件事是,不断需要重置基于浏览器的样式假设",在这些默认设置开始影响我的页面布局之前,我常常忘记了这一点.

For most of my development career, I've only had to dabble in front-end, web based UI development. One thing has always bugged me is the constant need to reset browser-based styling "assumptions", which I've often forgotten until these defaults started effecting the layout of my pages.

大多数浏览器所做的最大假设是HTML <body>元素应有一个空白,几乎总是8px.

The biggest assumption that most browsers make is that the HTML <body> element should have a margin, which almost always is 8px.

此默认保证金的设计决定是什么?

What was the design decision for this defaulted margin?

实际上,这种边距是非常不希望的,以至于将这种样式包含在网站的每个页面中都包含在.css文件中,这实际上是一种标准做法:

In fact, this margin is so undesirable that it is essentially standard practice to include this type of styling in a .css file which is included with every page of a site:

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

恕我直言,逻辑要求大多数开发人员希望自己可以使用完整的空白面板并自行决定格式.浏览器默认在body元素周围留出空白,如果他们不重置布局,设计人员必须对间距保持谨慎,并添加代码以为其内部元素计算正确的布局.

IMHO, logic would dictate that most developers would want a full, blank slate that they can work with and format at their own discretion. With the browser defaulting a margin around the body element, the designer must remain conscientious of the spacing and add code to calculate correct layout for their inner elements if they don't reset the layout.

出于什么原因,HTML浏览器的原始开发者或最初的HTML规范决定在所有主体元素上使用此默认边距?

For what reason did the original developers of HTML browsers, or the initial HTML spec, decide to throw this default margin on all body elements?

推荐答案

语言最初是为独立工作而构建的.这样一来,您就可以在技术上将特定语言用于仅用于特定用途的语言.对于HTML,仅允许您在浏览器上显示内容.另一方面(您一定知道),CSS旨在创建所有美化过程.因此,考虑到这一点,任何人都应该能够编写完全没有任何CSS的HTML文档,并且浏览器应以最清晰的形式显示它.现在,为了使这种情况尽可能一致,浏览器具有一个称为合理的默认值"的东西.这些默认值包括正文的边距和内边距,某些字体,最清晰的字体大小等.它们留给您根据需要使用CSS覆盖.

Languages are originally built to work independently. So that you could technically use that particular language for what is intended for only. In the case of HTML, it is only supposed to allow you to display something on a browser. CSS on the other hand (and as you surely know), is intended to create all the beautification process. So, with that in mind, Anyone should be able to write an HTML document without any CSS at all and browsers should display it in the most legible form. Now, for this to happen as consistent as possible, browsers have something called "sane defaults". These defaults cover the margin and padding on the body, some fonts, the most legible font size, etc. And they leave it up to you to overwrite as needed with CSS.

在主体上没有空白和填充的情况下,所有内容都将完全刷新到浏览器窗口.如果您正在阅读文档,那不是最佳实践.

Without the margin and padding on the body, everything would be completely flushed to the browser window. That is not the best practice if you were reading a document.

编辑

下面的链接显示了Firefox和Webkit CSS的默认设置.这将帮助您排除那些不知道它们来自何处或为何存在的默认值.

The links below show Firefox and Webkit CSS defaults. This will help you troubleshoot those defaults that you have no idea where they came from or why they exist.

Webkit

Firefox

这篇关于为什么在&lt; body&gt;上存在默认边距元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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