HTML Cascading Style Sheets Box Model


In HTML, although you can not neccesarily see it, every element is considered to be surrounded by a "box". If you think of the content of the HTML as the interior of the box, you can visualize it as surrounded by successive layers of the box. These layers are margins, borders, padding, and the actual content is inside.


Margins
Borders
Padding
Content
  • Content
  • Text, images, tables, and other visible elements.
  • Padding "Pads" the area around the content. This is invisible.
  • Border Border surrounding content and padding. Can be made invisible. Set border: 0px; to make invisible
  • Margin Clears an area around the border.


Here is an example of a class setting the box elements:

		
			.myBox {
			    width: 300px;
			    padding: 15px;
			    border: 2px solid gray;
			    margin: 2;
			}
		
	


Determining the Size of your Box


	
		300px (width)
		+ 30px (left + right padding)
		+ 4px (left + right border)
		+ 4px (left + right margin)
		= 338px
	

Total element width = width + left padding + right padding + left border + right border + left margin + right margin
Total element height = height + top padding + bottom padding + top border + bottom border + top margin + bottom margin


I hope you will join us for the next lesson Lesson 6)CSS Span

Additional Resources