Lesson 12: Layout (CSS)

Wouldn't be great if you could give your pages the layout they deserve?

Sure, but how?

Use Cascading Style Sheets (CSS) to give your website layout, style, and the formats you want. 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, the style attribute can control text colour:

Example 1:

	
	<h2 style="color:green">HTML</h2>
	
	

Will look like this in the browser:

HTML

In the example above we use the style attribute to change the colour. If you wanted to change the colour of every h2 on the page, you'd have to set the style attribute on all of them.

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:

	
	<html> 
	
	  <head>  
	    <title>My first CSS page</title>
	    <style type="text/css">
	      h1 {font-size: 30px; font-family: arial;} 
	      h2 {font-size: 15px; font-family: courier; color:green;} 
	      p {font-size: 8px; font-family: "times new roman";} 
	    </style> 
	  </head>
	  
	  <body>
	    <h1>My first CSS page</h1>
	    <h2>Welcome to my first CSS page</h2>
	    <p>Here you can see how CSS works </p> 
	  </body>
	
	</html> 
	
	

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 green. And all text in normal paragraphs will be in Times New Roman size 8 (notice how we set both the font size and font-family with a separating semicolon).

Another, even better option is to create the CSS in a separate document. With a separate CSS documents you can manage the layout of many pages all at once. This is 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 columns, colours and backgrounds. Here are some examples for you to experiment with:

	
	<body style="background-image: url('http://www.html.net/logo.png');"> 

	<p style="color:green;">Green text</p>

	<h1 style="background-color: blue;">Heading on blue background</h1> 
	
	

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 are also used to control the page layout 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:

	
	<p style="padding:25px;border:1px solid red;">I love CSS</p>
	
	

Will look like this in the browser:

I love CSS

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:

	
	<img src="bill.jpg" alt="Bill Gates" style="float:left;" >

	<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, 
	sed diam nonummy nibh euismod tincidunt ut laoreet dolore 
	magna aliquam erat volutpat. Ut wisi enim ad minim veniam, 
	quis nostrud exerci tation ullamcorper suscipit 
	lobortis nisl ut aliquip ex ea commodo consequat...</p> 
	
	

Will look like this in the browser:

Bill Gates

Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat...

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:

	
	<img src="bill.jpg" alt="Bill Gates" style="position:absolute;bottom:50px;right:10px;" >
	
	

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!


Related topics in the RepliesViews
No related topics yet

+ Post a new topic


<< Lesson 11: More about tables

Lesson 13: Uploading pages >>