CSS Basics 103 - Review of Selector Styling |
|||||
|
|||||
Featuring:
Review of targeting style rules with selectors and inheritance This will be a quick review of the two most important targeting rules in CSS . Here are the example CSS styles that will be used in this review: h2 , li { color: red; background-color: yellow; font-weight: bolder;}
In our previous CSS tutorials, we have seen how to apply styles contained in a .css file or in a <style>..</style> section at the top of a Web page in the <head> block. Remember if the CSS style rule is in a <style>..</style> section it only applies to that Web page unless you copy the style to other pages. If it is in a .css file then the CSS style can be applied to any Web pages with <link...>s to that .css file. .altrow { background-color: white} #backer {background-image:url(images/1animphotof.gif); height:200px; width:790px;} .whiter { color:white; font-weight:bold} The most important rule for applying CSS is simple: Rule I - define styles for one or more HTML tags h2 , li { color: red; background-color: yellow; font-weight: bolder;} This is a multi-tag styling rule which creates a warning style that will apply to all <h2> and <li> tags used on this page. Thus I have defined the rule in the <style> .. <style> section. Henceforth on this page, these two tags will have a bold red font and yellow background like so: This is an example of a Tag Selector CSS Rule
Notice that the stylings for the two tags did come out not identically the same - the <h2> has a border, the <li> does not. That is because back in the master styling file, osstyle.css, which is linked to this page there is a prior definition for <h2>(in contrast <li>starts with its default styling). Ahhh the plot thickens. What happens when there are two definitions for the same tag. Think Cascade. So now we are ready to go onto the second most important CSS styling rule.
So these two targeting methods are the meat and potatoes of CSS usage but as we noted previously there are CSS identity selectors plus lots more methods available to target where styles are to be applied. Here is one more targeting method which change how Selectors are written and work - the Identity Selectors. This is a sample of a id= styling
In the table row above we apply two stylings, #backer to bring a distinct background image that is repeated throughout the background (it is using an animated gif file). While #whiter changes the font color to white for the and applies some big margin values for the <h1> tag its used with.Note as in the case of class styles dropping the initial '.', so too the initial '#' is dropped in id="backer". Also in this tutorial demonstrates how to take advantage of these identity stylings with simple JavaScript. For those wanting to do more - I would recommend reading the following books/resources and tutorials about JavaScript, DOM and AJAX. Summary This tutorial reviews the dramatic changes one can make to their website with a few simple changes to a central .css file. It uses basic CSS stylings which we have learned in just three lessons. In short, it demonstrates the versatility of CSS. Users looking for more details on id= and class= options should check pages 16-40 in CSS in Easy Steps by Mike McGrath or pages 32-35 in CSS: The Definitive Guide 3d Edition by Eric Meyer. The next tutorial will look at background stylings. Top of Page Tutorials Home CSS references |
|||||