Week 5 and Week 6 Lab Activity Aims:: Instructions 5 - CSS Layout
Week 5 and Week 6 Lab Activity Aims:: Instructions 5 - CSS Layout
Aims:
• Understand and apply the CSS Box model.
• Apply page layout techniques to a Web page
• Implement a Web page that maintains legibility under scaling
• Implement conditional CSS stylesheets for responsive design (using CSS media queries)
Page 1
COS10032 Computing Systems Project
Step 1. Create another CSS file and link multiple stylesheets to the web page.
In this task we will reuse the CSS stylesheet lab04.css we created last week as well as a new CSS style sheet
we will create for this task. Create a new folder styles inside your lab05 folder and copy your
lab04.css into it.
It is common practice to keep images that are referenced by the CSS separate from any images referenced by
the HTML. Inside the styles folder, create another directory called images. Place the file fade.jpg from
Canvas in this new styles/images folder.
Using Notepad++ or similar editor, create a new text file called layout.css and save it in lab05/styles.
Add a comment header to the top of your CSS file similar to the following, replacing the text in italics:
Page 2
COS10032 Computing Systems Project
Create a link to this file from your HTML page by adding a reference to the external stylesheet in each of the
<head> elements as follows:
The stylesheet
you created in the
previous lab
Having multiple style sheets will very slightly slow the page load speed (because there are multiple HTTP
requests), but can help the developer organise a complex site. For example, general site-wide CSS rules might
be stored in one sheet, while page-specific CSS rules might be stored in another. All stylesheets will be loaded
into the browser as if they were a single file. Inheritance and over-riding rules will still apply.
In this week version of the HTML, we have changed the menu to a set of paragraphs each contain an anchor
element (rather than a single paragraph). All these paragraphs have an attribute class="menu". To make
these paragraphs display on a single line we will format them all using the display property again, namely:
display: inline-block;
Add a rule to your CSS file that selects on the menu class to apply this property.
Another common way to create a menu is to use a list. For example
<ul>
<li><a …>Menu item 1</a></li>
<li><a …>Menu item 2</a></li>
</ul
List items can be in-lined as above. The dot points can be removed with the rule
nav ul { list-style:none;}
Page 3
COS10032 Computing Systems Project
Source: http://www.w3schools.com/css/css_boxmodel.asp
2. We will also set a background graphic to make the aside stand out from the main content a little.
3. Let’s format the <header> in a similar way. Define properties for the header element so that:
• it has a solid border 1px wide coloured darkgray
• has a 0.5em margin and padding
• has a background colour set using the property background-color:lightgray;
• it is centred using the appropriate value for the text-align property.
4. Around the footer element define a border in your choice of colour. The border should be 4px thick,
double and only visible on top of the footer.
1. Notice that the aside takes up the whole width of the web page (minus its margins). Using percentages is a
good way to maintain the relative layout of your design. Let’s make it take up 30% of the width of its parent
element by adding to the aside rule you created in the previous step the property
width: 30%;
What is its parent element?
Now notice that the aside shrinks and expands as you resize the viewport. What is the width of the parent?
2. Asides typically sit to the right of the page out of the main flow of the text. Let’s move the aside to the right
of the page so that the other content “floats” up around it. We will use the float:right; property to do
this. Your CSS rule for aside should now look like:
Page 4
COS10032 Computing Systems Project
Want to make it ‘lift’ off the page even more? Add a box-shadow: 10px 10px 5px #aaaaaa;
3. Create a style rule that floats the lists section to the left. For this we will use an id selector.
What is the effect? Notice the subsequent sections float up to the right of the lists section, while
maintaining their normal flow.
4. There is still a problem as the table is hard against the side of the lists.
Set the margin of the table element to auto to centre it in the column.
5. Create a rule that floats the tables section to the right using the appropriate id selector.
Notice the subsequent sections now float up between the lists and the tables sections.
Reduce and enlarge the size of the viewport to observe what happens.
6. If we want to stop subsequent elements floating up, we need to use the clear property. Create the
following rule for the hyperlinks section:
Notice the hyperlinks section now sits under the tables sections but still floats to the right of the lists
section. Now try:
The hyperlinks and subsequent sections now sit below the lists section.
7. If we want the images section to sit under the tables section we could try:
What’s wrong? The page looks a bit of a mess because the images section and footer have moved up.
8. Force the footer to the bottom of the page by using a clear:both; property on the footer element.
9. Let’s float the images section to the right so that it sits under the tables section, and add a border
property for the figure element of 2px solid #ccc
10. Let’s even up the widths of our sections in the two columns we have created by using a grouping selector
as follows:
Another approach would be to give
all these elements a class e.g.
class=”column” in the HTML
Careful and then select on that class
Remember that the width property is the width of the content of the CSS box. If we are going to have
padding, borders or margins around that content, we must reduce the width accordingly, i.e. less than 50%. In
this we don’t need to because we have no border etc.
Page 5
COS10032 Computing Systems Project
Notice that when we apply the text-align:center; property to these sections above all their textual
ancestor elements also end up centre aligned through inheritance. The lists look messy, fix this by adding a
grouping selector ol, ul, dl {text-align:left;}
Your CSS rules for the last sections of the page should now look something like this:
11. As the width of a viewport is reduced but resizing the browser window, the layout and text can become
hard to read. Observe what happens when you resize the window to as narrow as it will go. Expand the
window again.
Let’s set a minimum width on the article element to help preserve legibility. Observe the difference.
Note that because some of the elements have been taken out of the regular flow by floating them to the right
they now disappear off the edge of the page. Ideally the CSS should be able to detect and handle this. To find
out more about how to do this look up resources on CSS3 Media Queries.
2. Create the following rule using the :firstchild,:nth-child(n), and :last child pseudo class
selectors. These will select the sibling paragraphs according to the position in which they sit under their parent
element. We then use the properties we used in the previous step to format the paragraphs across the footer:
3. Add the additional provided stylesheet dl.css to your style folder and add a link in the head.
Inspect the CSS code. This uses many of the styles we have used previously but also provides a simple example
of styling a dl, and the use of a pseudo element ::after.
Validate your HTML and CSS using the appropriate validators.
[IMPORTANT] Upload the link to your web page running on the Mercury server to the Canvas
submission link by the due date to be marked off. Your work will only be marked if you correctly
indicate the link to your Mercury server.
Page 6
COS10032 Computing Systems Project
In this task you will apply some simple CSS styling to a Web page. This HTML file is in Lab05.zip and is
called lab05task2.html. Put it in your lab05 folder on your local machine.
With CSS, we will layout the HTML (lab05task2.html) to look something like below:
Using Notepad++ or similar editor, create a new text file called main.css and save it in lab05/styles.
Add a comment header to the top of your CSS file similar to the following, replacing the text in italics:
/*
filename: your name
author: your name
created: enter date
last modified: enter date
description: html files it refers to (if known)
*/
Create a link to this file from your web page lab05task2.html by adding a reference to the external
stylesheet main.css in the <head> element.
Notice the files also reference an external font which will be imported.
Below is a CSS files using some of the techniques we coved in the Task 1. Review the CSS rules below to make
sure you understand what they are doing, and then type or cut-paste them into your main.css file.
Page 7
COS10032 Computing Systems Project
Page 8
COS10032 Computing Systems Project
Task 3: Create a responsive layout (Optional)
In this task you will look at some of the problems that can occur when pages design for a desktop are displayed
on smaller screens. You will then create some responsive CSS.
How can I test my webpage designs to see how responsive they are, without switching
back and forth across devices?
Use the “Developer tools” available in Firefox and Chrome. Then each of these tools has a mode that
shows how a web page will look on different size screens.
• In Firefox menu, select “Web Developer” then select the "Responsive Design Mode” item.
• In Google Chrome menu, select “More tools” then select “Developer tools”. To activate device
mode, click the device icon in the top left corner of the Developer Tools window.
It is recommended that you use developer tools in Firefox or Chrome. The device emulation tool in
Internet Explorer 11's developer tools window is more difficult to use for this purpose than either
Firefox or Chrome.
Let’s first check the Web page using a Responsive Web Developer Tool. We can identify the problems
regarding responsiveness when we select various screen sizes as follows:
Select Medium Display (maximum display width is 1024 px, like iPad, Tablet, etc.):
Unexpected image
size
navigation wraps
columns are too
narrow
Page 9
COS10032 Computing Systems Project
We can identify some problems here like navigation wraps, section columns are too narrow and misplaced,
too much passive (unintentional) white space, and unexpected image size. We could design a mock-up for the
above identified problems. The design could be something like below:
3 columns
navigation
section 1 and 2 in
first row, section 3
in second row
To implement a responsive design that can change to the above layout when the screen sizes changes we
should follow the following steps:
An example media query is shown below where the media type is screen and the media feature is max-width
(the max-width value is set to 480 px). The max-width value is called a breakpoint and it is best measured in
ems, because they are flexible (1 em equals 16 px, in most browsers when the default medium font is
selected.).
Page 10
COS10032 Computing Systems Project
Create a link to the HTML file lab05task2.html by adding a reference to the external responsive stylesheet
responsive.css in the <head> element as follows:
Create responsive.css file with the appropriate header in your lab05/styles folder then add the
following CSS rules.
Page 11
COS10032 Computing Systems Project
Medium display:
Your responsive HTML should
now look something like:
Now let’s create a design for a Small Display (maximum display width is 568 px):
The Web page will be something like below if we resize it to mobile size (:
Page 12
COS10032 Computing Systems Project
The mock-up design and some hints have been given. The mock-up design will be something like below (2
columns navigation and 1 column section):
Create a separate CSS file for a small display like Apple iPhone 5.
Add the following conditional media query in your responsive.css file (complete the following CSS file
and check your responsive Web page using Firefox Responsive Developer Tool).
Page 13
COS10032 Computing Systems Project
Page 14