Css Tutorial
Css Tutorial
CSS, or Cascading Styles Sheets, is a way to style HTML. Whereas the HTML is the content, the
style sheet is the presentation of that document.
Styles don't smell or taste anything like HTML, they have a format of 'property: value' and most
properties can be applied to most HTML tags.
Applying CSS - The different ways you can apply CSS to HTML.
Selectors, Properties, and Values - The bits that make up CSS.
Colours - How to use colour.
Text - How to manipulate the size and shape of text.
Margins and Padding - How to space things out.
Borders - Erm. Borders. Fings wot go around fings.
Putting It All Together - Throwing all of the above ingredients into one spicy hotpot.
Applying CSS
There are three ways to apply CSS to HTML.
In-line
In-line styles are plonked straight into the HTML tags using the style attribute.
They look something like this:
Internal ( or Embedded)
Embedded, or internal styles are used for the whole page. Inside the head tags, the style tags
surround all of the styles for the page.
This would look something like this:
This will make all of the paragraphs in the page red and all of the links blue.
Similarly to the in-line styles, you should keep the HTML and the CSS files separate, and so we are
left with our saviour...
External
External styles are used for the whole, multiple-page website. There is a separate CSS file, which
will simply look something like:
p {
color: red;
}
a {
color: blue;
}
If this file is saved as "web.css" then it can be linked to in the HTML like this:
To get the most from this guide, it would be a good idea to try out the code as we go along, so start a
fresh new file with your text-editor and save the blank document as "web.css" in the same directory
as your HTML file.
Now change your HTML file so that it starts something like this:
Save the HTML file. This now links to the CSS file, which is empty at the moment, so won't change a
thing. As you work your way through the CSS Beginner Tutorial, you will be able to add to and change
the CSS file and see the results by simply refreshing the browser window that has the HTML file in it,
as we did before.
HTML has tags, CSS has 'selectors'. Selectors are the names given to styles in internal and
external style sheets. In this CSS Beginner Tutorial we will be concentrating on HTML selectors,
which are simply the names of HTML tags and are used to change the style of a specific tag.
For each selector there are 'properties' inside curly brackets, which simply take the form of words
such as color, font-weight or background-color.
A value is given to the property following a colon (NOT an 'equals' sign) and semi-colons separate
the properties.
body {
font-size: 0.8em;
color: navy;
}
This will apply the given values to the font-size and color properties to the body selector.
So basically, when this is applied to an HTML document, text between the body tags (which is the
content of the whole window) will be 0.8 ems in size and navy in colour.
Colours
CSS brings 16,777,216 colours to your disposal. They can take the form of a name,
an rgb (red/green/blue) value or a hex code.
red
Is the same as
rgb(255,0,0)
Which is the same as
rgb(100%,0%,0%)
Which is the same as
#ff0000
Which is the same as
#f00
There are 17 valid predefined colour names. They
are aqua, black, blue, fuchsia, gray, green, lime, maroon, navy, olive, orange, purple, r
h1 {
color: yellow;
background-color: blue;
}
These colours might be a little too harsh, so you could change the code of your CSS file for slightly
different shades:
body {
font-size: 0.8em;
color: navy;
}
h1 {
color: #ffc;
background-color: #009;
}
Save the CSS file and refresh your browser. You will see the colours of the first heading
(the h1 element) have changed to yellow and blue.
You can apply the color and background-color properties to most HTML elements,
including body, which will change the colours of the page and everything in it.
Text
You can alter the size and shape of the text on a web page with a range of properties, outlined below:
font-family
This is the font itself, such as Times New Roman, Arial, or Verdana.
The font you specify must be on the user's computer, so there is little point in using obscure fonts.
There are a select few 'safe' fonts (the most commonly used are arial, verdana and times new
roman), but you can specify more than one font, separated by commas. The purpose of this is that if
the user does not have the first font you specify, the browser will go through the list until it finds one
it does have. This is useful because different computers sometimes have different fonts installed.
So font-family: arial, helvetica, for example, is used so that similar fonts are used on PC
(which traditionally has arial, but not helvetica) and Apple Mac (which, traditionally, does not have
arial and so helvetica, which it does normally have, will be used).
Note: if the name of a font is more than one word, it should be put in quotation marks, such
as font-family: "Times New Roman".
font-size
The size of the font. Be careful with this - text such as headings should not just be a paragraph in a
large font; you should still use headings (h1, h2etc.) even though, in practice, you could make the
font-size of a paragraph larger than that of a heading (not recommended for sensible people).
font-weight
This states whether the text is bold or not. In practice this usually only works as font-weight:
font-style
This states whether the text is italic or not. It can be font-style: italic or font-style:
normal.
text-decoration
This states whether the text is underlined or not. This can be:
text-decoration: underline should only be used for links because users generally expect
underlined text to be links.
This property is usually used to decorate links, such as specifying no underline with text-
decoration: none.
text-transform
7
text-transform: capitalize turns the first letter of every word into uppercase.
Text spacing
The letter-spacing and word-spacing properties are for spacing between letters or words. The
value can be a length or normal.
The line-height property sets the height of the lines in an element, such as a paragraph, without
adjusting the size of the font. It can be a number (which specifies a multiple of the font size, so '2' will
be two times the font size, for example), a length, a percentage or normal.
The text-align property will align the text inside an element to left, right, center or justify.
The text-indent property will indent the first line of a paragraph, for example, to a given length
or percentage. This is a style traditionally used in print, but rarely in digital media such as the web.
p {
letter-spacing: 0.5em;
word-spacing: 2em;
line-height: 1.5;
text-align: center;
h2 {
font-size: 1.5em;
background-color: #ccc;
margin: 1em;
padding: 3em;
}
You will see that this leaves one-character width space around the secondary header and the header
itself is fat from the three-character width padding.
The four sides of an element can also be set individually. margin-top, margin-right, margin-
CSS Borders
Borders can be applied to most HTML elements within the body.
To make a border around an element, all you need is border-style. The values can
be solid, dotted, dashed, double, groove, ridge, inset andoutset.
border-width sets the width of the border, which is usually in pixels. There are also properties
for border-top-width, border-right-width, border-bottom-width and border-left-
width.
Finally, border-color sets the colour.
Add the following code to the CSS file:
h2 {
border-style: dashed;
border-width: 3px;
border-left-width: 10px;
border-right-width: 10px;
border-color: red;
}
This will make a red dashed border around all HTML secondary headers (the h2 element) that is 3
pixels wide on the top and bottom and 10 pixels wide on the left and right (these having over-ridden
the 3 pixel wide width of the entire border).
10
body {
font-family: arial, helvetica, sans-serif;
font-size: 80%;
color: black;
background-color: #ffc;
margin: 1em;
padding: 0;
}
/* this is a comment */
p {
line-height: 1.5em;
}
h1 {
color: #ffc;
background-color: #900;
font-size: 2em;
margin: 0;
margin-bottom: 0.5em;
padding: 0.25em;
font-style: italic;
text-align: center;
letter-spacing: 0.5em;
border-bottom-style: solid;
border-bottom-width: 0.5em;
border-bottom-color: #c00;
}
h2 {
color: white;
background-color: #090;
font-size: 1.5em;
margin: 0;
padding: 0.1em;
padding-left: 1em;
}
11
h3 {
color: #999;
font-size: 1.25em;
}
img {
border-style: dashed;
border-width: 2px;
border-color: #ccc;
}
a {
text-decoration: none;
}
strong {
font-style: italic;
text-transform: uppercase;
}
li {
color: #900;
font-style: italic;
}
table {
background-color: #ccc;
}
12
Contents
Class and ID Selectors - Make your own selectors without the need for sticky-backed plastic!
Grouping and Nesting - Properties assigned to multiple selectors or selectors within selectors.
Shorthand Properties - Various properties, such as borders and margins that amalgamate other
properties into one.
Background-images - Guess.
13
#top {
background-color: #ccc;
padding: 1em
}
.intro {
color: red;
font-weight: bold;
}
The HTML refers to the CSS by using the attributes id and class. It could look something like this:
<div id="top">
<h1>Chocolate curry</h1>
<p class="intro">This is my recipe for making curry purely with chocolate</p>
<p class="intro">Mmm mm mmmmm</p>
</div>
The difference between an ID and a class is that an ID can be used to identify one element, whereas a
class can be used to identify more than one.
You can also apply a selector to a specific HTML element by simply stating the HTML selector first,
so p.jam { whatever } will only be applied to paragraph elements that have the class 'jam'.
14
h2 {
color: red;
}
.thisOtherClass {
color: red;
}
.yetAnotherClass {
color: red;
}
Nesting
If the CSS is structured well, there shouldn't be a need to use many class or ID selectors.
This is because you can specify properties to selectors within other selectors.
For example:
#top {
background-color: #ccc;
padding: 1em
}
#top h1 {
color: #ff0;
}
#top p {
color: red;
font-weight: bold;
15
<div id="top">
<h1>Chocolate curry</h1>
<p>This is my recipe for making curry purely with chocolate</p>
<p>Mmm mm mmmmm</p>
</div>
This is because, by separating selectors with spaces, we are saying 'h1 inside ID top is colour #ff0'
and 'p inside ID top is red and bold'.
This can get quite complicated (because it can go for more than two levels, such as this inside this
inside this inside this etc.) and may take a bit of practice.
16
Pseudo Classes
Pseudo classes are bolted on to selectors to specify a state or relation to the selector. They
take the form of selector:pseudo class { property: value; }, simply with a colon in between the
selector and the pseudo class.
Many CSS proposals are not supported by all browsers, but there are four pseudo classes that can be
used safely when applied to links.
active is for a link when it is gains focus (for example, when it is clicked on).
a.snowman:link {
color: blue;
}
a.snowman:visited {
color: purple;
}
a.snowman:active {
color: red;
}
a.snowman:hover {
text-decoration: none;
color: blue;
background-color: yellow;
}
Although CSS gives you control to bypass it, maintaining different colours for visited links is good
practice as many users still expect this. As pseudo classes (other than hover) are not often used, this
is a feature that is unfortunately not as common as it once was. Because of this, it is less important
than it used to be, but if you are looking for the optimum user response, then you should use it.
Traditionally, text links were blue if not visited and purple if visited, and there is still reason to
believe that these are the most effective colours to use, although, again, with the increasingly
widespread use of CSS, this is becoming less commonplace and the average user no longer assumes
that links must be blue or purple.
You should also be able to use the hover pseudo class with elements other than links.
Unfortunately, Internet Explorer doesn't support this method. This is a bloody irritation because there
are lots of nice little tricks you can do that look delightful in other browsers.
17
Shorthand Properties
Some
CSS properties allow a string of values, replacing the need for a number of properties. These are
margin, padding and border-width allow you to amalgamate margin-top-width, marginright-width, margin-bottom-width etc. in the form of property: top right bottom
left;
So:
p {
border-top-width: 1px;
border-right-width: 5px;
border-bottom-width: 10px;
border-left-width: 20px;
}
p {
border-width: 1px 5px 10px 20px;
}
border-width, border-color and border-style can also be summed up as, for example:
p {
border: 1px red solid;
}
p {
font: italic bold 1em/1.5 courier;
}
(Where the '/1.5' is the line-height)
So, to put it all together, try this code:
p {
font: 1em/1.5 "Times New Roman", times, serif;
padding: 3em 1em;
border: 1px black solid;
border-width: 1px 5px 5px 1px;
18
19
Background Images
There are a number of properties involved in the manipulation of background images.
Luckily, the property background can deal with them all.
body {
background: white url(http://www.htmldog.com/images/bg.gif) no-repeat
top right;
}
background-repeat, which is how the image repeats itself. This can be repeat (equivalent to a
'tile' effect across the whole background),repeat-y (repeating on the 'y-axis', above and
below), repeat-x (repeating on the 'x-axis', side-by-side) or no-repeat (which shows just one
instance of the image).
background-position, which can be top, center, bottom, left, right or any sensible
combination, such as above.
Background-images can be used in most HTML elements - not just for the whole page (body) and can
be used for simple but effective results, such as shaped corners.
HTML elements - not just for the whole page (body) and can
20
inline does just what it says - elements that are displayed inline follow the flow of a
line. Strong, anchor and emphasis elements are traditionally displayed inline.
block puts a line break before and after the element. Header and paragraph elements are
examples of elements that are traditionally displayed block-line.
none, well, doesn't display the element, which may sound pretty useless but can be used to good
effect with accessibility considerations (seeAccessible Links), alternate stylesheets or advanced hover
effects.
The original default style-sheet for this site for example, manipulates a few traditionally in-line and
block-line elements to fit the design.
h1 {
display: inline;
font-size: 2em;
}
#header p {
display: inline;
font-size: 0.9em;
padding-left: 2em;
}
This enabled the title 'techaltum.com' and the tag-line to be displayed next to each other rather than
above and below each other while maintaining optimum accessibility.
The above code is used in the print-only styles to basically 'turn-off' those elements, such as
navigation, which are insignificant to the single page.
display: none and visibility: hidden vary in that display: none takes the element completely
out of play, whereas visibility: hidden keeps the element and its flow in place without visually
representing its contents. For example, if the second paragraph of 3 were set to display: none, the
first paragraph would run straight in to the third whereas if it were set to visibility: hidden, there
would be a gap where the paragraph should be.
21
Tables
Perhaps the best way to understand the table-related display property values is to think of HTML
tables. table is the initial display and you can mimic the tr and td elements with the table-
Getting carried away with CSS tables can seriously damage your accessibility. HTML should be used to
convey meaning, so if you have tabular data it should be arranged in HTML tables. Using CSS tables
exclusively could result in a mash of data that is completely unreadable without the CSS. Bad. And
not in a Michael Jackson way.
run-in makes an element either in-line or block-line depending on the display of its parent. It
doesn't work on IE or Mozilla based browsers. Very helpful.
compact also makes the element inline or block-line depending on the context. It doesn't work that
well either...
marker is used exclusively with the :before and :after pseudo elements to define the display of
the value of the content property. The automatic display of the contentproperty is
already marker, so this is only useful if you are overriding a previous display property for the
pseudo element.
22
Page Layout
Layout with CSS is easy. If you are used to laying out a page with tables, it may at first appear
difficult, but it isn't, it's just different and actually makes much more sense.
You need to look at each part of the page as an individual chunk that you can shove wherever you
choose. You can place these chunks absolutely or relative to another chunk.
Positioning
The position property is used to define whether an element
is absolute, relative, static or fixed.
The value static is the default value for elements and renders the position in the normal order of
things, as they appear in the HTML.
relative is much like static, but the element can be offset from its original position with the
properties top, right, bottom and left.
absolute pulls an element out of the normal flow of the HTML and delivers it to a world all of its
own. In this crazy little world, the absolute element can be placed anywhere on the page
using top, right, bottom and left.
fixed behaves like absolute, but it will absolutely position an element in reference to the
browser window as opposed to the web page, so, theoretically, fixed elements should stay exactly
where they are on the screen even when the page is scrolled. Why theoretically? Why else - this
works great in browsers such as Mozilla and Opera, but in IE it doesn't work at all.
<div id="navigation">
<ul>
<li><a href="this.html">This</a></li>
<li><a href="that.html">That</a></li>
<li><a href="theOther.html">The Other</a></li>
</ul>
</div>
<div id="content">
<h1>Ra ra banjo banjo</h1>
<p>Welcome to the Ra ra banjo banjo page. Ra ra banjo banjo. Ra ra
banjo banjo. Ra ra banjo banjo.</p>
<p>(Ra ra banjo banjo)</p>
</div>
23
#navigation {
position: absolute;
top: 0;
left: 0;
width: 10em;
}
#content {
margin-left: 10em;
}
You will see that this will set the navigation bar to the left and set the width to 10 em's. Because the
navigation is absolutely positioned, it has nothing to do with the flow of the rest of the page, so all
that is needed is to set the left margin of the content area to be equal to the width of the navigation
bar.
How bloody easy. And you aren't limited to this two-column approach. With clever positioning, you
can arrange as many blocks as you like. If you wanted to add a third column, for example, you could
add a 'navigation2' chunk to the HTML and change the CSS to:
#navigation {
position: absolute;
top: 0;
left: 0;
width: 10em;
}
#navigation2 {
position: absolute;
top: 0;
right: 0;
width: 10em;
}
#content {
margin: 0 10em; /* setting top and bottom margin to 0 and right and
left margin to 10em */
}
The only downside to absolutely positioned elements is that because they live in a world of their own,
there is no way of accurately determining where they end. If you were to use the examples above and
all of your pages had small navigation bars and large content areas, you would be okay, but,
especially when using relative values for widths and sizes, you often have to abandon any hope of
placing anything, such as a footer, below these elements. If you wanted to do such a thing, it would
be necessary to float your chunks, rather than absolutely positioning them.
24
Floating
Floating an element will shift it to the right or left of a line, with surrounding content flowing around it.
Floating is normally used to position smaller elements within a page (in the original default CSS for
this site, the 'Next Page' links in the HTML Beginner Tutorial and CSS Beginner Tutorial are floated
right. See also the :first-letter example in Pseudo Elements), but it can also be used with bigger
chunks, such as navigation columns.
Taking the HTML example below, you could apply the following CSS:
#navigation {
float: left;
width: 10em;
}
#navigation2 {
float: right;
width: 10em;
}
#content {
margin: 0 10em;
}
If you do not want the next element to wrap around the floating objects, you can apply the clear
property. clear: left will clear left floated elements, clear: right will clear right floated elements and
clear: both will clear both left and right floated elements. So if, for example, you wanted a footer to
your page, you could add a chunk of HTML with the id 'footer' and then add the following CSS:
#footer {
clear: both;
}
And there you have it. A footer that will appear underneath all columns, regardless of the length of
any of them.
This has been a general introduction to positioning and floating, with emphasis to the larger 'chunks'
of a page, but remember, these methods can be applied to any element within those chunks too. With
a combination of positioning, floating, margins, padding and borders, you should be able to represent
ANY web design and there is nothing that can be done in tables that can not be done with CSS.
The ONLY reason for using tables for layout at all is if you are trying to accommodate ancient
browsers. This is where CSS really shows its advantages - it results in a highly accessible page a
fraction of the weight of an equivalent table-based page.
25
Pseudo Elements
CSS pseudo-classes are used to add special effects to some selectors.
Pseudo elements suck on to selectors much like pseudo classes, taking the form
of selector:pseudoelement { property: value; }
Hover
mouse over link
p{
font-size: 3em;
float: left;
}
p:hover {
font-weight: bold;
}
26