Style Sheets
  Home arrow Style Sheets arrow Page 4 - Learn CSS, part 1
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
STYLE SHEETS

Learn CSS, part 1
By: Michael Youssef
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 31
    2005-05-16

    Table of Contents:
  • Learn CSS, part 1
  • CSS Specifications and Implementations
  • Browsers we are going to use
  • The HTML vs. CSS example
  • CSS rules and selectors
  • Groups of Selectors
  • Validating your CSS Documents

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Learn CSS, part 1 - The HTML vs. CSS example


    (Page 4 of 7 )

    You may say "Why would I need to learn CSS when I'm fine with HTML and I can do what I need to do with it?" Think about it: if CSS is not needed, then why has the W3C wasted a great deal of time designing the specifications, and why have browser makers (like Microsoft and Mozilla) been integrating support for CSS in their browsers? Actually, there are many problems with HTML. Suppose that we have a 100-page website; each page has five paragraphs, and each paragraph has a Tahoma font (in the form of font tags of course). After you have completed your website development, your manager says "I don't like Tahoma, so let's use Arial." Now you have to do a search and replace on 100 pages; at five tags for each page, you must replace 500 tages. Using CSS, you can take just three seconds to modify the font on all of the pages.

    CSS is a language that has been designed to format the styles of markup elements. This means that you can use CSS to format HTML, XHTML, XML, and any other markup language. HTML is a structured markup language, and as you know the font tags are deprecated, which means that they will be removed in the future from HTML. CSS is a simple, easy to use and easy to understand styling language; in fact, you can do a lot of things with CSS that you can't do with HTML. So let's look at this example:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>The IT Field</title>
    </head>
    <body>
    <p>
    <font face="Tahoma" color="red">
    Information Technology (IT) has been the most famous and attractive field in the last
    20 years and many students choose to study Computer Science and Information System which qualify
    them to work as Programmers, Developers, Database Administrators and Network Engineers.
    </font>
    </p>
    </body>
    </html>

    Now if you have 10 or even more paragraphs on a single page, and you have 100 or even more pages on your website, it's a big problem to change from Tahoma to Arial font. You may think about using the <basefont> tag, but it's still a problem when you have many pages in your website. To solve this problem, we need to create a CSS file and use the features of CSS to state that, each time we use the element <p>, a certain font will be used. Copy the following code into a file and save it as Paragraph.css, then place the file in the same folder as the above HTML file. By the way, take note the extension .css; you must save the file with the extension .css, because some browsers actually ignore the file if it doesn't have this extension.

    p
    {
    font-family: Arial, sans-serif;
    color: red;
    }

    As you can see, the CSS file contains just a few CSS lines (which we will talk about shortly). These few lines state that you will have an Arial font (or the alternative sans-serif font) with a red color every time you define a paragraph (using the <p> element), so the only place you need to modify when you change the font is the font-family property. Note that the CSS file is a standalone file. You must refer to it from every HTML page you have in order to get the effects we are talking about. Let's see how to do that in the HTML page. Please replace the HTML page with the following code:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <link rel="stylesheet" href="paragraph.css" type="text/css">
    <title>The IT Field</title>
    </head>
    <body>
    <p>

     
    Information Technology (IT) has been the most famous and attractive field in the last
    20 years and many students choose to study Computer Science and Information System which qualify
    them to work as Programmers, Developers, Database Administrators and Network Engineers.
    </p>
    </body>
    </html>

    We have removed the font tag from the document and added a link to the CSS file using the link element. The rel attribute defines the relationship between both documents -- which is that the referenced document (Paragraph.css) is a style sheet document for this HTML document. The href attribute (hyperlink reference) refers to where the document is, and accepts an absolute or relative path to the document. The type attribute defines the MIME of the referencing file, which is text/css.

    Now you can modify the font and its color through the CSS file, and it will be applied to all of the <p> elements in all of the pages as long as the pages refer the CSS file. Let's talk more about the Paragraph.css file.

    More Style Sheets Articles
    More By Michael Youssef


       · Pretty basic stuff; certainly not bad, though.You can find another (slightly...
       · Hello, I have wrote 10 articles in my Learn CSS Series and most of them have...
       · I have written the following articles in the CSS series in the following order1-...
       · As strictly a web designer (not coder!) that relied on Dreamweaver/HTML for small...
       · W3 is strictly show-and-tell. It doesn't explain the concepts presented. Units of...
     

    STYLE SHEETS ARTICLES

    - Image Replacement CSS Techniques
    - Using BlueTrip`s Success, Notice and Error C...
    - More Uses for the Thin and Caps CSS Classes ...
    - Styling Definition Lists with the BlueTrip C...
    - Styling Unordered and Ordered HTML Lists wit...
    - Using the BlueTrip CSS Framework`s Thin and ...
    - Adding Borders to Web Page Columns with Blue...
    - Introducing the BlueTrip CSS Framework
    - Using a Background Grid to Assist Web Page L...
    - Extending the Rule Of Thirds for Web Page La...
    - A Two-Column Web Page Layout Based on the Ru...
    - Using the Rule Of Thirds for Web Page Layout
    - Swapping Columns Using the Divine Ratio for ...
    - Using the Golden Ratio in Liquid Web Page De...
    - Fundamental Design Principles for Web Page L...







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek