Style Sheets
  Home arrow Style Sheets arrow Page 2 - Learn CSS, Selectors, part 2
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, Selectors, part 2
By: Michael Youssef
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 14
    2005-06-13

    Table of Contents:
  • Learn CSS, Selectors, part 2
  • The Attribute Selector
  • Specific-Value Attribute Selector
  • The Specific-Values Attribute Selector
  • The Begin With Specific-Value Attribute Selector
  • The End With Specific-Value Attribute Selector
  • The Contain-Value Attribute Selector

  • 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, Selectors, part 2 - The Attribute Selector


    (Page 2 of 7 )

    This is the simplest and most basic attribute selector. You can use it to apply certain styles to an element that has a specific attribute. This can be useful in many ways; for example, in XML the attribute selector can give a style to an element when it has a certain attribute, regardless of this attribute's value. Let's take a look. Below is an XML file (save it as Books.xml) followed by the CSS code (save this file as Selectors.css)

    <?xml version="1.0" encoding="UTF-8"?>
    <?xml-stylesheet type="text/css" href="Selectors.css"?>
    <Books>
    <Book>
    <title>Programming Microsoft ASP.NET</title>
    <Author Rank="5">Dino Esposito</Author>
    </Book>
    <Book>
    <title>Inside C#</title>
    <Author>Andrew Whitechapel</Author>
    </Book>
    </Books>

    The CSS code:

    Author
    {
    display: block;
    }
    Author[Rank]
    {
    color: Blue;
    background-color: YellowGreen;
    font-size: .9em;
    font-family: Arial, Tahoma;
    display: block;
    }

     
    Open this XML document using a Web browser that supports attribute selectors, such as Mozilla 7.1 or Opera 7.5 (remember, IE doesn't support attribute selectors). You will get the following result:

    The XML file contains the root element <Books>, which contains two <Book> elements. The first <Book> element contains the <Author> element, which has the Rank attribute. The following CSS syntax states that every time the <Author> element has the Rank attribute, the element will have a GreenYellow background and a blue color with an Arial font and a size of 90% of the default size font.

    Author[Rank]
    {
    color: Blue;
    background-color: YellowGreen;
    font-size: .9em;
    font-family: Arial, Tahoma;
    display: block;
    }

    The syntax is very simple; after the element name, put the attribute inside brackets and you are done. Note that the second <Book> element doesn't have any style because there's no Rank attribute for this <Book> element. You can apply the styling rule if the element has more than one attribute, but the syntax will be different. Modify the XML document as follows:

    <?xml version="1.0" encoding="UTF-8"?>
    <?xml-stylesheet type="text/css" href="Selectors.css"?>
    <Books>
    <Book>
    <title>Programming Microsoft ASP.NET</title>
    <Author Rank="5" Country="Italy">Dino Esposito</Author>
    </Book>
    <Book>
    <title>Inside C#</title>
    <Author>Andrew Whitechapel</Author>
    </Book>
    </Books>

    We have added the attribute Country to the <Author> element. Now modify the CSS code as follows:

    Author
    {
    display: block;
    }
    Author[Rank][Country]
    {
    color: Blue;
    background-color: YellowGreen;
    font-size: .9em;
    font-family: Arial, Tahoma;
    display: block;
    }

    As you can see, we have added the attribute Country inside [] to the Author attribute selector. If you reload the Web page, you will find the same formatting style. The same applies to HTML; we will see examples in the next sections.

    More Style Sheets Articles
    More By Michael Youssef


     

    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