Margin Properties

PropertyPossible ValuesDescription
marginauto, length (px, em, %), inheritShorthand for setting all four margins (top, right, bottom, left)
margin-topauto, length (px, em, %), inheritSets the top margin of an element
margin-rightauto, length (px, em, %), inheritSets the right margin of an element
margin-bottomauto, length (px, em, %), inheritSets the bottom margin of an element
margin-leftauto, length (px, em, %), inheritSets the left margin of an element

Shortcut Values

Value FormatExampleMeaning
margin: 10px;margin: 10px;All sides = 10px
margin: 10px 20px;margin: 10px 20px;10px (top & bottom), 20px (left & right)
margin: 10px 20px 30px;margin: 10px 20px 30px;10px (top), 20px (left & right), 30px (bottom)
margin: 10px 20px 30px 40px;margin: 10px 20px 30px 40px;10px (top), 20px (right), 30px (bottom), 40px (left)

Example:

.box {
    width: 200px;
    background-color: lightblue;
    margin: 20px auto; /* Adds 20px margin on top & bottom, centers horizontally */
    padding: 10px;
}
✔ 20px margin on top & bottom
✔ Horizontally centered with auto
Online Exam Quiz for One day Exam Online Typing Test CCC Online Test 2026 Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level Online Test in Hindi Best Website and Software Company in Allahabad