HTML CSS JavaScript jQuery AJAX ASP PHP SQL tutorials, examples for web building ,author kapil kumar: Layout (CSS)

Tuesday 20 May 2014

Layout (CSS)

To give your website layout use Cascading Style Sheets (CSS). In this lesson you will get a short introduction to CSS.

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).

CSS can be added with the style attribute. For example, you can set the font type and size on a paragraph:

Example 1:
 
 <p style="font-size:20px;">This is typed in size 20px</p>
 <p style="font-family:courier;">This is typed in Courier</p>
 <p style="font-size:20px; font-family:courier;">This is typed in Courier size 20px</p>
 
 
Will look like this in the browser:
This is 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:
 
 <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;}
    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 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:
 
 <p style="color:red;">Redtext</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 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:
 
 <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.

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!


No comments:

Post a Comment