CSS Margins

The CSS margin properties are used to create space around elements, outside of any defined borders.

Margins define the distance between an element’s border and the surrounding elements.

With CSS, you have full control over the margins. CSS has properties for setting the margin for each individual side of an element (top, right, bottom, and left), and a shorthand property for setting all the margin properties in one declaration.

Example:

Result:

Margin – Individual Sides

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

All the margin properties can have the following values:

Tip: Negative values are also allowed.

Set different margins for all four sides of a <p> element:

Example:

Result:

Margin – Shorthand Property

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

The margin property is a shorthand property for the following individual margin properties:

Here is how it works:

If the margin property has four values:

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

Use the margin shorthand property with four values:

Example:

Result:

If the margin property has two values:

  • margin: 25px 50px;
    • top and bottom margins are 25px
    • right and left margins are 50px

Example:

Result:

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.

Example:

Result:

CSS Margin Collapse

Margin collapse is when two margins collapse into a single margin.

Top and bottom margins of elements are sometimes collapsed into a single margin that is equal to the largest of the two margins.

Note: Margin collapse only happens with top and bottom margins! Not left and right margins!

In the following example, the <h1> element has a bottom margin of 50px and the <h2> element has a top margin of 20px. So, the vertical margin between the <h1> and the <h2> would be a total of 70px (50px + 20px). But due to margin collapse, the actual margin ends up being 50px:

Example:

Result:

All CSS Margin Properties

Property Description
margin A shorthand property for setting all the margin properties in one declaration
margin-bottom Sets the bottom margin of an element
margin-left Sets the left margin of an element
margin-right Sets the right margin of an element
margin-top Sets the top margin of an element

CSS Padding

The CSS padding properties are used to generate space around an element’s content, inside of any defined borders.

With CSS, you have full control over the padding. There are properties for setting the padding for each side of an element (top, right, bottom, and left), and a shorthand property for setting all the padding properties in one declaration.

Example:

Result:

Padding – Individual Sides

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

All the padding properties can have the following values:

  • length – specifies a padding in px, pt, cm, etc.
  • % – specifies a padding in % of the width of the containing element
  • inherit – specifies that the padding should be inherited from the parent element

Note: Negative values are not allowed.

Example:

Result:

Padding – Shorthand Property

To shorten the code, it is possible to specify all the padding properties in one declaration.

The padding property is a shorthand property for the following individual padding properties:

Here is how it works:

If the padding property has four values:

  • padding: 25px 50px 75px 100px;
    • top padding is 25px
    • right padding is 50px
    • bottom padding is 75px
    • left padding is 100px

Example:

Result:

CSS box-sizing

Padding and Element Width

The CSS width property specifies the width of the element’s content area. The content area is the portion inside the padding, border, and margin of an element (the box model).

So, if an element has a specified width, the padding added to that element will be added to the total width of the element. This is often an undesirable result.

Example:

Result:

Padding and box-sizing

The box-sizing property defines how the width and height of an element are calculated: should they include padding and borders, or not.

The box-sizing property can have the following values:

  • content-box – This is default. The width and height properties includes only the content (border and padding are not included)
  • border-box – The width and height properties includes content, padding and border

So, to keep the width at 300px, no matter the amount of padding, you can use the box-sizing: border-box;. This causes the element to maintain its actual width; if you increase the padding, the available content space will decrease.

Example:

Result:

All CSS Padding Properties

Property Description
padding A shorthand property for setting all the padding properties in one declaration
padding-bottom Sets the bottom padding of an element
padding-left Sets the left padding of an element
padding-right Sets the right padding of an element
padding-top Sets the top padding of an element

 

CSS Height and Width

CSS Height and Width

The CSS height and width properties are used to set the height and width of an element.

Example:

Result:

CSS Set height and width

The height and width properties are used to set the height and width of an element.

The height and width do not include padding, borders, or margins. It sets the height and width of the area inside the padding, border, and margin of the element.


CSS height and width Values

The height and width properties can have the following values:

CSS height and width

Example:

Result:

Example:

Result:

CSS Min/Max Height and Width

CSS Min/Max Height and Width

The min-width and max-width properties are used to set the minimum and maximum width of an element.

The min-height and max-height properties are used to set the minimum and maximum height of an element.


CSS Using max-width

The max-width property sets the maximum allowed width of an element. This prevents the width of an element to be larger than the max-width property value.

The max-width property can have the following values:

One problem with the width property can occur when the browser window is smaller than the width of the element. The browser then adds a horizontal scrollbar to the page. So, using max-width will improve the browser’s handling on small windows.

Example:

Result:

Note: If you use both the width property and the max-width property on the same element, and the value of the width property is larger than the max-width property; the max-width property value will be used!

Example:

Result:

All CSS Dimension Properties

Property Description
height Sets the height of an element
max-height Sets the maximum height of an element
max-width Sets the maximum width of an element
min-height Sets the minimum height of an element
min-width Sets the minimum width of an element
width Sets the width of an element