+ Reply to Thread
Results 1 to 4 of 4

Thread: CSS Part 3: CSS Selectors

  1. #1
    Lop's Avatar
    Lop
    Lop is offline Speaks fluent binary
    Join Date
    May 2006
    Posts
    1,178
    Rep Power
    30

    CSS Part 3: CSS Selectors

    CSS Tutorials
    Chapter 3: CSS Selectors

    We now move into the first advanced topic in CSS, the Selectors. A CSS Selector is basically what its name implies. It is used to "select" the elements on a HTML page so as to apply styles. The different types of CSS selectors are...
    1. Type Selectors
    2. Class Selectors
    3. ID Selectors
    1. Type Selectors
    Type selectors is the most common selector used in CSS. A type selector simply applies the style to the corresponding matching HTML element in the page. Let us go through an example for this. Note that for examples in this tutorial, i would be using internal CSS. You are however free to use this in an external CSS and try it out

    HTML Code:
    <html>
    <head>
    <style>
    h1
    {
    color:pink;
    }
    
    h2
    {
    color:violet;
    }
    </style>
    <body>
    <h1>This is a pink rose</h1>
    <h2>This is a violet sunflower</h2>
    </body>
    </html>
    The output looks like this:


    So basically the CSS engine built into the web browsers matches the corresponding tag between the < > brackets and applies the corresponding style attributes on them. Isn't this cool? Another important thing is that Type selector is so common that almost all modern browsers support them. So you can use them without any thoughts on browser compatibilities.

    2. Class Selectors
    To understand the need for class selectors, consider the following scenario. What if you wanted to apply a particular class of style to be applied to multiple elements in a HTML page? Lets say you want heading 3 as well as paragraph tags to be of a particular style say of a violet font color. It would be rather redundant if you use type selectors here. For such cases, introducing Class Selectors.

    Consider the following code Code:
    HTML Code:
    <html>
    <head>
    <style>
    .violetBig
    {
    color:violet;
    font-weight: bold;
    }
    </style>
    <body>
    <p class="violetBig">How do i appear on screen</p>
    <h3 class="violetBig">This is Hi from H3... i am popular among HTML community</h3>
    
    <br /><div style="z-index:3" class="smallfont" align="center">LinkBacks Enabled by <a rel="nofollow" href="http://www.crawlability.com/vbseo/">vBSEO</a> 3.0.0</div></body>
    </html>
    The output looks like this...


    A class selector must start with a period followed by the name of the class. like the tag, .violetBig described above. Both the paragraph as well as h3 tag have specified the class name through the class attribute.

    If we need class based specialization for a particular HTML tag, we can do so as given below.

    HTML Code:
     <html>
    <head>
    <style>
    div.violetBig
    {
    color:violet;
    font-weight: bold;
    }
    
    div.blueBig
    {
    color:blue;
    font-weight: bold;
    }
    </style>
    <body>
    <div class="violetBig">I am a violet rose</div>
    <div class="blueBig">I am a blue rose</div>
    <br /><div style="z-index:3" class="smallfont" align="center">LinkBacks Enabled by <a rel="nofollow" href="http://www.crawlability.com/vbseo/">vBSEO</a> 3.0.0</div></body>
    </html>
    The output looks like this...


    3. ID Selectors
    Certain HTML elements in a document has an ID attribute. Using ID makes it easy to write and debug Javascript code. IDs are used to uniquely identify a particular HTML element in a page. An ID selector can be used to apply style attributes to a particular ID in a web page.

    HTML Code:
    div#menuBar
    {
    font-color:green;
    }
    
    <div id="menuBar">
    Home<br />
    Products<br />
    Services<br />
    Contact Us<br />
    
    </div>
    The disadvantage of ID based selector is that it can be used only once in a given HTML page while class based selectors can be used as many times as we want.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Re: CSS Part 3: CSS Selectors

    Techically an ID can be used as many times as you like.. but that fails validation, of course.

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  4. #3
    Jordan Guest

    Re: CSS Part 3: CSS Selectors

    Another nice one! +rep

    Posted via CodeCall Mobile

  5. #4
    elizas is offline Newbie
    Join Date
    Feb 2010
    Posts
    2
    Rep Power
    0

    Re: CSS Part 3: CSS Selectors

    There can be multiple CSS classes and inline style applied to a single html element. By the default CSS rule, if same style attribute is set with different values on top of a single element, the closest attribute value will dominate in the out put at the browser end. !important is a special attribute that can break the rule, and can force a particular style irrespective of the hierarchy of all the styles applied.


    Eliza

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. C Help Part 2
    By AntLaTech in forum C and C++
    Replies: 3
    Last Post: 12-18-2010, 09:54 AM
  2. Replies: 1
    Last Post: 11-02-2009, 05:46 AM
  3. Replies: 1
    Last Post: 11-02-2009, 05:43 AM
  4. jQuery: Selectors
    By Brandon W in forum JavaScript Tutorials
    Replies: 8
    Last Post: 02-26-2009, 05:23 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts