Web Development

Dr Derek Bridge
School of Computer Science & Information Technology
University College Cork

Lecture Objectives

  • learn how to use CSS to float an element
  • learn how to use CSS to position an element
  • learn when to use positioning (almost never!) from examples of relative, absolute, fixed and sticky positioning

Warning

Fog of details ahead!

Do not 'learn off'.
Look-up when needed.

Out of Flow

  • By default, elements are laid out in the order they appear in the HTML.
  • There are ways of moving elements so that they appear in a place not based on the order of the HTML.
  • Some of these take the element out of flow, so that other elements are not aware that it exists:
    • See absolute and fixed positioning.
  • Others leave the element in flow, so that other elements are aware that it exists.
    • See floating and relative positioning.

CSS Floating

  • Floating an element:
    • moves an element to the left or right within its parent;
    • makes its outer display type block, if it wasn't already;
    • allows text and other inline content to appear alongside and below it.
  • The main use is for images but you can float other elements.
  • You may want to set the width of the floated element.
  • You may want to give it some margins.

Floating an Image

Text is from Wikipedia

CSS Shapes

  • CSS shapes allows content to flow around a non-rectangular shape.
  • (In the future, it might be possible to have text flow within a shape).
  • The element around which the content flows:
    • must be floated left or right and have a width and height;
    • must have the shape-outside property whose values include circle, ellipse, inset (= rectangle) or polygon.

CSS Shapes Example

CSS Shapes: Generated Content

The div spoils the HTML.

To avoid this, people use CSS generated-content:

CSS Shapes: Images

An image is another way to avoid using a div.

CSS Shapes: Transparent Images

If the image has an alpha channel, then you can even use that to create the shape.

CSS Positioning

CSS positioning allows you to precisely position an element.

However, avoid CSS Positioning! (Mostly.)

CSS Positioning

  • The position property specifies how an element will be positioned:
    static, relative, absolute, fixed, sticky.
  • The box offset properties
    top, right, bottom, left, z-index
    specify where an element will be positioned relative to something.
    • Values for the offset properties can be in any units such as px, em, %, …
    • (Mostly, if you use both top and bottom, then bottom is ignored; if you set both left and right, then right is ignored.)

Static Positioning

This is the default: normal layout.

The offset properties have no effect: the element remains in its normal position.

Relative Positioning

This is for 'nudging' the element.

The offset properties are how much to move relative to the element's original position, e.g.:

top: 2em; nudge the element down from where it was
top: -2em; nudge the element up from where it was

But the element is still in flow, so it leaves a gap behind.

Relative Positioning Example

Absolute Positioning

The element is removed from the flow
(it is out of flow so no gap left behind).

It is positioned relative to a 'containing element':
either its nearest ancestor that is not static, if there is one,
else the viewport (i.e. roughly speaking, the screen/browser window).

The offset properties are now the distance from the edge of the containing element.

top: 2em; place it 2em down from the top of the containing element

Absolute Positioning Example

In this one, the element will be positioned relative to the viewport:

Absolute Positioning Example

To make absolute positioning work as we want,
we often need to use relative positioning on its parent
but without changing the parent's position!

Fixed Positioning

Fixed positioning is like absolute positioning but always relative to the viewport.

Sticky Positioning

Sticky positioning is sensitive to the user's scroll position.

The element is positioned normally until it gets to a specific distance from the edges of the viewport.
At that point, the element behaves as if it we had used fixed positioning.

Advice on Positioning

Generally, avoid CSS Positioning!

Web developers used to lay out their whole web page using positioning.
Don't do this. There are now better alternatives.

However, there are some fun things we can do with positioning.

Some Uses of Positioning

You could position text over an image.

Some Uses of Positioning

Fixed headers and footers: only the main will scroll.

G'luck!