Coding Ireland STEM Report 2024 Have Your Say
HTML CSS
Advanced
30 mins
385 points
What you need:
  • Computer/laptop

CSS Box Model

In this lesson we learn about the CSS Box Model, Margins, Borders and Padding.

1 - CSS Box Model

Everything in a web page has a box around it. These boxes are mostly invisible but they control how everything is arranged and laid out on the page. We call this the CSS Box Model.

Each box has:

  • Margins - top, bottom, left and right margins outside the box. These are transparent.
  • Borders - top, bottom, left and right borders of the box. These can be set to a colour.
  • Padding - top, bottom, left and right padding inside the box. These are transparent.
  • Content - The content of the box. This can be text, images etc.

Margin

Border

Padding

Hi, I'm the content of the box!

2 - CSS Borders

Let's take a look at CSS Borders first. Here are some examples of <div></div> elements that have different borders applied to them using CSS.

I have red borders on the top, bottom, left and right.
I have a green border on the top and right.
I have blue borders on the top, bottom, left and right that are rounded.
I have gray borders on the top, bottom, left and right that are dashed.

3 - border-style

The border-style CSS property sets the style of border to use on the box. The following values can be set for border-style:

  • dotted - sets a dotted border.
  • dashed - sets a dashed border.
  • solid - sets a solid border.
  • double - sets a double border.
  • groove - sets a 3D grooved border.
  • ridge - sets a 3D ridged border.
  • inset - sets a 3D inset border.
  • outset - sets a 3D outset border.
  • none - sets no border.
  • hidden - sets a hidden border.

4 - border-width

The border-width CSS property sets how wide (usually in pixels) the border is.  

We can also set different widths for each of the top, right, bottom and left borders by providing 4 values for the border-width CSS property in the following order:

  1. top width
  2. right width
  3. bottom width
  4. left width 

5 - border-color

The border-color CSS property sets the colour of the border. The values we can use to set the colour can be:

  • A colour name - for example "red".
  • A HEX value - for example "#0000FF". A hexadecimal value is made up of hexadecimal numbers for red, green and blue to create one colour.
  • A RGB value - for example "rgb(0,255,100)". This sets the values for red, green and blue to create one colour.
  • Transparent - we can set a border to be transparent.

Join our club 😃

To view the remaining 7 steps and access hundreds of other coding projects please login or create an account.

Copyright Notice
This lesson is copyright of Coding Ireland. Unauthorised use, copying or distribution is not allowed.
🍪 Our website uses cookies to make your browsing experience better. By using our website you agree to our use of cookies. Learn more