CSS Margin

CSS margin is the space between the border of an element and the other elements on the page. It can be used to create a margin of space around an element, or to make an element appear larger or smaller.

Margin is specified using the margin property. The margin property can take up to four values, one for each side of the element:

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

If you only specify one value, it will be applied to all four sides of the element. If you specify two values, they will be applied to the top and bottom, and the left and right, respectively. If you specify three values, the first value will be applied to the top, the second value to the left and right, and the third value to the bottom. If you specify four values, they will be applied to the top, right, bottom, and left, respectively.

You can specify margin values in pixels, percentages, or other CSS units. For example, the following code would add a 10px margin to all sides of the element:

.element {
  margin: 10px;
}

You can also use the margin property to add different amounts of margin to different sides of the element. For example, the following code would add a 10px margin to the top and bottom of the element, and a 20px margin to the left and right:

.element {
  margin: 10px 20px;
}

Task

Give the ul a margin of 10px, add a selector ul li with a margin of 5px 0, and form with a margin of 0 auto values. Run the code to see the final results and experiment with the code to get used to it.

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT