CSS Positioning

Course Content

Positioning and the Box Model

At National 5 we were happy for you to place your content vertically one after the other. At Higher we will be expecting you to place content side by side using CSS rules.

But before this you will have to gain a further understanding of the sizes of elements using the box model.

The Box Model

Each 'box' on a web page has a content area (e.g., text or an image) and an optional surrounding area which consists of padding, border and a margin areas, this is known as the box model. A diagram representing this model is shown below:

Box Model diagram

Element Height and Widths

You have to consider the different sizes that a browser window can be displayed at. You can set the height and width of elements as fixed  sizes by :

Or relative percentages of their container elements - creating what can be known as a fluid layout.

Display Type - Block and Inline

Elements can be classed as an inline or block element. A block element can be defined as elements of the document that are formatted visually as blocks (e.g. paragraphs). They will take up the entire width of its container e.g. the DIV or other container that it is contained in or the page etc.

An inline element can be defined as those elements of the document that do not form new blocks of content, the content is distributed in lines. They will take up width of the content.

Some different block and inline elements are detailed below:

Block Elements

Inline Elements

Block vs Inline

The image below demonstrates the difference between putting 4 block elements immediately after one another or 4 inline elements immediately after one another.

Block Elements will always take up the entire width of their container (div/section/page etc).

Inline Elements will only take up the width that is necessary and will not cause a new line to be taken.

Block Elements

Paragraphs and headings are examples of block elements. You will notice that it has taken up the entire width of the container that it is in.

Heading borders are in black and the paragraph borders are in red.

If we add an image after the heading 1 element and before the text you will notice that it is placed on the line after Heading 1 and before Paragraph 1 as an image is an example of an inline element ( more on these in the next section). The image border has been set to green for display purposes.

Inline elements

Images are examples of inline elements. We have now we have added an image into the paragraph text as can be seen below. You will notice that the image element is displayed on the same line as the paragraph. And the green boundary shows that it is only taking up the width that it requires.

Positioning - Margin

The margin is the area outside of the element and by default is set to 0. When setting the margin you can specify the margin on all 4 sides of an element by using the margin property such as:

Shorthand Notation 

You can specify the margin for all 4 margins by listing the margin-top, margin-right, margin-bottom and margin-left properties as shown below:

The top, bottom, left and right margin settings can be set individually using:

Margin Examples

In this example the CSS rule below sets the margin at the top of each element to 5 pixels. The colour of the body has been set to a pale blue where as the element colours are white/grey.

header, nav, main, footer {margin-top:5px}

Centre Aligning Using Margins

The {margin: auto;} rule can be used to position an element in the centre of a browser window or within its container element.

So the CSS rule below would style the body element to always be in the middle of a browser window (as it will not be in any other container).

body{

margin: auto;

}

Centre Aligning Images

There will be occasions when you want to centre align an image or other inline element. To do this not only do you have to display the image as a block element (as an image is an inline element) but you will have to set the margin property to auto;

CSS Code

img{

display: block;

margin: auto;

}


Padding

Similar to margins, the padding property can be defined for any of the 4 sides of an element or to all 4 sides.

Or you can specify the top, bottom, left and right padding settings individually:

You will see that inside the two div elements the content is up at the very top left corner.

header, div, main, footer, section {padding:0px}

The two div elements above have no padding. You will notice that the text and the form is pressed up hard against the left top of the element.

Padding Example - With Padding

With 10 pixels of padding you will see that there is a 10 pixel space between the outside bounds of the two container elements.