The 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 HTML elements, and it consists of: margins, borders, padding, and the actual content.

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

The image below illustrates the box model:

 

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

Example

div {
    width: 300px;
    padding: 25px;
    border: 25px solid navy;
    margin: 25px;
}


Try it yourself » 

 

 

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 full size of an element, you must also add the padding, borders and margins.

Let's make a div element with a total width of 350px:

Example

div {
    width: 320px;
    padding: 10px;
    border: 5px solid gray;
    margin: 0; 
}


Try it yourself » 

Let's do the math:
320px (width)
+ 20px (left + right padding)
+ 10px (left + right border)
+ 0px (left + right margin)
= 350px

The total width of an element should 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 be calculated like this:

Total element height = height + top padding + bottom padding + top border + bottom border + top margin + bottom margin

 

Browsers Compatibility Issue

Internet Explorer 8 and earlier versions, include padding and border in the width property.

To fix this problem, add a <!DOCTYPE html> to the HTML page.

 

 

 

CSS Border

« Previous

Watch video of this tutorial

Next Chapter »

 

CSS Border Properties

The CSS border properties allow you to specify the style, size, and color of an element's border.

Border Style

The border-style property specifies what kind of border to display.

 

Note: None of the border properties will have ANY effect unless the border-style property is set!

border-style values:

none: Defines no border

dotted: Defines a dotted border

dashed: Defines a dashed border

solid: Defines a solid border

double: Defines two borders. The width of the two borders are the same as the border-width value

groove: Defines a 3D grooved border. The effect depends on the border-color value

ridge: Defines a 3D ridged border. The effect depends on the border-color value

inset: Defines a 3D inset border. The effect depends on the border-color value

outset: Defines a 3D outset border. The effect depends on the border-color value

Try it yourself: Set the style of the border

 

Border Width

The border-width property is used to set the width of the border.

The width is set in pixels, or by using one of the three pre-defined values: thin, medium, or thick.

Note: The "border-width" property does not work if it is used alone. Use the "border-style" property to set the borders first.

Example

p.one {
    border-style: solid;
    border-width: 5px;
}

p.two {
    border-style: solid;
    border-width: medium;


Try it yourself » 

 

 

Border Color

The border-color property is used to set the color of the border. The color can be set by:

  • name - specify a color name, like "red"
  • RGB - specify a RGB value, like "rgb(255,0,0)"
  • Hex - specify a hex value, like "#ff0000"

You can also set the border color to "transparent".

If the border color is not set it is inherited from the color property of the element.

Note: The "border-color" property does not work if it is used alone. Use the "border-style" property to set the borders first.

Example

p.one {
    border-style: solid;
    border-color: red;
}

p.two {
    border-style: solid;
    border-color: #98bf21;


Try it yourself » 

 

 

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;
}


Try it yourself » 

The example above can also be set with a single property:

Example

p {
    border-style: dotted solid;
}


Try it yourself » 

The border-style property can have from one to four values.

  • border-style: dotted solid double dashed; 
    • top border is dotted
    • right border is solid
    • bottom border is double
    • left border is dashed

 

  • border-style: dotted solid double; 
    • top border is dotted
    • right and left borders are solid
    • bottom border is double

 

  • border-style: dotted solid; 
    • top and bottom borders are dotted
    • right and left borders are solid

 

  • border-style: dotted;
    • all four borders are dotted

The border-style property is used in the example above. However, it also works with border-width and border-color.

 

Border - Shorthand property

As you can see from the examples above, there are many properties to consider when dealing with borders.

To shorten the code, it is also possible to specify all the individual border properties in one property. This is called a shorthand property.

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

  • border-width
  • border-style (required)
  • border-color

Example

p {
    border: 5px solid red;
}


Try it yourself » 

 

 

 

More Examples

All the top border properties in one declaration
This example demonstrates a shorthand property for setting all of the properties for the top border in one declaration.

Set the style of the bottom border
This example demonstrates how to set the style of the bottom border.

Set the width of the left border
This example demonstrates how to set the width of the left border.

Set the color of the four borders
This example demonstrates how to set the color of the four borders. It can have from one to four colors.

Set the color of the right border
This example demonstrates how to set the color of the right border.

 

All CSS Border Properties

Property

Description

border

Sets all the border properties in one declaration

border-bottom

Sets all the bottom border properties in one declaration

border-bottom-color

Sets the color of the bottom border

border-bottom-style

Sets the style of the bottom border

border-bottom-width

Sets the width of the bottom border

border-color

Sets the color of the four borders

border-left

Sets all the left border properties in one declaration

border-left-color

Sets the color of the left border

border-left-style

Sets the style of the left border

border-left-width

Sets the width of the left border

border-right

Sets all the right border properties in one declaration

border-right-color

Sets the color of the right border

border-right-style

Sets the style of the right border

border-right-width

Sets the width of the right border

border-style

Sets the style of the four borders

border-top

Sets all the top border properties in one declaration

border-top-color

Sets the color of the top border

border-top-style

Sets the style of the top border

border-top-width

Sets the width of the top border

border-width

Sets the width of the four borders

 

 

CSS Outlines

« Previous

Watch video of this tutorial

Next Chapter »

 

An outline is a line that is drawn around elements (outside the borders) to make the element "stand out".

The outline properties specify the style, color, and width of an outline.

 

Examples

Draw a line around an element (outline)
This example demonstrates how to draw a line around an element, outside the border edge.

Set the style of an outline
This example demonstrates how to set the style of an outline.

Set the color of an outline
This example demonstrates how to set the color of an outline.

Set the width of an outline
This example demonstrates how to set the width of an outline.

 

CSS Outline

An outline is a line that is drawn around elements (outside the borders) to make the element "stand out". 

However, the outline property is different from the border property.

The outline is not a part of an element's dimensions; the element's total width and height is not affected by the width of the outline.

 

 

All CSS Outline Properties

Property

Description

Values

outline 

Sets all the outline properties in one declaration

outline-color
outline-style
outline-width
inherit

outline-color 

Sets the color of an outline

color_name
hex_number
rgb_number
invert
inherit

outline-style 

Sets the style of an outline

none
dotted
dashed
solid
double
groove
ridge
inset
outset
inherit

outline-width 

Sets the width of an outline

thin
medium
thick
length
inherit

 

 

 

CSS Margin

« Previous

Watch video of this tutorial

Next Chapter »

 

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 calculates a margin

length

Specifies a margin in px, pt, cm, etc. Default value is 0px

%

Specifies a margin in percent of the width of the containing element

inherit

Specifies that the margin should be inherited from the parent element

 

 

Note: It is also possible to use negative values, to overlap content.

 

 

Margin - Individual sides

In CSS, it is possible to specify different margins for different sides of an element:

Example

p {
    margin-top: 100px;
    margin-bottom: 100px;
    margin-right: 150px;
    margin-left: 50px;
}


Try it yourself » 

 

 

Margin - Shorthand property

To shorten the code, it is possible to specify all the margin properties in one property. This is called a shorthand property.

The shorthand property for all the margin properties is "margin":

Example

p {
    margin: 100px 50px;
}


Try it yourself » 

The margin property can have from one to four values.

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

 

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

 

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

 

  • margin: 25px;
    • all four margins are 25px

 

 

More Examples

Set the top margin of a text using a cm value
This example demonstrates how to set the top margin of a text using a cm value.

Set the bottom margin of a text using a percent value
This example demonstrates how to set the bottom margin in percent, relative to the width of the containing element.

 

All CSS Margin Properties

Property

Description

margin

A shorthand property for setting 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

« Previous

Watch video of this tutorial

Next Chapter »

 

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

p {
    padding-top: 25px;
    padding-right: 50px;
    padding-bottom: 25px;
    padding-left: 50px;
}


Try it yourself » 

 

 

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

p {
    padding: 25px 50px;
}


Try it yourself » 

The padding property can have from one to four values.

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

 

  • padding: 25px 50px 75px;
    • top padding is 25px
    • right and left paddings are 50px
    • bottom padding is 75px

 

  • padding: 25px 50px;
    • top and bottom paddings are 25px
    • right and left paddings are 50px

 

  • padding: 25px;
    • all four paddings are 25px

 

 

More Examples

All the padding properties in one declaration
This example demonstrates a shorthand property for setting all of the padding properties in one declaration, can have from one to four values.

Set the left padding
This example demonstrates how to set the left padding of a p element.

Set the right padding
This example demonstrates how to set the right padding of a p element.

Set the top padding
This example demonstrates how to set the top padding of a p element.

Set the bottom padding
This example demonstrates how to set the bottom padding of a p element.

 

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 Dimension

« Previous

Next Chapter »

 

The CSS dimension properties allow you to control the height and width of an element.

 

 

Try it Yourself - Examples

Set the height of elements
This example demonstrates how to set the height of different elements.

Set the height of an image using percent
This example demonstrates how to set the height of an element using a percent value.

Set the width of an element using a pixel value
This example demonstrates how to set the width of an element using a pixel value.

Set the maximum height of an element
This example demonstrates how to set the maximum height of an element.

Set the maximum width of an element using percent
This example demonstrates how to set the maximum width of an element using a percent value.

Set the minimum height of an element
This example demonstrates how to set the minimum height of an element.

Set the minimum width of an element using a pixel value
This example demonstrates how to set the minimum width of an element using a pixel value.

 

All CSS Dimension Properties

Property

Description

Values

height

Sets the height of an element

auto
length
%
inherit

max-height

Sets the maximum height of an element

none
length
%
inherit

max-width

Sets the maximum width of an element

none
length
%
inherit

min-height

Sets the minimum height of an element

length
%
inherit

min-width

Sets the minimum width of an element

length
%
inherit

width

Sets the width of an element

auto
length
%
inherit

 

 

 

CSS Display and Visibility

« Previous

Watch video of this tutorial

Next Chapter »

 

The display property specifies if/how an element is displayed, and the visibility property specifies if an element should be visible or hidden.

Box 1

Box 2

Box 3

 

Hiding an Element - display:none or visibility:hidden

Hiding an element can be done by setting the display property to "none" or the visibility property to "hidden". However, notice that these two methods produce different results:

visibility:hidden hides an element, but it will still take up the same space as before. The element will be hidden, but still affect the layout.

Example

h1.hidden {
    visibility: hidden;
}


Try it yourself » 

display:none hides an element, and it will not take up any space. The element will be hidden, and the page will be displayed as if the element is not there:

Example

h1.hidden {
    display: none;
}


Try it yourself » 

 

 

CSS Display - Block and Inline Elements

A block element is an element that takes up the full width available, and has a line break before and after it.

Examples of block elements:

  • <h1>
  • <p>
  • <li>
  • <div>

An inline element only takes up as much width as necessary, and does not force line breaks.

Examples of inline elements:

  • <span>
  • <a>

 

Changing How an Element is Displayed

Changing an inline element to a block element, or vice versa, can be useful for making the page look a specific way, and still follow web standards.

The following example displays <li> elements as inline elements:

Example

li {
    display: inline;
}


Try it yourself » 

The following example displays <span> elements as block elements:

Example

span {
    display: block;
}


Try it yourself » 

 

 

Note: Setting the display property of an element only changes how the element is displayed, NOT what kind of element it is. So, an inline element with display:block is not allowed to have other block elements inside of it.

 

 

 

 

 

 

CSS Float

« Previous

Watch video of this tutorial

Next Chapter »

 

What is CSS Float?

 

 

 

 

With CSS float, an element can be pushed to the left or right, allowing other elements to wrap around it.

Float is very often used for images, but it is also useful when working with layouts.

 

How Elements Float

Elements are floated horizontally, this means that an element can only be floated left or right, not up or down.

A floated element will move as far to the left or right as it can. Usually this means all the way to the left or right of the containing element.

The elements after the floating element will flow around it.

The elements before the floating element will not be affected.

If an image is floated to the right, a following text flows around it, to the left:

Example

img {
    float: right;
}


Try it yourself » 

 

 

Floating Elements Next to Each Other

If you place several floating elements after each other, they will float next to each other if there is room.

Here we have made an image gallery using the float property:

Example

.thumbnail {
    float: left;
    width: 110px;
    height: 90px;
    margin: 5px;
}


Try it yourself » 

 

 

Turning off Float - Using Clear

Elements after the floating element will flow around it. To avoid this, use the clear property.

The clear property specifies which sides of an element other floating elements are not allowed.

Add a text line into the image gallery, using the clear property:

Example

.text_line {
    clear: both;
}


Try it yourself » 

 

 

 

More Examples

An image with border and margins that floats to the right in a paragraph
Let an image float to the right in a paragraph. Add border and margins to the image.

An image with a caption that floats to the right
Let an image with a caption float to the right.

Let the first letter of a paragraph float to the left
Let the first letter of a paragraph float to the left and style the letter.

Creating a horizontal menu
Use float with a list of hyperlinks to create a horizontal menu.

Creating a homepage without tables
Use float to create a homepage with a header, footer, left content and main content.

 

All CSS Float Properties

Property

Description

Values

clear

Specifies which sides of an element where other floating elements are not allowed

left
right
both
none
inherit

float

Specifies whether or not a box should float

left
right
none
inherit

 

 

 

 

CSS Horizontal Align

« Previous

Watch video of this tutorial

Next Chapter »

 

In CSS, several properties are used to align elements horizontally.

 

Aligning Block Elements

A block element is an element that takes up the full width available, and has a line break before and after it.

Examples of block elements:

  • <h1>
  • <p>
  • <div>

For aligning text, see the CSS Text chapter.

In this chapter we will show you how to horizontally align block elements for layout purposes.

 

Center Aligning Using the margin Property

Block elements can be center-aligned by setting the left and right margins to "auto".

 

Note: Using margin:auto; will not work in IE8 and earlier, unless a !DOCTYPE is declared.

Setting the left and right margins to auto specifies that they should split the available margin equally. The result is a centered element:

Example

.center {
    margin-left: auto;
    margin-right: auto;
    width: 70%;
    background-color: #b0e0e6;
}


Try it yourself » 

Tip: Center-aligning has no effect if the width is 100%.

 

Left and Right Aligning Using the position Property

One method of aligning elements is to use absolute positioning:

Example

.right {
    position: absolute;
    right: 0px;
    width: 300px;
    background-color: #b0e0e6;
}


Try it yourself » 

Note: Absolute positioned elements are removed from the normal flow, and can overlap elements.

 

Crossbrowser Compatibility Issues

When aligning elements like this, it is always a good idea to predefine margin and padding for the <body> element. This is to avoid visual differences in different browsers.

There is a problem with IE8 and earlier, when using the position property. If a container element (in our case <div class="container">) has a specified width, and the !DOCTYPE declaration is missing, IE8 and earlier versions will add a 17px margin on the right side. This seems to be space reserved for a scrollbar. Always set the !DOCTYPE declaration when using the position property:

Example

body {
    margin: 0;
    padding: 0;
}

.container {
    position: relative;
    width: 100%;
}

.right {
    position: absolute;
    right: 0px;
    width: 300px;
    background-color: #b0e0e6;
}


Try it yourself » 

 

 

Left and Right Aligning Using the float Property

One method of aligning elements is to use the float property:

Example

.right {
    float: right;
    width: 300px;
    background-color: #b0e0e6;
}


Try it yourself » 

 

 

Crossbrowser Compatibility Issues

When aligning elements like this, it is always a good idea to predefine margin and padding for the <body> element. This is to avoid visual differences in different browsers.

There is a problem with IE8 and earlier when using the float property. If the !DOCTYPE declaration is missing, IE8 and earlier versions will add a 17px margin on the right side. This seems to be space reserved for a scrollbar. Always set the !DOCTYPE declaration when using the float property:

Example

body {
    margin: 0;
    padding: 0;
}

.right {
    float: right;
    width: 300px;
    background-color: #b0e0e6;
}