如何为页面主体(html)设置边距?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5670927/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 08:00:15  来源:igfitidea点击:

How to margin the body of the page (html)?

html

提问by Buffon

I used the following

我使用了以下

<body topmargin="0" leftmargin="0" rightmargin="0">

which works only on IE6. I want it to work with firefox and opera.

仅适用于 IE6。我希望它与火狐和歌剧一起工作。

I attempted the following:

我尝试了以下操作:

<style type="text/css">
.header {
    width: 100%;
    background-color: #3B5998;
    height: 35px;
    margin-top:0;
}
.style1 {
    margin-right: 38px;
}
.style2 {
    width: 100%;
    background-color: #3B5998;
    height: 35px;
}


</style>

<div dir="rtl" class="style2" style="width: 100%">
<p align="center"style="width: 92px; height: 32px; font-family: Arial, Helvetica, sans-serif; font-size: 20px; color: #EFF1F6;" class="style1"></p>
</div>

</body>

回答by Damb

For start you can use:

首先,您可以使用:

<body style="margin:0;padding:0">

Once you study a bit about css, you can change it to:

一旦你学习了一些关于 css 的内容,你可以将其更改为:

body {margin:0;padding:0}

in your stylesheet.

在您的样式表中。

回答by Ady Ngom

Yeah a CSS primer will not hurt here so you can do two things: 1 - within the tags of your html you can open a style tag like this:

是的,CSS 入门不会在这里受到伤害,因此您可以做两件事:1 - 在 html 的标签中,您可以打开这样的样式标签:

<style type="text/css">
  body {
   margin: 0px;
  }
  /*
   * this is the same as writing
   * body { margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;}
   * I'm adding px here for clarity sake but the unit is not really needed if you have 0
   * look into em, pt and % for other unit types 
   * the rules are always clockwise: top, right, bottom, left
  */
</style>

2- the above though will only work on the page you have this code embeded, so if if you wanted to reuse this in 10 files, then you will have to copy it over on all 10 files, and if you wanted to make a change let's say have a margin of 5px instead, you would have to open all those files and make the edit. That's why using an external style sheet is a golden rule in front end coding. So save the body declaration in a separate file named style.css for example and from your add this to your html instead:

2- 以上虽然只适用于您嵌入了此代码的页面,因此如果您想在 10 个文件中重复使用它,那么您必须将其复制到所有 10 个文件上,如果您想进行更改假设有 5px 的边距,您必须打开所有这些文件并进行编辑。这就是为什么使用外部样式表是前端编码的黄金法则。因此,将主体声明保存在名为 style.css 的单独文件中,例如,将其添加到 html 中:

<link rel="stylesheet" type="text/css" href="style.css"/>

Now you can put this in the of all pages that will benefit from these styles and whenever needed to change them you will only need to do so in one place. Hope it helps. Cheers

现在,您可以将其放在将从这些样式中受益的所有页面中,并且无论何时需要更改它们,您只需要在一个地方进行。希望能帮助到你。干杯

回答by Uday Hiwarale

I hope this will be helpful.. If I understood the problem

我希望这会有所帮助.. 如果我理解了这个问题

html{
     background-color:green;
}

body {
    position:relative; 
    left:200px;    
    background-color:red;
}
div{
    position:relative;  
    left:100px;
    width:100px;
    height:100px;
    background-color:blue;
}

http://jsfiddle.net/6M6ZQ/

http://jsfiddle.net/6M6ZQ/

回答by Billy Moon

Html for content, CSS for style

Html 表示内容,CSS 表示样式

<body style='margin-top:0;margin-left:0;margin-right:0;'>

回答by Blender

<body topmargin="0" leftmargin="0" rightmargin="0">

I'm not sure where you read this, but this is the accepted way of setting CSS styles inline is:

我不确定你在哪里读到的,但这是内联设置 CSS 样式的可接受方式:

<body style="margin-top: 0px; margin-left: 0px; margin-right: 0px;">

And with a stylesheet:

并使用样式表:

body
{
  margin-top: 0px;
  margin-left: 0px;
  margin-right: 0px;
}

回答by k to the z

You need to use css. It's how modern web design gets things done.

你需要使用 css。这就是现代网页设计完成工作的方式。

This is a basic csswalk through.

这是一个基本的css 演练

Your html file would be like:

您的 html 文件将类似于:

(really simple html)

(非常简单的html)

    <html>
    <head>
    <link rel="stylesheet" type="text/css" href="mystyle.css" />
    </head>
    <body>
    </body>
    <html>

Your css file (mystyle.css) would look like:

您的 css 文件 (mystyle.css) 将如下所示:

body 
{
 margin-top:0px;
 margin-left:0px;
 margin-right:0px;

}

回答by alex

Try using CSS.

尝试使用CSS

body {
   margin: 0 0 auto 0;
}

The order is clockwise from the top, so top right bottom left.

顺序是从顶部顺时针方向,所以top right bottom left

回答by Scherbius.com

I would say: (simple zero will work, 0px is a zero ;))

我会说:(简单的零可以工作,0px 是零;))

<body style="margin: 0;">

but maybe something overwrites your css.(assigns different style after you ;))

但也许有些东西会覆盖你的 css。(在你之后分配不同的风格;))

If you use Firefox - check out firebug plugin.

如果您使用 Firefox - 查看 firebug 插件。

And in Chrome - just right-click on the page and chose "inspect element"in the menu. Find BODY in elements tree and check its properties.

在 Chrome 中 - 只需右键单击页面并在菜单中选择“检查元素”。在元素树中找到 BODY 并检查其属性。