| You are here: | About>Computing & Technology>Web Design / HTML> CSS> CSS2> CSS 2 Selectors and How to Use More Advanced Cascading Style Sheets Selectors |
![]() | Web Design / HTML |
More Help With CSS SelectorsCSS 2 SelectorsCSS 2 Selectors Extend What You Can StyleCSS 2 selectors are an extension of CSS 1 selectors, but, as you might expect, they add more options and features to the mix. These selectors are supported by modern browsers, so there is no reason not to start using them to make your style sheets more specific and direct. CSS 2 Universal SelectorA universal selector is exactly what it sounds like: a selector that selects universally. In other words, if you use the universal selector it will match the name of any element type in your document. It is written as a star: * You can use the universal selector any place you would use an element name. So, for example, if you wanted to match any child element in a blockquote, you would write: blockquote * { ... } CSS 2 Attribute SelectorsWith CSS 2 you can now select elements that have specific attributes. You can also match on the attribute's value. For example, if you want to set a style on all anchor tags that have the href attribute (and not match on anchor tags that don't, such as named anchors), you would write: a[href] { ... } If you wanted to only match on anchors links that pointed to a specific site, you would write the following. Note that this must be an exact match, links that pointed to sub-pages within the href listed would not match. a[href="http://webdesign.about.com/"] { ... } You can also do partial matches on attribute values:
New CSS 2 Pseudo-Elements and Pseudo-ClassesCSS 2 brings a number of new pseudo-elements and -classes.
CSS 2 Child and Sibling SelectorsCSS 1 allowed you to select elements that were descendants of another element, but with CSS 2 you can now specify that you only want direct children selected, or you only want elements that are siblings (have the same parent) of one another. Child Selectors div ] p { ... } You can also combine child selectors with descendant selectors. For example, to match all p tags that are descendant from an ordered list child element that is decendant from a div, you would write: div ol]li p { ... } Adjacent Sibling Selectors h1 + h2 { ... } As with child selectors, you can combine other selectors. For example, to match all paragraphs that follow an h2 with a special class, you would write: h2.special + p { ... } Use CSS 2 SelectorsUnderstanding and using CSS 2 selectors will make your style sheets more powerful. You can match elements more directly and specifically, and display styles that exactly bring across your meaning. CSS 2 selectors are an important part of CSS. More Help With CSS Selectors |
Las Vegas on a BudgetFind a BargainHotel DealsCheap EatsFree AttractionsEntertainment for Less |
All Topics | Email Article | | | ![]() |
| Advertising Info | News & Events | Work at About | SiteMap | Reprints | Help | Our Story | Be a Guide |
| User Agreement | Ethics Policy | Patent Info. | Privacy Policy | ©2008 About, Inc., A part of The New York Times Company. All rights reserved. |


