In the next two weeks you are going to be introduced to the core theoretical and practical background necessary to style web pages using CSS.
Our classes are going to be based on the core textbook:
Ducket, J. (2014) HTML and CSS: design and build websites. Wiley.
More information on the textbook on our on-line pages. Examples available from htmlandcssbook.com
You should have already some good experience in web development, and you should be able to:
In this block we build on the learning outcomes that you achieved during your first block, trying to move one step further.
Since the advent of the World Wide Web (WWW) much of the focus in computing has been on technologies that work with, or on, the World Wide Web. This includes W3C technologies as HTML, CSS and XML (that work on the web) but also programming languages as Java, Python, Ruby - the allow to build applications that rely on - or use - the World Wide Web.
These technologies can be grouped (by function) into three main layers:
At this point, it would be useful to summarise the more important WWW technologies, and in which layers they operate.
Please note that any technology can be used in different ways; we are now focusing on the more common uses of the technologies.
Some of the technologies focus on just one of the layers that we have identified:
Some of the technologies focus on two groups of function (typically, structuring content and managing the presentation layer):
A number of languages implement features of all three layers:
The CSS 1 specification was released in 1996. CSS1 supports:
The CSS2 specification was released in 1998. CSS2 is a superset of CSS 1 and includes:
CSS2.1 was published in 2011, with minor modifications.
The CSS3 specification was released modularly between 2011 and 2012. CSS3 adds new capabilities or extends features defined in CSS 2.
During this module we will not refer further to the different CSS versions - but keep in mind that is both incremental and modular. Browser support is still not complete.
Using HTML for complex layouts typically results in:
Table-based layouts
ARE EVIL!
Table-based layouts are bad in terms of accessibility, bandwidth, maintainability, and search engine optimisation - amongst other reasons.
For a discussion on table-based layouts, see Tableless Web Design on Wikipedia and this Tutorial Republic page.
Most old style web sites that used to be table-based, as Craiglist, have now been converted to CSS.
<u>
, <font>
, <center>
— any tag or attribute setting that specifies how something should be displayedVaries between browsers (IE, Chrome, Firefox, Opera etc.), platforms (Windows, *nix, Mac), and versions. To maximise interoperability you should refrain from using any non-standard CSS code.
Stylesheets can be located:
<head>
<style type="text/css">
h1 {font-size:medium; color:#FF00FF}
</style>
</head>
<h1 style="color:red; text-transform:capitalize;">CSS Test</h1>
The link tag must be in the head section of your HTML document:
<link rel="stylesheet" type="text/css" href="common.css" media="all">
body {background-color:white; color: black; font-family: Verdana, Arial, Helvetica; font-size: x-small; font-style: normal; text-align: left}
p {background-color:white; color:#000066; font-family: Verdana, Arial, Helvetica; font-size: small; font-style: normal; text-decoration: none; font-weight: bold}
h1 {font-family:Verdana, Arial, Helvetica; text-align: center; font-size:large; color:#000000;; font-style: normal}
h2 {font-family: "Dungeon", Arial; color:#000000; text-align: left; font-size:medium;}
ul {list-style-image: url(media/arrow.png)}
Styles are applied to tags using CSS selectors, which allow to identify specific elements.
The most common selectors include:
The universal selector is used to match all the elements in a document.
Example:
* {color: #ffc}
The type selector is used to match the element (tag) name.
Example:
h1 {color: #ffc}
Classes are used in HTML tag to identify one or more elements of the page as having some characteristic in common:
<p class="type1">In taberna cuando sumus</p>
<p class="type2">Non curamus quid sit humus</p>
Classes require a matching selector in a .css file or in a style section, in the form of .classname
:
.type1 {color: #ffc}
.type2 {color: #ffd}
An id is a core attribute and can be applied to any HTML element to univoquely identify individual elements of a page:
Example:
<p id="paragraph1">In taberna cuando sumus</p>
<p id="paragraph2">Non curamus quid sit humus</p>
Ids require a matching selector in a .css file or in a style section, and are idenfied by the use of the pound (#), in the form of #idname
:
#paragraph1 {color: #ffc}
#paragraph2 {color: #ffd}
[...]
<head>
<style type="text/css">
#element1 {font-size:medium; color:#f0f}
</style>
</head>
<body>
<p id="element1">In taberna cuando sumus</p>
<p>Non curamus quid sit humus</p>
[...]
Classes and id selectors can be generic (and apply to all elements) or be specific and apply to only a specific tag:
Example:
p.paragraph1 {color: blue;}
p#element1 {color: red;}
In this example, all p
elements are children of section. All p
elements are siblings, and the first strong
element is a descendant of the section
element.
Child selectors are used to match an element that is a child of another element in the document tree.
Example: the following rule selects only b
elements contained within p
elements.
p > b {color: blue;}
Descendant selectors are used to match an element that is a descendant of another elements in the document tree.
Example: the following rule selects only b
elements that have a p
element as ancestor (e.g. father, grandfather, great-grandfather):
p b {color: blue;}
Adjacent sibling selectors are used to match elements that share the same parent and are adjacent in the document tree.
The + (plus sign) is used to denote adjacent siblings.
Example: the following rule selects only p
elements that are just after an h1
element with the same parent:
h1+p {color: blue;}
General sibling selectors are used to match an element that is a sibling of another elements in the document tree.
Example: the following rule selects all p
elements that are siblings (have the same parent) of an h1
element:
h1~p {color: blue;}
The following list of selector types is by increasing specificity:
h1
) and pseudo-elements (e.g., :before
)..example
), attributes selectors (e.g., [type="radio"]
) and pseudo-classes (e.g., :hover
).#example
)You can find a detailed explanation of how to precisely calculate the specificity here.
The location where the style is specified also affects the specificity.
The following list is by increasing specificity:
If two selectors have the same specificity, the last one will take precedence.
Some CSS properties are inherited from parent elements to child elements: li
elements will inherit (some) style rules from ul
elements, for example.
Not all the CSS properties are inherited; these are the most common inherited properties:
A full list is available here.
To successfully engage with this part of the module, you will need:
Before you start studying/working:
Let's create our Hello World in HTML 5 + CSS:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Hello World</title>
</head>
<body>
<p style="font-family: Cambria, 'Hoefler Text', 'Liberation Serif', Times, 'Times New Roman', serif">
</body>
</html>
Let's create a web page styling quotes from the last letter of Ernesto "Che" Guevara to Fidel Castro.
The page must include:
Please set the following CSS properties:
Use an internal style sheet and validate with both the HTML and CSS validators.
Re-do the previous activity using an external style sheet.
Building on top of the previous activities, change background-color for all quotes using one class per each quote.
Use https://color.adobe.com/ to create a analogous color scheme (based on similar colors).
Add a list-based table of contents. Clicking on each element of the list the user will be taken to the given quote.
Using an id, justify the text of the first quote.
Using an internal style sheet, justify the text of the first quote.
Demonstrate the use of child, descendant, adjacent sibling and sibling selectors.
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License (what does it mean? you can copy/modify/redistribute this content).