Wouldn't be great if you could give your pages the layout they deserve?
Sure, but how?
To give your website layout use Cascading Style Sheets (CSS). In this lesson you will get a short introduction to CSS. But later you can learn all about CSS from scratch in our CSS tutorial. So please consider this lesson only as an appetizer.
CSS is the better half of HTML. And in coding, there is no equality of status: HTML takes care of the rough stuff (the structure), while CSS gives it a nice touch (layout).
As shown in Lesson 7, CSS can be added with the style attribute. For example, you can set the font type and size on a paragraph:
Example 1:
Will look like this in the browser:
This is typed in size 20px
This is typed in Courier
This is typed in Courier size 20px
In the example above we use the
style
attribute to specify the type of font to be used (with the command font-family
) and the font size (with the command font-size
). Notice how in the last paragraph we set both the font type and size with a separating semicolon.It seems like a lot of work?
One of the smart features of CSS is the possibility to manage your layout centrally. Instead of using the
style
attribute in each tag, you can tell the browser once how it must layout all the text on the page:
Example 2:
In the example above CSS has been inserted in the head section and therefore applies to the entire page. To do this, just use the tag
<style type="text/css">
which tells the browser that you are typing CSS.
In the example all headings on the page will be in Arial in size 30px. All subheads will in Courier size 15. And all text in normal paragraphs will be in Times New Roman size 8.
Another option is to type the CSS in a separate document. With a separate CSS document you can manage the layout of many pages all at once. Pretty smart if you want to change the font type or size on a large website with hundreds or thousands of pages. We won't go into that now but you can learn it later in our CSS tutorial.
What else can I do with CSS?
CSS can be used for much more than specifying font types and sizes. For example, you can add colours and backgrounds. Here are some examples for you to experiment with:
Try inserting the examples in some of your pages - both as shown above and also as CSS inserted in the head section.
Is CSS nothing but colours and font types?
Besides adding layout such as colors, font types etc., CSS can also be used to control the page setup and presentation (margins, float, alignment, width, height etc.). By regulating the different elements with CSS you are able to layout your pages elegantly and precisely.
Example 3:
Will look like this in the browser:
With the property
float
an element can either be floated to the right or to the left. The following example illustrates the principle:
Example 4:
Will look like this in the browser:
arzzman |
---|
In the example, one element (the image) floats to the left and the other element (the text) fills the hole.
With the property
position
, you can place an element exactly where you want it in your page:
Example 5:
In the example the image is placed 50 pixels from the bottom and 10 pixels from the right in the browser. But you can place it exactly where you want it. Give it a try. Pretty easy and pretty cool, eh?
Cool, sure. But easy?
You do not learn CSS in 10 minutes. And as mentioned above, this lesson is only meant as a brief introduction. Later you will learn much more in our CSS Tutorial.
For now, concentrate on HTML, and move on to the next lesson where you will learn how to get your website out on the Internet so the whole world can see it!