






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The CSS Box Model, which is used to position and style HTML elements. It covers the different parts of the box model, including margins, borders, padding, and the actual content. The document also discusses compatibility issues with Internet Explorer and provides examples of setting border style, width, and color, as well as margin and padding.
Typology: Study notes
1 / 11
This page cannot be seen from the preview
Don't miss anything!







For explaining how to use CSS to arrange objects (elements) we have understand the CSS Box Model. 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 HTML elements, and it consists of: margins, borders, padding, and the actual content. The box model allows us to place a border around elements and space elements in relation to other elements. The image below illustrates the box model:
Explanation of the different parts:
Width and Height of an Element Important: When you specify the width and height properties of an element with CSS, you are just setting the width and height of the content area. To know the full size of the element, you must also add the padding, border and margin. The total width of the element in the example below is 300px:
width:250px; padding:10px; border:5px solid gray; margin:10px;
Let's do the math: 250px (width)
Example
width:220px; padding:10px; border:5px solid gray; margin:0px;
The total width of an element should always be calculated like this: Total element width = width + left padding + right padding + left border + right border + left margin + right margin The total height of an element should always be calculated like this: Total element height = height + top padding + bottom padding + top border + bottom border + top margin + bottom margin
Browsers Compatibility Issue If you tested the previous example in Internet Explorer, you saw that the total width was not exactly 250px. IE includes padding and border in the width, when the width property is set, unless a DOCTYPE is declared. To fix this problem, just add a DOCTYPE to the code:
Example
Border Color The border-color property is used to set the color of the border. The color can be set by:
Example
p.one { border-style:solid; border-color:red; } p.two { border-style:solid; border-color:#98bf21; }
Border - Individual sides In CSS it is possible to specify different borders for different sides:
Example
p { border-top-style:dotted; border-right-style:solid; border-bottom-style:dotted; border-left-style:solid; }
The example above can also be set with a single property:
Example
border-style:dotted solid;
The border-style property can have from one to four values.
o top and bottom borders are dotted o right and left borders are solid
An outline is a line that is drawn around elements, outside the border edge, to make the element "stand out". The outline properties specifies the style, color, and width of an outline.
Example
border:1px solid red; outline-style:dotted; outline-width:thick; outline-color:green;
The CSS margin properties define the space around elements.
Margin
The margin clears an area around an element (outside the border). The margin does not have a background color, and is completely transparent. The top, right, bottom, and left margin can be changed independently using separate properties. A shorthand margin property can also be used, to change all margins at once.
Possible Values
Value Description Auto The browser sets the margin. The result of this is dependant of the browser
length Defines a fixed margin (in pixels, pt, em, etc.) % Defines a margin in % of the containing element
Note: It is possible to use negative values, to overlap content.
The CSS padding properties define the space between the element border and the element content.
Padding
The padding clears an area around the content (inside the border) of an element. The padding is affected by the background color of the element. The top, right, bottom, and left padding can be changed independently using separate properties. A shorthand padding property can also be used, to change all paddings at once.
Possible Values
Value Description
length Defines a fixed padding (in pixels, pt, em, etc.)
% Defines a padding in % of the containing element
Padding - Individual sides
In CSS, it is possible to specify different padding for different sides:
Example
padding-top:25px; padding-bottom:25px; padding-right:50px; padding-left:50px;
Padding - Shorthand property
To shorten the code, it is possible to specify all the padding properties in one property. This is called a shorthand property. The shorthand property for all the padding properties is "padding":
Example
padding:25px 50px;
The padding property can have from one to four values.
Borders
No border.
A dotted border.
A dashed border.
A solid border.
A double border.
A groove border.
A ridge border.
An inset border.
An outset border.
A hidden border.
Outlines
Note: Internet Explorer 8 (and higher) supports the outline property if a !DOCTYPE is specified.
Note: Internet Explorer 8 (and higher) supports the outline property if a !DOCTYPE is specified.
Margins
This is a paragraph with no specified margins.
This is a paragraph with specified margins.
Paddings
This is a paragraph with no specified padding.
This is a paragraph with specified paddings.
Source: w3schools.com