Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

13. CSS - Margins

Lesson 13 of 34 in the free CSS Customization: Zero to Hero notes on Siksha Sarovar, written by Rohit Jangra.

CSS Margins

Margins are used to create space around elements, outside of any defined borders.

1. Margin - Individual Sides

CSS has properties for specifying the margin for each side of an element:

  • margin-top
  • margin-right
  • margin-bottom
  • margin-left

Values can be:

  • auto: The browser calculates the margin.
  • length: Specifies a margin in px, pt, cm, etc.
  • %: Specifies a margin in % of the width of the containing element.
  • inherit: Specifies that the margin should be inherited from the parent element.

2. Margin - Shorthand Property

To shorten the code, it is possible to specify all the margin properties in one property.

Four values: margin: 25px 50px 75px 100px;

  • top margin is 25px
  • right margin is 50px
  • bottom margin is 75px
  • left margin is 100px

Three values: margin: 25px 50px 75px;

  • top: 25px
  • right and left: 50px
  • bottom: 75px

Two values: margin: 25px 50px;

  • top and bottom: 25px
  • right and left: 50px

One value: margin: 25px;

  • all four margins are 25px

3. The auto Value

You can set the margin property to auto to horizontally center the element within its container. The element will then take up the specified width, and the remaining space will be split equally between the left and right margins.

div {
  width: 300px;
  margin: auto;
}