| You are here: | About>Computing & Technology>Web Design / HTML> CSS> Advanced CSS> CSS 1 Selectors and How to Use Cascading Style Sheet Selectors |
![]() | Web Design / HTML |
More Help with CSS SelectorsAdvanced CSSCSS Descendant SelectorsWhy Use Descendant Selectors CSS SelectorsCSS SelectorsThe Basics of CSS 1 SelectorsBasic CSS 1 SelectorsThe selector is the part of the CSS style definition that defines the exact elements to apply the style to. At its most basic, this is the actual element name. For example, if you wanted to style all your first-level headings as red, you would use an h1 selector: h1 { color: red; } CSS 1 Class SelectorsIn the event that you didn't want to define every first-level heading as red, but only a sub-set, you can use a class selector (written as a string with a dot in front of it). For example, if you wanted that same h1 to be part of a sub-set of red h1s, with the rest staying as normal, you would use a class selector: h1.red { color: red; } CSS 1 ID SelectorsID selectors work nearly the same as class selectors, only they only impact one element on the page (the one with that specific ID), and they are written with a pound-sign (#) at the front of the word. For example, if you had one ID'd h1 that you wanted red: #myredh1 { color: red; } CSS 1 Pseudo-ClassesIn CSS 1 there are a couple of useful pseudo-classes that help you define styles based on characteristics of the element or page rather than specifically on the elements themselves. There are five pseudo-classes in CSS 1:
CSS 1 Descendant Selectors or Contextual SelectorsCSS 1 selectors also allow you to define a selector based upon where it is in the HTML tree. For example, while you might want to make every h1 in your document red (using the basic selectors above), you might want to call more attention to any emphasized (<em></em>) text within that h1. While you could set another class on that em, it's easier to just set a special style on all emphasized text that falls within an h1. Use spaces to separate the tags, rather than commas, to indicate descendant selectors: h1 { color: red; } You can also use wildcards to define any number of levels in your descendant selectors. For example, if you wanted all q tags that were grandchildren of a div, you would write: div * q { border: thin solid blue; } This style will not affect any q's that are direct descendants of a div or q's that are not within a div at all. More Help with CSS SelectorsAdvanced CSSCSS Descendant SelectorsWhy Use Descendant Selectors CSS Selectors |
Las Vegas on a BudgetFind a BargainHotel DealsCheap EatsFree AttractionsEntertainment for Less |
All Topics | Email Article | Print this Page | | ![]() |
| 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. |


