Skip to content

Margins and Padding

We have seen some padding and margin action while we were going through box model article, let’s look at these properties in detail in this one.

Margins and padding are fundamental CSS properties used to create space around elements, they help in spacing and lay outing websites better.

The margin property creates space outside the border of an element, effectively pushing other elements away.

  1. margin-top: Sets the margin at the top of an element.
  2. margin-right: Sets the margin on the right side of an element.
  3. margin-bottom: Sets the margin at the bottom of an element.
  4. margin-left: Sets the margin on the left side of an element.

Example:

.example {
margin-top: 20px;
margin-right: 15px;
margin-bottom: 10px;
margin-left: 5px;
}

The margin shorthand property allows you to set the margins for all four sides of an element in a single declaration.

Syntax:

div {
margin: top right bottom left;
}

If one value is provided, it applies to all four sides,margin: 20px; here all sides get 20px margin.

If two values are provided, the first applies to the top and bottom, and the second to the left and right, margin: 10px 15px; top and bottom gets 10px margin left and right gets 15px margin.

If three values are provided, the first applies to the top, the second to the left and right, and the third to the bottom. margin: 10px 15px 5px; top gets 10px, left and right get 15px, and bottom gets 5px margin.

If four values are provided, they apply to the top, right, bottom, and left, respectively. margin: 10px 15px 5px 2px; each side gets defined values as margins, top 10px right 15px bottom 5px, and left 2px. Think of these values applied in a clockwise manner.

div {
/* All sides the same */
margin: 20px;
/* Vertical - Horizontal */
margin: 10px 15px;
/* Top - Horizontal - Bottom */
margin: 10px 15px 5px;
/* Top - Right - Bottom - Left */
margin: 10px 15px 5px 2px;
}

Negative margins allow you to pull an element closer to its neighboring elements, essentially reducing the space around it. They can be applied to any side of an element.

.example {
margin-top: -10px; /* Pulls the element 10px upward */
margin-right: -20px; /* Pulls the element 20px to the left */
margin-bottom: -5px; /* Pulls the element 5px upward */
margin-left: -15px; /* Pulls the element 15px to the right */
}
  1. Overlap Elements

Negative margins can be used to overlap different elements.

.element1 {
background-color: lightblue;
margin-bottom: -20px;
}
.element2 {
background-color: lightgreen;
}
  1. Adjust Layout

Negative margins can help fine-tune layout adjustments without altering other elements.

.header {
margin-bottom: -10px;
}
.content {
margin-top: 10px;
}

The padding property creates space inside the border of an element, effectively increasing the space between the content and the border.

  1. padding-top: Sets the padding at the top of an element.
  2. padding-right: Sets the padding on the right side of an element.
  3. padding-bottom: Sets the padding at the bottom of an element.
  4. padding-left: Sets the padding on the left side of an element.
.example {
padding-top: 20px;
padding-right: 15px;
padding-bottom: 10px;
padding-left: 5px;
}

The padding shorthand property follows the same pattern as the margin shorthand.

Syntax:

{
padding: top right bottom left;
}

If one value is provided, it applies to all four sides,padding: 20px; here all sides get 20px padding.

If two values are provided, the first applies to the top and bottom, and the second to the left and right, padding: 10px 15px; top and bottom gets 10px padding left and right gets 15px padding.

If three values are provided, the first applies to the top, the second to the left and right, and the third to the bottom. padding: 10px 15px 5px; top gets 10px, left and right get 15px, and bottom gets 5px padding.

If four values are provided, they apply to the top, right, bottom, and left, respectively. padding: 10px 15px 5px 2px; each side gets defined values as padding, top 10px right 15px bottom 5px, and left 2px. Think of these values applied in a clockwise manner.

div {
/* All sides the same */
padding: 20px;
/* Vertical - Horizontal */
padding: 10px 15px;
/* Top - Horizontal - Bottom */
padding: 10px 15px 5px;
/* Top - Right - Bottom - Left */
padding: 10px 15px 5px 2px;
}