CSS margin-top Property

CSS margin-top property sets the top margin to HTML Element(s).

The syntax to specify a value for margin-top property is

margin-top: value;

The following table gives the possible values that could be given to margin-top property.

ValueDescriptionExamples
lengthAny valid CSS length unit. Refer CSS Length Units tutorial.margin-top: 10px;margin-top: 5pt;margin-top: 2cm;
Percentage ValueMargin in percent of the width of the containing element.margin-top: 20%;
initialSets margin-top to default value.margin-top: initial;
inheritInherits margin-top value from its parent element.margin-top: inherit;

Examples

In the following examples, we set top margin to HTML Element(s) with a few allowed values for margin-top CSS property.

ADVERTISEMENT

Simple Example

The following is a simple example, where we set margin-top for paragraph with 50px.

index.html

Different Values for margin-top

The following is a simple example, where we set different margin-top values for paragraphs.

index.html

margin-top property with CSS Length Units

In the following example, we apply margin-top property to paragraphs with a few different CSS Length Unit values.

index.html

margin-top property with Percentage Values

In the following example, we apply margin-top property to paragraphs with a few different percentage values.

If margin-top is specified in percentage values, and if width of the HTML element changes, then the margin also changes. For example, try to resize the browser window, and observe the top margin for these HTML Elements.

index.html

margin-top property for a few HTML elements

In the following example, we take a few HTML elements like h1, p, a, and pre, and set margin-top for these.

index.html

Conclusion

In this CSS Tutorial, we learned about margin-top property, and how to use this property for HTML Elements, with examples.