|
|||||
|
|||||
| Featuring:
CSS-Cascading Style Sheets - More Basic Text styling and Targeting Motivation: How to target when/where CSS will be applied- Selectors are the key As we have seen in our intro CSS tutorial, Cascading stylesheets provide web developers
with powerful ways to format and style their pages. But even more important,
web developers are given better means to change their minds and modify their
stylings as a website matures overtime. Why better ? Because the stylesheets
are separate from the HTML tags which provide web page structuring. So CSS simplifies the original HTML, often substantially by ridding the HTML of bloated formating options. But also by having the stylesheets in one .css file, it is easier to do 2 things: 1)make a styling change because often only one style rule in one .css file can change dozens of HTML tags and stylings spread over dozens of files. This is hardcore efficiency;
2)re-using a particular styling is much easier as the developer does not have to search through dozens of files; and can often make simple changes or add a new style based on changes to a copy of an existing style. So the question becomes when and how to use CSS styles most effectively. The Art of Targeting In this tutorial we shall see how to use and target CSS stylesheets so they apply to specific text , images, or containers. But if you have been applying stylings primarily through HTML tags like font= or b=, it is hard and inefficient to remove all HTML styling tags or what I call single property stylings . And so I only do so using CSS HTML tag stylings. However, whenever I see two or more styling properties being applied to a single object- then I immediately target that as a candidate for conversion to a class= style rule. I discuss in detail both methods below. For example, I was using underlines with bolding fairly often in reviews. That usage of two styling properties immediate became the following class= style rule: <h6>has default margin settings which are retained and used along with the new tag stylings such as the green text, bold and larger font size.Second now whenever I use the <h6> tag on a web page which uses the osstyle.css file, it will always result in green bold large text. But you can redefine the styles for any HTML tag by adding a style= option when the tag is used. This in fact is the next example, targetting for one specific tag usage.3)Using the style= option in any HTML tag. This allows the styling to be applied directly to a specfic point on the Web page and not be associated with a particular HTML tag. In this case, we use a <span> tag and use the underbold styling properties; however it could be just about any HTML tag. So, <span style="text-decoration: underline; font-weight: bolder;"> That was easy enough to do</span> - then produces the following styling: That was easy enough to do Note here that <span> does the equivalent of the .underbold . But there is a danger here with the the style= approach to CSS styling. I have seen Web pages with just as many style= options as there were old <font> tags. This is one of the situations CSS is designed to fix. So lets try a third method, creating your own style class. This will allow Web designers to shorten the reference to class="thisStyle" like statement as shown immediately below. 4)create your own style class in the<style>..</style> section or your own mystyles.css file like so: .underbold {text-decoration: underline; font-weight: bolder;} - apply it with <p>, <div>, <span> or any other HTML tag that allows the class= option(most do): <h7 class="underbold">That was easy enough to do</h7> - which results in: Notice that a period "." has to be put in front of underbold when defining the class in the <style>..</style> section or your own class file. However, when using the style in a class= option (as in the <h7> tag just above), now the period is not required and must not be used. But also notice the power of defining your own style class. Now you can add underbolding to just about any HTML tag - say <em> as we have just done here. So creating your own set of style classes is one of web developers major styling tasks and opportunities. Now remember, this is just 4 of what are dozens of ways that that the .underbold or .greenlite or .whateverYourFavoriteStyleIs can be applied or targetted on a Web page. So as you shall see throughout these tutorials, learning how targeting works through CSS Selectors is as important as mastering all the different styling Descriptors. A Second Look at Style= <p style=" border: solid darkred 4px; color: green; font-weight:bolder; font-style: italic;"> In this and the last tutorial I have been pretty harsh on using the style= option way of applying CSS in HTML tags as seen just above. I say it clutters up your HTML file, causing the same confusion as those huge font="..." monsters. However, there are two arguments in favor of style= . First, how many times are you going to reuse "border: solid darkred 4px; ... " . Exactly. Likely never, so why not keep the style statement right next to the text it is modifying.
Second, these inline style rules can cascade because CSS directives cascade
down. Thus, the latest inline "style=" directive taking precedence over a document
style. So in my osstyles.css file I have the statement: So for me, the <p> tag has already been defined. However, by using the style= option in the <p> clause above, that styling is overridden and a new style applied. In effect, style= allows me to apply a specific, targeted style very effectively and override parts or all of my general stylings. In this case I have added a light orange background-color to this paragraph. So cascading style= rules similar to this one are, like targeting through Selectors, the essential secret to applying CSS successfully. Now I took a course at a forever-shall-it-be-nameless university where the professor taught us with big points off in exams never to use the style= option. So be aware, though CSS styling should be a matter of efficiency and effectiveness and less about the style of styling - CSS, alas like religion, has its culture wars.Summary We have just sampled some of the essentials rules of Selector usage as a way of targeting or applying stylings to specific text or portions of a page. There is more, much more, to targeting than we have covered here as we shall see in upcoming tutorials. Also we got a quick intro into the the nature of Cascading CSS rules. For readers interested in exploring these topics further, let me recommend two excellent texts on CSS.CSS in Easy Steps by Mike McGrathcover these topics and more on pages 15-32 in a short but very well illustrated book on CSS. CSS: The Definitive Guide 3d Edition by Eric Meyer covers these topics on pages 23-61. Top of Page Home Tutorials |
|||||