CHAPTER 1 · 27 lessons · 337 exercises
Put a page together
Give your text meaning with tags, then change colours and sizes with CSS. The frame of a web page, and your way in to how it looks.
The page you are reading now, and every site you open during the day, is made of text underneath. A way of writing called HTML gives it meaning — this is a heading, this is a paragraph — and a way of writing called CSS decides how it looks — headings are big, and blue. In this chapter you stand at the door of both.
The first thing to take on board is how HTML and CSS divide the work. HTML writes down what a thing is. CSS writes down how that thing should be shown. You do not use a heading tag because you want bigger text; you use a heading tag because it is a heading, and then you decide the size in CSS. Get that division into your body early and you save yourself a lot of hesitating later.
The way you write HTML is simple. Open with <p> and close with </p>. That wrapping is called a tag, and whatever is wrapped takes on the meaning of that tag. A paragraph is <p>, a heading is <h1> through to <h6>, a link is <a>. It looks like a lot to remember, but the ones you actually use come to about twenty.
Tags can be nested. A link inside a paragraph, items lined up inside a list. If the nesting stops matching up, the page falls apart with it, so build the habit here: if you open it, you close it.
CSS is written as a set of three — which one, what, how. In h1 { color: red; } that is "take h1, take the colour, make it red". The "which one" part is called a selector. Naming a tag is the simplest way to point at something, but on its own it cannot do "change the colour of just this one button". That is where class comes in. You give an element a name of your own and then aim at that name.
One more important one is div. It is a tag with no meaning of its own, used to gather a few elements into a single box. Once they are gathered you can move the whole box, or put space around all of it at once. When you get to layout, this habit of thinking in boxes really starts to pay.
The second half of the chapter moves on to the frame of the whole page (the html, head and body structure) and to space and borders. Space is unglamorous, but it does more to the impression a page makes than anything else. The space inside an element and the space outside it are specified separately. Leave that distinction fuzzy and you will spend a very long time failing to get a gap where you wanted one.
There is one more thing worth knowing about CSS: when several rules land on the same element, which one wins. A rule aimed by class beats one aimed by tag name, and a rule written later beats one written earlier. When "the CSS I definitely wrote is not working", the cause is usually just that something else is beating it. Before you go looking for a typo, suspect that you have been overridden.
In the section on space, keep the inside space and the outside space clearly apart. The inside is the gap within the border, and the background colour spreads to fill it. The outside is the distance to the neighbouring element, and it carries no background colour. Both "leave a gap", but which you use changes what you see.
As you work through it, check the preview often. CSS shows you the result of what you wrote straight away, so writing a little and looking is the fastest way through. In this course the preview sits alongside, so you can watch things change as you type.
By the end of this chapter you will be able to build a simple page of text and pictures, all the way through, with your own hands. It will not be dressed up yet, but this is the ground every web page stands on. From the next chapter you start stacking the look on top of it.
Lessons in this chapter
Give text meaning with tags
- 01Wrap it in a tagHTML is text with tags wrapped round it. Let us start with the smallest thing you can write.Go to the exercises
- 02Add some headingsPut
<h1>to<h6>headings together with paragraphs to give your writing a shape.Go to the exercises - 03Join pages with linksUse the
<a>tag and the href attribute to make text that takes you to another page.Go to the exercises - 04Put pictures on the pageUse the
<img>tag and its src attribute to place a picture. You meet a tag with no closing tag.Go to the exercises - 05Make a bullet listNest the item tag
<li>inside the container<ul>or<ol>and build a list.Go to the exercises - 07Lists and linksGet comfortable with ul and li for bullet lists, and the a tag that joins pages.Go to the exercises
- 08The basics of CSSGive the page colours and sizes — a look of its own — with CSS.Go to the exercises
Change how it looks with CSS
- 10Colour your text with CSSSelector, property, value — three parts, and the colour of your text changes.Go to the exercises
- 11Size and typefaceUse font-size for size and font-family for typeface, and remake how the same HTML looks.Go to the exercises
- 12Treat an element as a boxUse background-color, width and height to change the area of the element itself, not its text.Go to the exercises
- 13Name things with classUse the class attribute to name an element, so the same tag can carry different styles.Go to the exercises
The frame of the whole page
- 14The frame of an HTML fileLearn what head and body are for, and write the
<title>that names your page.Go to the exercises - 15Load a CSS fileUse the
<link>tag to call style.css in from your HTML, keeping contents and appearance in separate files.Go to the exercises
Build it part by part
- 16Split the page into regionsUse
<div>and class to split a page into a top, a middle and a bottom, and lay the ground for a layout.Go to the exercises - 17Build the inside of a headerNest a logo and a menu inside the band across the top of the page.Go to the exercises
- 18Put the header side by sideUse float to take elements that were stacked downwards and set them in a row.Go to the exercises
- 19Line things up inside the headerMatch the height of a line to the height of the band, so the text sits halfway down.Go to the exercises
- 20Build the inside of a footerLine up a name, some links and a copyright line in the band at the foot of the page.Go to the exercises
- 21Send the footer left and rightLearn float: right and build a band with the name on the left and the links on the right.Go to the exercises
- 22Put the main box in the middleDecide how wide the body of the page is, and set it in the middle.Go to the exercises
- 23Line up blocks of the same shapeWrite the items in a listing as blocks built to exactly the same structure.Go to the exercises
Tidy up with borders and spacing
- 24Put a border on itGive a thickness, a kind and a colour, and draw a border around a box.Go to the exercises
- 25Space inside and space outsideTell padding and margin apart, and give a cramped page some room to breathe.Go to the exercises
- 26Lay out the parts of a formBuild up boxes to type in, boxes to choose from, and a button, all inside a form.Go to the exercises
- 27Tidy up how the form looksGive labels a line of their own, make the boxes bigger, and finish the button so it is easy to press.Go to the exercises