Chinaunix首页 | 论坛 | 博客
  • 博客访问: 99029
  • 博文数量: 34
  • 博客积分: 925
  • 博客等级: 准尉
  • 技术积分: 350
  • 用 户 组: 普通用户
  • 注册时间: 2011-08-15 11:52
文章分类

全部博文(34)

文章存档

2011年(34)

我的朋友

分类: 系统运维

2011-08-18 13:28:20

CSS Margin

The following CSS margin codes demonstrate the various CSS properties you can use to apply styles to the border of any HTML element.

Margins define the space around the element. CSS margins are specified in a similar way to borders - they can be set individually for each side, or all sides at once.


Setting Margins on all Sides
  1. <div style="border:1px solid blue;">
  2. <p style="border:1px solid orange;margin:20px;">
  3. This text has a margin of 20 pixels on all four sides.
  4. It is nested within a div with a border to make it easier to see the effect of the margin.
  5. </p>
  6. </div>

This results in:

This text has a margin of 20 pixels on all four sides. It is nested within a div with a border to make it easier to see the effect of the margin.

Setting Margins for Each SideIf you don't want the margin settings to be applied to all four sides, or if you want each side to have different margins applied, you can use the following properties:
Example:
  1. <p style="border:1px solid orange;margin-left:60px;">This text has a left margin of 60 pixels.</p>

Shorthand Property

This method uses a shorthand property for setting , , , and in the one place. This method is quicker. It also uses less code than the previous method. Actually, it's the same property that we used in our first example (i.e. the property). The only difference is that we apply multiple values against it.

Code:

  1. <div style="border:1px solid blue;width:200px;">
  2. <p style="border:1px solid orange;margin:40px 10px 1px 40px;">
  3. This text has a different sized margin for each side.
  4. It is nested within a div with a border to make it easier to see the effect of the margin.
  5. </p>
  6. </div>

This text has a different sized margin for each side. It is nested within a div with a border to make it easier to see the effect of the margin.

Variations

You don't need to provide different values for all four sides. You can provide one, two, three, or four values. Here's how it works:

If there is only one value, it applies to all sides. If there are two values, the top and bottom margins are set to the first value and the right and left margins are set to the second. If there are three values, the top is set to the first value, the left and right are set to the second, and the bottom is set to the third. If there are four values, they apply to the top, right, bottom, and left, respectively.

In other words:

margin:10px;
  • All four sides have a margin of 10 pixels.
margin:10px 20px;
  • Top and bottom have a margin of 10 pixels.
  • Right and left have a margin of 20 pixels.
margin:10px 20px 30px;
  • Top is 10px
  • Left and right are 20px
  • Bottom is 30px
margin:10px 20px 30px 40px;
  • Top is 10px
  • Right is 20px
  • Bottom is 30px
  • Left is 40px

阅读(301) | 评论(0) | 转发(0) |
0

上一篇:CSS Border

下一篇:CSS Height and Width

给主人留下些什么吧!~~