Editing
Selectors
(section)
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Selectors == === Contextual Selectors === Contextual selectors use more than one tag in the selector. The tag closest to the declaration is the targeted tag. The additional tag(s) state where the target tag must be located in the markup up in order for target tag to be affected. Contextual selectors have spaces between them. Example: The p is targeted tag. Only p tags within div tags will be red. <pre>div p {color:red;}</pre> ==== Contextual selector examples ==== <pre><html> <head> <title>Contextual Selector Example 1</title> <link href="style_sheets/contextual_selector_example_1.css" rel="stylesheet" type="text/css"> </head> <body> </pre> ==== Child selectors ==== Write a rule so the target tag has to be a child of a specific tag. <pre>p>em {color:green;}</pre> ==== Contextual Class Selectors ==== You can combine tag and class name to make a selector more specific. The second line states '''any tags with "specialtext" class will be bold'''. The third line states '''"specialtext" class must be within the context of p tag for rule to apply'''. <pre> p { font-family: Helvetica, sans-serif; } .specialtext { font-weight:bold; } p.specialtext { color:red} </pre> [http://www.gotopinion.info/styles/class_selector_example_0.html | Class Selector Example 0] You can further specify by adding more tags. The fourth line states '''span tag within a paragraph with "specialtext" class will be italicized''' <pre> p { font-family: Helvetica, sans-serif; } .specialtext { font-weight:bold; } p.specialtext { color:red} p.specialtext span { font-style:italic; } </pre> [http://www.gotopinion.info/styles/class_selector_example_1.html | Class Selector Example 1] ==== Skipping the restrictions of hierarchy ==== The line states '''span tag can be a descendant of any tag with specialtext class''' <pre>.specialtext span { color:blue; } </pre> === IDs === ID synax is similar to classes except a hash (#) symbol is used versus a class's period. ==== Difference between Classes & IDs ==== The ID can only be used once per page and a class may appear many times. If you want to identify a unique piece of your page's markup use an ID. If you want to apply rules to multiple tags in same page or many pages use a class. Example using an ID for a wrapper container: <pre>#wrap {min-width:800px; max-width:1000px; margin-left:auto; margin-right:auto; text-align:left;}</pre> === Universal Selector === The asterisk (*) means anything so this rule means all text will be blue. <pre>* { color:blue; } </pre> Another use is as the inverse of a child selector. This rule means any em tag that is at least a grandchild of the p tag, but not a child, is selected. <pre>p * em { font-weight:bold; }</pre> A nice tip is to set all margin and padding to zero so browsers are same. <pre>* { margin:0; padding:0; }</pre> === Adjacent Sibling Selector === Rule selects a tag that follows a specific sibling tag. p48 === Attribute Selector === Attribute selectors use the attributes of the tag. p48 <center>[[Cascade Style Sheets|Back to Cascade Style Sheets]]</center>
Summary:
Please note that all contributions to GotOpinion may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
GotOpinion:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
Edit source
View history
More
Search
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Tools
What links here
Related changes
Special pages
Page information