Other special CSS selectors
Special selector notations.
What about the Universal Selector
-
The * (asterisk) selector means "anything," so if you use:
*.myclass {color:green;} /* which is the same as */ .myclass {color:green;}
-
You can use the * (asterisk) selector as:
* {color:blue;}
-
An interesting use of the * (asterisk) selector is as the inverse of the child
selector a not-a-child selector, if you will.
h2 * em {font-weight:bold;}
Example of the Universal selector:<html> <head> <style type="text/css"> p * em {color:green} </style> </head> <body> <p>This <span><em>em element</em></span> is inside an p element, but is not a direct child of the p element so it will be green.</p> <p>This <em>em element</em> is a direct child of the p element and will not be green.</p> </body> </html>
What about the Adjacent Sibling Selector.
- This rule, with a + (pluss) selects a element that follows a specific sibling element.
-
Sibling elements are at the same level in the markup hierarchy that is, they share the same parent element.
h1 + p {color: red}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <style type="text/css"> h1 + p {color: red} </style> </head> <body> <h1>All about siblings selectors.</h1> <p>There must be at least two selectors, with a + sign before the last one.</p> <p>The targeted selector will only be affected if it is a sibling to, and preceded by, the one before the + sign.</p> </body> </html>
What about the Attribute Selectors.
- Attribute selectors use the attributes of the element. They are primarily used in XML but have their uses in HTML too.
-
If you want the img (image) elements with a title attribute to
appear with a blue, 2-pixel border around so you can express in this way:
img[title] {border: 2px solid blue;}
-
If you want paragraph elements with an attribute id='beta'
to be written in red you can express it in this way:
p[id='beta'] {color: red;}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <style type="text/css"> h2[title] {color: red} p[id='beta'] {color: green} </style> </head> <body> <h2 title='The red color has nothing to do with the tiltle contents'> This text will show in red color.</h2> <p id='beta'>This text will show in green color as the attribute id='beta'.</p> <p id='alpha'>This text will show in default color as the attribute id='alpha'.</p> <p >This text will show in default color as this element has no id.</p> </body> </html>
© 2010 by Finnesand Data.
All rights reserved.
This site aims to provide FREE programming training and technics.
Finnesand Data as site owner gives no warranty for the
correctness in the pages or source codes.
The risk of using this web-site pages or any program
codes from this website is entirely at the individual user.