Web Development

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

Lecture Objectives

  • learn about layouts: fixed versus liquid; responsive; multi-column
  • learn how to use CSS to center an element
  • learn a little about flexbox from examples

Types of Layout

Fixed-width
Set widths in pixels.
Liquid
Set widths in percentages/ems.

Fixed-Width Layouts

Liquid Layouts

When to Use

  • When to use fixed-width layouts:
    • when you know the width of the majority of your visitors' windows;
    • your site has lots of fixed-width content, e.g. images;
    • pixel-perfect appearance is paramount to you.
  • When to use liquid layouts:
    • most of the time.

Multi-column Layout

Two- or three-colum layout is very common!

A three-column layout comprising a header across the top, then a nav, a main and an aside side-by-side, and a footer across the bottom.

Layout and Components

There may be a layout for the page and separate layouts for the components on the page.
E.g. this is still just a page that has a three-column layout:

A three-column layout comprising a header across the top, then a nav, a main and an aside side-by-side, and a footer across the bottom. But the elements within the header and the main.

CSS Layouts

Flow layout:
Based on the outer display type of each element.
Flexbox:
One-dimensional layout based on rows or columns but elements 'flex' to fill additional space or shrink to fit smaller spaces.
Grid layout:
Two-dimensional layout.

Other: floating, positioning, multicolumn text.

Example

Responsive Web Design (RWD)

In RWD, we adapt the layout to the characteristics of the device/window.

E.g.
one-column layout for narrow devices/windows;
two-column layout when there is more room;
three-column layout when there is even more room.

Responsive Web Design

  • A flexible (liquid) layout.;
  • Flexible images and media;
  • Media queries;
  • Least-capable-clients-first ('mobile-first'); and
  • Progressive enhancement.

CSS Flexbox

A flexbox contains items, which are laid out in a one-dimensional way,
either in rows or in columns.

The items in the flexbox can 'flex',
grow or shrink.

Don't use flexbox when flow layout is sufficient!

Flexbox Properties

A Complete Guide to Flexbox

On the container:
display: flex;

Parent properties
(the flexbox itself)
(the container)
Child properties
(the flex items)
(the contents)
flex-direction order
flex-wrap flex-grow
justify-content flex-shrink
align-items flex-basis
align-content align-self

Horizontal Centering I

To center text or an image horizontally within a parent element, use
text-align: center;


p {
    text-align: center;
}
        

Horizontal Centering II

To center a single element horizontally within a parent element, set its left and right margin to auto.
(Note that nothing will happen unless the element is narrower than the parent.)


p {
    margin-left: auto;
    margin-right: auto; 
}
    

Common shorthand:


p {
    margin: 0 auto;
}
    

Horizontal Centering II, continued

This technique is often used to center the whole web page, e.g.:


html {
    background-color: white;
}
        
body {
    width: 80%;
    margin: 0 auto;
    background-color: green;
}
    

Horizontal Centering III

To center several elements elements horizontally within a parent element, use a flexbox.


main {
    display: flex;
    justify-content: center;
}
        
p {
    flex-basis: 20%;
    flex-grow: 0;
}
    

Equal-Sized Columns

Flexbox for an Image Gallery

Very easy if all images are the same size.

Flexbox for an Image Gallery

But there are more decisions to make when images are not the same size.

G'luck!