Welcome
Arachne's Web
A group focused on serving the AW community by providing help and discussion on topics such as HTML, CSS, web design, homesite decorating, netiquette and issues important to web artists.

Arachne's Web Academy (- threads, 696 posts)
    Tutorials - Cascading Style Sheets (47 posts)
    Social Thread

    Stylesheets let you control the look of your pages like never before, right down to the spacing between letters. Here is where we'll learn how it's all done. ...
    8 Members have made 32 Posts here to date.
    Google
    AncientWorlds.net Web
    Next: Loved The Tutorials!
    Prev: 08-Inheritance
    09-Classes
    Sankira.gif
    Author: * Sankira Qin - 15 Posts on this thread out of 1,340 Posts sitewide.
    Date: May 28, 2005 - 17:30

    Redefining an HTML Element

    As discussed in 04-Selectors, you can override the pre-existing properties of HTML tags using any of the selector types. Probably the most used selector type is the class selector, so this deserves a little closer look.

    Pre-existing Properties Before you can apply new formatting to an existing tag, you need to know what the tag does. Decide which properties you want to keep and which you want to change. Using a previous example, let's look at the tag. It renders the text in boldface type using the default font face, color, and size. If you want to change all the bold tags in your document you can use the element selector like this:

    b {color: red; }


    But suppose you want to use the default bold throughout most of your document, and have it appear red in only a few places? You could apply the style attribute to only those tags you want to appear red. Like this:

    I won First Place in Totally Tribond!


    But again, you would be limiting the benefit of CSS if you included this in several places throughout your document. You might do it, if you're using different colors in each tag, but suppose you've done it to match your Study. When you decide to change your color scheme, you would have to change each tag again. You can get around this by defining any additional or changed properties in a class selector.

    Choosing a class name. To begin defining your class selector, choose a name. We'll call ours "mybold". We could have as easily called it "b42" or "lovely", but since you'll probably be defining lots of these, it's a good idea to make it something that reminds you of what it does. In addition, we could have called it "redbold", but what if you change the color later (as we intend to do)? It would still work, but "redbold" if the color is now purple would be confusing to future designers who might be editing your webpage (or in the case of AW, who might be helping you troubleshoot a problem).

    Defining the rule.In your stylesheet, type a period (.) and the word "mybold" followed by a space. Open your class with a left curly brace ({) and add your declarations. End with a semi-colon (optional in this rule), and then don't forget to close your class rule with a right curly brace (}). Like this:

    .mybold {color: red; }

    Applying the rule. Now you have a class rule. But unlike element selectors that automatically apply the new rule to the HTML tags you redefined, you have to tell HTML to use class selectors. So, when you're ready to use the rule inside your document, add it to the HTML tag using the class= attribute. Like this:

    I won First Place in Totally Tribond!


    Now, if you decide to change the color to purple, you only have to change it in your class rule and it will affect all the bold tags to which you originally applied it.

    Coding Tip: I mentioned using the style= attribute for applying different colors to multiple bold tags. If you never intend to change all those bold colors, you could do this. But if you MIGHT someday want to change them, it's still a good idea to define multiple selectors, such as "b#mybold1", "b#mybold2", "b#mybold3" and apply those using the ID= attribute. Editing later would be much easier, since all the classes will be grouped together in your stylesheet, saving you the trouble of scanning thru your code to find them. This is a particular consideration if you have a complicated webpage. We'll discuss ID selectors more after we've learned about positioning.


    Applying multiple classes. There are two different ways to apply more than one class to an element. As always in these tutorials, we'll use a very simplified example. Let's say you have a class for centering elements (text, tables, images) and another class for creating a box around elements (like the box used to surround our examples). Like this:

    .cntr {text-align: center; }
    .box { color: #D6C6A5; border: 1px solid; padding: 10px; border-color: #408E40; width: 350px; margin-left:100px; }

    Now, let's say you want the first paragraph in your gram to be left aligned (the default) but the second item should be centered. You can apply the gram rule to the entire block and the cntr rule only to the second item. Like this:

    This is one of my favorite symbols:


    Don't you just love Celtic knotwork?

    This would give us:

    This is one of my favorite symbols:

    Warning

    Don't you just love Celtic knotwork?

    Notice the second div (cntr) is opened immediately before and closed immediately after the image tag. This insures that only the image will be centered. If you wanted to center everything, you could open the border with a div tag and the centering with a span tag...or use two divs, one after the other, and then close both at the end. But CSS provides a short cut. If you want to apply more than one class to an element, simply list them inside the class= attribute, leaving a space between them. Like this:

    This is one of my favorite symbols:


    Don't you just love Celtic knotwork?

    That will give you this:

    This is one of my favorite symbols:

    Warning

    Don't you just love Celtic knotwork?


    Either would work, but the second method give you cleaner code. Such shorthand methods are nice to have when your code starts getting longer and longer!


    NEXT: Loved The Tutorials!
    PREV: 08-Inheritance
Rome - Rome, Season 1 - The Stolen Eagle


Copyright 2002-2011 AncientWorlds LLC | Code of Conduct and Terms of Service | Contact Us! | The AncientWorlds Staff