/Learn-Web-Development

Learning web development

Primary LanguageHTML

CSS Box Model

All HTML elements can be considered as boxes.

In CSS, the term "box model" is used when talking about design and layout.

The CSS box model is essentially a box that wraps around every HTML element. It consists of: content, padding, borders and margins. The image below illustrates the box model:

CSS Box Image

Explanation of the different parts:

  • Content - The content of the box, where text and images appear
  • Padding - Clears an area around the content. The padding is transparent
  • Border - A border that goes around the padding and content
  • Margin - Clears an area outside the border. The margin is transparent

The box model allows us to add a border around elements, and to define space between elements.

Width and Height of an Element

In order to set the width and height of an element correctly in all browsers, you need to know how the box model works.

Important: When you set the width and height properties of an element with CSS, you just set the width and height of the content area. To calculate the total width and height of an element, you must also include the padding and borders.

CSS Padding

Padding is used to create 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).

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

  • padding-top

  • padding-right

  • padding-bottom

  • padding-left

    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.

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

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

padding shorthand property with two values

padding: top and bottom px  right and left px

padding shorthand property with three values

padding: padding-top padding-right padding-bottom

padding shorthand property with four values

padding: padding-top padding-right padding-bottom padding-left

CSS Margins

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

Refer to padding

Tip: Negative values are allowed.

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.

Tip: Try to center a div using margin: 0 auto

Margin Collapse

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

This does not happen on left and right margins! Only top and bottom margins!