HTML CSS examples for CSS Property:margin
The margin CSS property sets the margin on all four sides of the element.
It is a shorthand property for margin-top, margin-right, margin-bottom, and margin-left property.
The following table summarizes the margin Property.
Item | Value |
---|---|
Default value: | 0 |
Applies to: | All elements except those with table display types other than table-caption, table, and inline-table. It also applies to ::first-letter. |
Inherited: | No |
Animatable: | Yes. |
The syntax of the property is as follows:
margin: [ length | percentage | auto ] 1 to 4 values | initial | inherit
This shorthand notation can take one, two, three, or four whitespace separated values.
The following table describes the values of this property.
Value | Description |
---|---|
margin-top | Sets the top margin of the element. |
margin-right | Sets the right margin of the element. |
margin-bottom | Sets the bottom margin of the element. |
margin-left | Sets the left margin of the element. |
initial | Sets this property to its default value. |
inherit | take the value of its parent element margin property. |
The example below shows the margin property.
<!DOCTYPE html> <html lang="en"> <head> <title>Example of CSS margin Property</title> <style type="text/css"> h1 {<!-- w ww . j av a 2s. c o m--> margin: 25px; } p { margin: 50px 100px; } </style> </head> <body> <h1>This is a heading</h1> <p>This is a paragraph.</p> <p><strong>Note:</strong> Play with the margin property value to see how it works.</p> </body> </html>