Wednesday, 26 October 2011

Class And ID Selector


There are two different ways through which the style can be applied.

Ø       Class selector

There may be situation when the HTML elements of the same type requires different type of styles. At that time the inline method can be used but the HTML code becomes lengthy. So for this the class selector is used in styles. The class selector defined as a .(dot).

Syntax

          [selector].classname
            {
                        Property:value;
            }

With the class selector you can define different styles for the same type of HTML element. say that you would like to have two types of paragraphs in your document: one right-aligned paragraph, and one center-aligned paragraph. Here is how you can do it with styles:

Example

<html>
          <head>
                   <style type=”text/css”>
p.right
{
text-align:right;
}
p.center
{
text-align:center;
}
                   </style>
          </head>
          <body>
                   <p class="right">This paragraph will be right-aligned.</p>         
                   <p class="center">This paragraph will be center-aligned.</p>
          </body>
</html>

Note: To apply more than one class per given element, the syntax is:

<p class="center bold">This is a paragraph.</p>

The paragraph above will be styled by the class "center" AND the class "bold".

You can also omit the tag name in the selector to define a style that will be used by all HTML elements that have a certain class. In the example below, all HTML elements with class="center" will be center-aligned:

.center
          {
                   Text-align:center;
            }

In the code below both the h1 element and the p element have class="center". This means that both elements will follow the rules in the ".center" selector:

<h1 class=”center”> This paragraph will also be center-aligned.</h1>
<p class="center">This paragraph will also be center-aligned.</p>

Note : Do NOT start a class name with a number! This is only supported in Internet Explorer.

Ø       Add Styles to Elements with Particular Attributes

You can also apply styles to HTML elements with particular attributes.
The style rule below will match all input elements that have a type attribute with a value of "text":

Input[type="text"]
{
background-color:blue;
}

Ø       ID selector

You can also define styles for HTML elements with the id selector. The id selector is defined as a #.

Syntax

          [selector]#idname
            {
                        Property:value;
            }

Example

<html>
          <head>
                   <style type=”text/css”>
P#test
{
          color:green;
}
                   </style>
          </head>
          <body>
                   <p id=”test”>Hello World </p>                 
          </body>
</html>
The style rule above will match the element that has an id attribute with a value of "green":

Note : Do NOT start an ID name with a number! It will not work in Mozilla/Firefox.

Ø      CSS Comments

Comments are used to explain your code, and may help you when you edit the source code at a later date. A comment will be ignored by browsers. A CSS comment begins with "/*", and ends with "*/", like this:

No comments:

Post a Comment