CSS – Margins

Learn about CSS Margins.  A margin defines the space around the content of an element. It has options such as margin-top, margin-bottom, margin-right, and margin-left to set margins for top, bottom, right, and left respectively. The below figure explains wherein margin, padding, and border get placed around a content:

CSS Margins

Let us see how to set margins on a web page around content.

CSS margin-top property (Set Top Margin)

To set the top margin of an element on a web page, use the margin-top property. Let us see an example and set the top margin:

Output

CSS Margins Top Property

CSS margin-bottom property (Set Bottom Margin)

To set the bottom margin of an element on a web page, use the margin-bottom property. Let us see an example and set the bottom margin:

Output

CSS Margins Bottom Property

CSS margin-left property (Set Left Margin)

To set the left margin of an element on a web page, use the margin-left property. Let us see an example and set the left margin:

Output

CSS Margins Left roperty

CSS margin-right property (Set Right Margin)

To set the right margin of an element on a web page, use the margin-right property. Let us see an example and set the right margin:

Output

CSS Margin Right Property

CSS Margins Shorthand Property

In CSS, you can easily set all the margins in a single line using the margins property itself. It allows you to set:

  • Top Margin
  • Bottom Margin
  • Left Margin
  • Right Margin

Let us see an example to set margins:

Output

CSS Margins shorthand property

If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.


Read More:

CSS - Comments
CSS - Padding
Studyopedia Editorial Staff
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment

Discover more from Studyopedia

Subscribe now to keep reading and get access to the full archive.

Continue reading