Notes on Liquid Layouts

Goal: using CSS and HTML, create a basic 2-column liquid layout

one column is the menu, and it will be on the left of the screen

it has links to various important pages in the website.

the second column is the main content

we want a small amount of space separating the two columns

the exact amount of space is whatever looks best

The Basic Content

here’s the body of our page:

<body>

<h3>Menu</h3>
<ul>
    <li>Home page</li>
    <li>Food</li>
    <li>Clothing</li>
    <li>Furniture</li>
</ul>

<h1>Content</h1>
<p>This is the content column. The main content of the page goes here.</p>

<p>When styled using using the appropriate <code>float</code> styles,
these two columns will appear side-by-side.</p>

<p>Each column takes up a percentage of the screen, and so it is an
example of a <em>liquid layout</em>: the layout automatically re-formats
itself if you change re-size the browser window.</p>

</body>

right now, the Menu appears first followed underneath by the Content

instead, we want the Menu on the left side in its own column, and then to the right of that, we want the Content

Naming the Columns

the first thing we need to is to treat each columnas their own element so that we can apply styles to them

we will do this using div tags

we’ll put the Menu into its own div element with id menu

and the Content into its own div with id content

<body>

<div id="menu">
<h3>Menu</h3>
<ul>
    <li>Home page</li>
    <li>Food</li>
    <li>Clothing</li>
    <li>Furniture</li>
</ul>
</div>

<div id="content">
<h1>Content</h1>
<p>This is the content column. The main content of the page goes here.</p>

<p>When styled using using the appropriate <code>float</code> styles,
these two columns will appear side-by-side.</p>

<p>Each column takes up a percentage of the screen, and so it is an
example of a <em>liquid layout</em>: the layout automatically re-formats
itself if you change re-size the browser window.</p>
</div>

</body>

by default, a div does not change the syle of its contents

Float

the basic trick for putting the #menu and #content divs side-by-side is to use their float properties

we want #menu on the left, so we will make it float left

we want #content on the right, so we will make it float right

/* this style applies to the element with id="menu" */
#menu {
    float: left;
    width: 15%;
}

/* this style applies to the element with id="content" */
#content {
    float: right;
    width: 80%;
}

the #menu is smaller than the #content, so we set their widths differently

the widths are set using % as the unit, so the columns will re-size themselves if the size the of the screen changes; this is called a liquid layout

notice that the total width of the two columns adds up to 95%, which is not the full width of the screen

this leaves a little bit of space between the columns

Styling the Menu

the menu items are on an unordered list

lets get rid of the bullet points and the extra space on the left side:

/* this style applies to all ul elements in #menu */
#menu ul {
    /* remove list bullet points */
    list-style: none;

    /* get rid of excess left padding */
    padding-left: 0;
}

the selector #menu ul means that the style will apply to all ul elements that appear in the #menu element

we’ll only have one ul in #menu, i.e. the one list showing the links that can be selected

it turns out that the excess space on the left side of the ul in #menu is due to the padding of the default ul

one way to figure out that it’s padding (and not the margin) that is the source of the space is to use Chrome’s built-in console

type ctrl-shift-C in Chrome, and a new sub-window should appear

in this new window you can hover the mouse pointer over the source of an element and then see in the window a box showing the margin, padding, and border of that element (using colors to distinguish which is which)

once you’ve determined that it’s the padding where the space is coming from, the we set padding-left to 0 to get rid of it

More Menu Styling

there’s a lot of space around the menu heading

inspecting it with the Chrome console shows that this space is due to two things: the header’s bottom margin, and the lists top margin

so setting those to 0 improves the spacing:

/* this style applies to all ul elements in #menu */
#menu ul {
    /* remove list bullet points */
    list-style: none;

    /* get rid of excess left padding */
    padding-left: 0;

    /* get rid of excess top margin */
    margin-top: 0;
}

/* this style applies to all h3 elements in #menu */
#menu h3 {
    margin-bottom: 0;
}

now another visual issue is apparent: the list items and the menu header are both at the same level of indentation

it might look nicer if we indent the list items a little more, e.g.:

#menu ul {
    /* ... */

    /* get rid of excess left padding */
    padding-left: 0.3em;

    /* ... */
}

the particular value of 0.3em came from trial-and-error; you may prefer a smaller/bigger value

another visual detail is the size of the font in the menu may look better if it were a bit smaller

so lets make the #menu column font smaller and sans-serif, and also add a thing border around the entire menu:

#menu {
    float: left;
    width: 15%;

    border: 1px blue dotted;
    font-size: x-small;
    font-family: sans-serif;
}

the introduction of the border presents some more spacing issues, so lets adjust the header a little more:

#menu h3 {
    margin-top: 0.2em;
    margin-bottom: 0;
    text-align: center;
}

of course, you may prefer different style decisions than the ones made here, so go ahead and adjust things to be the way you prefer

Content Styling

the #content header now appears below the #menu header

lets try to make them both at about the same level:

#content h1 {
    margin-top: 0;
    font-family: Verdana;
}

we’ve also changed the heading font to Verdana, a sans-serif web-safe fonts

now lets suppose we also want to the #content background to be a light gray:

#content {
    float: right;
    width: 80%;

    background-color: rgb(240, 240, 240);
    padding-left: 0.2em;
}

we’ve also added a little bit of left padding so that the letters in #content don’t appear right beside the very edge

And So On

many more tweaks and adjustments are possible

designers can spend a lot of time getting things just right

there are many more advanced page layout features we could discuss, but this is a beginning course so we will not go into any more detail (you can find lots of information and help about CSS on the web)

when you are done, you should test your layout by re-sizing the screen; it should look okay for all reasonable screen re-sizings