Skip Navigation LinksHome > Categories > Code from a Category

Beginning Development with style sheets



User Name: codelecturer
Name: Mike Chauhan
Contact Me: www.datawebcoder.com/ContactUs.aspx
Home Page: www.datawebcoder.com
6 years of total IT experience including programming, application development, System Maintenance . 3 years of Experienced and expertise in .Net Framework VB, Visual Basic.Net, ASP.Net 2.0, 3.5 and AD... [More]
Viewed Times: 1071
Add Date: 01/03/2012
CSS is not exactly new. It has been around for a while now, but it never really caught on since few browsers supported it. Now that the fourth generation browsers have become the norm, more and more sites are taking advantage of it.
CSS gives you the ability to control almost every aspect of your page's layout, from text font-faces, text line-heights, text styles (like bolding and italics), colours, and margins.

There are several ways to include styles in your document. The first, and easiest to set up (initially), is to make use of the STYLE tag which, when placed in the HEAD portion of your document, will define styles for tags which appear elsewhere. For example, the following few lines will set all text which appears inside a

to a face of Arial, a point size of 12, and blue, and also turns off underlining in all hypertext links ( tags):



There are a few things to notice about this example. The first is the type="text/css" statement inside the STYLE tag. This tells your browser that the enclosed markup is text/css. It sounds obvious, but you may also define styles using JavaScript. Styles defined with JavaScript use the "text/JavaScript" mime type, accordingly. Like regular JavaScript code, it is important to be explicit when defining your statements. This ensures that browsers do not mis-interpret what you intended.

Again, notice the use of comment tags () to prevent the source from being displayed in browsers which do not support the style tag. This helps ensure that backwards compatibility will not be an issue.

Lets take a look at the definition for the P tag. Without exception, a style is contained in curly braces ( {} ), with each defined name/value pair separated by a semi-colon. You may list as many name/value pairs as you like.

I should also point out that you do not need to place your attributes on separate lines. I find it increases clarity and reduces the chance of making an error.

In the above example, font-family was defined to be "arial". What happens if the viewer does not have this particular font installed on their system? It is perfectly acceptable to list several alternatives, separated by commas, to ensure that at least some of the markup appears the way you intended. In all cases, the last alternative you provide should be a generic "family", one of which is almost guaranteed to be present on your viewer's system. There are five generic families to choose from. They are, in no particular order, serif, sans-serif, cursive, monospace, and fantasy. Please note that the exact font from each family that may be used is system dependent. The following line of code will start with verdana, switch to garamond if that is not present, and then default to serif in the end if the first two aren't present.



As is with families, fonts come in a wide range of sizes. The font-size property allows you to specify exact point-size definitions (try that with the regular font tag!). The following line of code specifies a font with a height of 12 points:


In reality, there are several ways to specify the size of a font. You may also use an "absolute-size" keyword instead of a point size. These include xx-small, x-small, small, medium, large, x-large, and xx-large. If you do decide to use an absolute-size keyword, you then have the option of re-defining a child style (more on that later) with a relative size. There are two - larger, or smaller. A percentage size is also possible if a parent size has been defined. 80%, for example, will size a font to 80 percent of its parent.

Before more types of elements are described, it is important to mention another very important way to include styles in your document. You may link an external file containing your styles, much in the same manner as you would link an external JavaScript file. The LINK tag uses an URL to source a file containing your styles. It's syntax is shown below:



Notice that, again, the MIME type is specified, and that the location of the style sheet may be relative or absolute. The format of the external style sheet is such that it must contain ONLY style definitions; HTML markup is not permitted. A STYLE tag is not necessary inside an external style sheet.

Linking an external file into your document truly unleashes the power of style sheets. By using single document linked into all your HTML documents, you can change the formatting of every single HTML page by editing one css file. Imagine a site containing a thousand pages of HTML, and you want to change the background colour of every page from white to grey. Before style sheets, it was necessary to update the BGCOLOR attribute of every BODY tag in every document. With linked in style sheets, simply edit your BODY attribute to reflect the changes:



and you are done. This is a good time to point out that colour definitions can still be defined in hexadecimal.

There is one difference between the linking of a CSS file and a regular JavaScript source file. With CSS, no modifications need to be done to the server in order for it to serve it properly. With regular JavaScript files, you may need to add a MIME type to your configuration in order to prevent it from returning a plain-text file.

Alright - back to style definitions. An important note is that styles are inherited. This means that if you define a style for a particular element, then place another element INSIDE that first element, it inherits the style of the first element along with whatever was defined for itself. In the first example,

was defined to be blue arial 12-point, and was defined to have no underlining. If you placed an anchor in your code that was inside a

tag, it would be blue arial 12-point, along with not being underlined.

The final piece of the CSS puzzle comes with the idea of IDs and classes. With classes, it becomes possible to selectively apply a style to a particular tag. In the above example, all

tags will be rendered in blue text. What if you wanted both red AND blue

tags? In this case, you would create a regular

tag (rendered in blue text), and a class of

tag which contains a different colour modifier. The following bit of code illustrates the example:



The period notation represents a class of P which will be red. Because it is defined this way, ONLY the

tag may make use of it. If you want to apply it to all tags, use:



and then any tag may use the red class. A class modifier is specified using the CLASS attribute in your document:



An HTML element may make use of only one class at a time. If the CLASS attribute is used more than once, the first style is the one applied.

There is a way to apply a style modification, however, through the use of an ID. Because you can specify both an ID and a CLASS for an element, it is possible to fine tune your styles even more. An example would be nice here:


and then later on in the document, we have something like this:


Now, a word on something called contextual styles. With CSS, it is possible to define how a tag should be rendered if it appears INSIDE another tag. Lets say you want all the bulleted text in a list to be italics. You could do something like this:


which will render all

  • tags inside
      tags in italics. If you used an ordered list (
        ), its bullets would not be affected by this style definition.

        But even then it'll only render the bullet in italics, not the text. Like HTML 3.2, font settings that get applied to text outside of tables and other tags (like lists) do not get applied inside so you have to define them again. The DIV tag is incredibly handy for applying a style without adding spaces to text and so in this case you might want to define two styles:

        And then use the DIV tag around your text inside your list, like this:


        which would render it correctly.
  • Post a Comment

    Name: (Optional)
    Email: (Optional, you can get an email if somebody replys your comments)*
    Email me if somebody respons my comment below:
    Details**:
    Enter Text
    as Below:
    (case insensitive, if hard to read, click the "get a new one" button)
     
        
    * Your email address will not be shared with any third parties for any reason.
    ** Maximum 1000 charactors.