Web Development

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

Lecture Objectives

  • learn how to center things
  • learn how to layout a nav and a header
  • learn how to layout a multi-column page

Warning

Fog of details ahead!

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

Centering Text

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


p {
    text-align: center;
}

Centering a Block

To center a single element whose display is block 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;
}
    

Centering the Page

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

Centering Several Elements

To center several elements elements horizontally within a parent element, use a flexbox and set justify-content.


.parent {
    display: flex;
    flex-direction:row;
    flex-wrap: nowrap;
    justify-content: center;
    align-items: center;
    gap: 1em;
}
        
.child {
    flex: 1;
}
    

Styling a nav

We see a horizontal menu. Why?
What is a one-line way to make it a stacked menu?

Styling a nav

Styling a nav

Styling a header 1

Styling a header 2

Styling a header 3

Stacked Page Layout

CSS flow layout takes care of this!

Two-column Page Layout

This can be done using either flexbox or grid

Two-column using Flexbox

Two-column using Grid

Two-column using Grid

G'luck!