CSS AND USABILITY
CSS AND USABILITY
In a Microsoft Word document, write a short essay on the impact that CSS has on usability and efficiency. Castro and Hyslop mentioned that CSS brings about “flexible, powerful, and efficient” design, but they do not go into detail fully explaining their reasoning for these CSS design traits. In your opinion, which e-book in the Capella online library offers the best explanations of how CSS is “flexible, powerful, and efficient”? What explanations are given that can help designers more fully understand the capabilities and improvements brought about with the advent of CSS coding? Cite specific examples that illustrate the points that you feel are important.
7 CSS Building Blocks
In This Chapter
Constructing a Style Rule
181
Adding Comments to Style Rules
182
The Cascade: When Rules Collide
184
A Property’s Value
188
Whereas HTML defines your content’s meaning and gives your Web pages their basic structure, CSS (Cascading Style Sheets) defines the appearance.
A style sheet is simply a text file that contains one or more rules that determine—through properties and values—how certain elements in your Web page should be displayed. There are CSS properties for controlling basic formatting such as font size and color, layout properties such as positioning and float, and print controls such as deciding where page breaks should appear when visitors print a page. CSS also has a number of dynamic properties that allow items to appear and disappear and that are useful for creating drop-down lists and other interactive components.
CSS2 is the version that is best supported across browsers both new and old, so this book will cover it extensively. CSS3, which is still evolving as a specification, builds upon CSS2 to provide features that designers and developers have long been clamoring for. The great news is that modern browsers have implemented several CSS3 components already, so you’re able to start using them today. You’ll learn some of the most useful features with the best support.
The wonderful thing about CSS is that it can be created outside of a Web page and then applied to all the pages on your site at once. It is flexible, powerful, and efficient and can save you lots of time and bandwidth.
To get the full benefit of CSS, your Web pages must be marked up well and consistently according to the recommendations in the HTML chapters.
Constructing a Style Rule
Each style rule in a style sheet has two main parts: the selector, which determines which elements are affected, and the declaration block, made up of one or more property/value pairs (each constitutes a declaration), which specifies just what should be done ( and ).
A style rule is made up of a selector (which indicates which elements will be formatted) and a declaration block (which describes the formatting that should be executed). Each declaration within the block is a property/value pair separated by a colon and ending with a semicolon. A left curly brace begins a declaration block, and a right curly brace ends it.
The order of declarations doesn’t matter unless the same property is defined twice.
In this example, color: red could be before background: yellow and have the same effect. Note the extra spacing and indenting (optional, but recommended) to keep everything readable.
To construct a style rule:
1. Type selector, where selector identifies the element or elements you wish to format. You’ll learn how to create all sorts of selectors in Chapter 9.
2. Type { (an opening curly bracket) to begin the declaration block.
3. Type property:value;, where property is the name of the CSS property that describes the sort of formatting you’d like to apply and value is one of a list of allowable options for that property. Descriptions of CSS properties and values begin in Chapter 8.
4. Repeat step 3 as needed. Typically, you’ll enter each property: value (a declaration) on its own line.
5. Type } to complete the declaration block and the style rule.
You may add extra spaces, tabs, or returns in a style rule to keep the style sheet readable . The format in the example is perhaps the most common among coders.
Although each property/value pair should be separated from the next by a semicolon, you may omit the semicolon that follows the last pair in the list. But I recommend you always include it, since it’s a best practice to do so.
Missing (or duplicate) semicolons can cause the browser to ignore the style rule.
Adding Comments to Style Rules
It’s a good idea to add comments to your CSS to note the primary sections of your style sheets or simply to explain something about a particular rule or declaration. Comments help not only you but also others who are viewing your code. For your own sake, you’ll be happy that you left yourself comments if you revisit the code some months after having initially worked on it.
To add comments to style rules:
1. In your style sheet, type /* to begin your comment.
2. Type the comment.
3. Type */ to signal the end of the comment.
Comments may include returns and thus span several lines .
You may not put comments inside other comments. In other words, comments may not include */.
You may start comments on their own line , inside a declaration block , or after a rule .
Comments can be long or short, though they tend to be short. Use them as you see fit to describe the purpose of a style rule or a group of related rules. Comments go a long way toward making your style sheet easier to maintain.
You can also insert comments within the declaration block or after a rule.
Comments are extremely helpful as an organizational tool. Style sheets can quickly get long, so organizing them is critical to making your CSS easy to evolve and maintain. It’s common practice to group related rules together and precede each with a descriptive comment .
However you format your comments , I recommend you decide on a convention and use it consistently, especially if you’re working with a team.
You can put comments around or within style rules to hide them from the browser . This is a good way to test a style sheet without permanently removing the commented portion until (and if) you are ready to do so. It’s a helpful debugging tool; comment out something you think might be causing a problem, refresh the page in the browser, and see if the problem is fixed.
Although these examples are heavy on comments for demonstration purposes, don’t feel the need to comment everything. You’ll probably find your style sheets harder to read if they have too many comments. You’ll probably find that a good mix entails organizational comments coupled with descriptive ones as needed. Find the balance that works for you and the others on your team.
Comments make your life easier when managing style sheets. Simply comment primary sections of rules within your style sheets to keep them organized. I find that using a format like the one here (with all caps and an underline) makes it clear where each major grouping begins. This treatment clearly distinguishes them from other comments, such as the ones in and .
You can “comment out” a declaration that you don’t want to affect the page. Here, all images will get a four-pixel solid red border but not a right margin treatment, because margin-right: 12px; is inside a comment. A comment can go around an entire rule too, as long as there aren’t any comments inside the comment.
The Cascade: When Rules Collide
Styles come from many sources. As you learned in Chapter 1, every browser has its own default styles. But you can apply your own styles to override or complement those in three ways: You can load one or more from an external file (the recommended method) , insert them at the top of an HTML document, or apply them to a specific HTML element right in the code (though this is to be avoided whenever possible). See the next chapter for specifics about each method.
Also, some browsers let your visitors create and apply their own style sheets to any pages they visit—including yours. Finally, some styles are passed down from parent element to child.
This is the style sheet for the HTML document in . Don’t worry too much about the details right now, but do notice that there is a rule for p elements, but not for h1, em, or small elements.
The em and small elements are contained within the p element and thus are children of p. However, the h1 is not, so it isn’t blue like the other text .
What happens, you might ask, when there is more than one style rule that applies to a given element? CSS uses the principle of the cascade to take into account such important characteristics as inheritance, specificity, and location in order to determine which of a group of conflicting rules should win out.
Let’s start with inheritance. Many CSS properties not only affect the elements defined by the selector but are also inherited by the descendants of those elements ( through ). For example, suppose you make all your h1 elements blue with a red border. It so happens that the color property is inherited, but the border property is not. Thus, any elements contained within the h1 elements will also be blue, but they will not have their own red border. You’ll learn which properties are inherited in the individual section describing each property (and in Appendix B on the book’s site). You can also use a value of inherit with most properties to force inheritance (see the next section, “A Property’s Value”).
In the absence of a rule specified explicitly for the em and small elements in , they inherit their font, weight, and color from their parent, the p element. The italics come from the browser’s default styling of em. The size of the legal notice marked up with small (that is, legal “fine print”) is reduced for the same reason. The h1 does not have its own style and is not a child of p, so it displays entirely in accordance with the browser default.
While inheritance determines what happens if no style rule is applied to an element, specificity is the key when more than one rule is applied ( through ). The law of specificity states that the more specific the selector, the stronger the rule. Makes sense, right? So if one rule states that all h1 elements should be blue but a second rule states that all h1 elements with a class of spanish be red, the second rule will override the first for all those h1 elements whose class is spanish, because h1.spanish is a more specific selector than simply h1.
Note that id attributes are considered the most specific (since they must be unique in a document), while the presence of a class attribute makes a selector more specific than a simple selector that has none. Indeed, a selector with more than one class is more specific than a selector with only one. Selectors with only element names come next on the specificity scale; inherited rules are considered to be the most general of all and are overruled by any other rule.
For the exact rules of calculating specificity, see Section 6.4.3 of the CSS specifications (www.w3.org/TR/CSS21/cascade.html#specificity).
In this example, there are four rules of varying specificity. The first affects any pelement, the second affects only those p elements with a class equal to group, and the third and fourth affect only the single p element with an id equal to last.
Three paragraphs: one generic one, one with just a class, and one with a class and an id.
Sometimes, specificity is not enough to determine a winner among competing rules. In that case, the location of the rule breaks the tie: Rules that appear later have more weight ( through ). For example, rules that are applied inline right in the HTML element (again, not recommended) are considered to appear after (and thus have more weight than) equally specific rules applied in either an external style sheet or one embedded at the top of the HTML document. For details, consult “The Importance of Location” in Chapter 8.
If that isn’t enough, you can override the whole system by declaring that a particular rule should be more important than the others by adding !important at the end of the rule. (This also isn’t recommended except in uncommon cases.)
In summary, in the absence of a rule, many styles are passed down from parent element to child. With two competing rules, the more specific the rule, the more weight or importance it has—regardless of its location. With two rules of equal specificity, the one that appears later in the style sheet wins.
If any of this sounds confusing, don’t worry about it right now. Once you start playing with CSS and different selectors, I think you’ll find that the cascade operates just as you’d expect it to in most cases.
Since the third and fourth rules have the same specificity, their position becomes a factor—and thus the fourth rule wins out since it appears last.
A Property’s Value
Each CSS property has different rules about what values it can accept. Some properties accept only one of a list of predefined values. Others accept numbers, integers, relative values, percentages, URLs, or colors. Some can accept more than one type of value. The acceptable values for each property are listed in the section describing that property (mostly in Chapters 10 and 11), but you’ll learn the basic systems here.
Inherit
You can use the inherit value for any property when you want to explicitly specify that the value for that property be the same as that of the element’s parent.
Predefined values
Most CSS properties have a few predefined values that can be used. For example, the float property can be set to left, right, or none. In contrast with HTML, you don’t need to—and indeed must not—enclose predefined values in quotation marks .
Lengths and percentages
Many CSS properties take a length for their value. All length values must contain a quantity and a unit, with no spaces between them. For example, 3em or 10px . The only exception is 0, which may be used with or without units.
Many CSS properties will only accept values from a predefined list. Type them exactly and do not enclose them in quotation marks.
Lengths must always explicitly state the unit. There should be no space between the unit and the measurement.
There are length types that are relative to other values. An em is roughly equal to the element’s font size, so 2em would mean “twice the font size.” (When the em is used to set the element’s font-size property itself, its value is derived from the font size of the element’s parent.) The ex should be equal to the font’s x-height, that is, the height of a letter x in the font, but its support varies, so you aren’t likely to use it.
Pixels (px) are not relative to other style rules. For instance, values in px aren’t affected by the font-size setting, as ems are. A pixel on one type of device isn’t necessarily the same size as on another. (See Peter-Paul Koch’s detailed description at www.quirksmode.org/blog/archives/2010/04/a_pixel_is_not.html.)
There are also the largely self-explanatory absolute units, such as points (pt), which is a unit that should be reserved for print style sheets. (There are others, but there’s little point in mentioning them, because they aren’t used in practice.) In general, you should only use absolute lengths when the size of the output is known (as with pt and the printed page).
Percentage values—65%, for example—work much like ems, in that they are relative to some other value .
Of all these, you will use ems, pixels, and percentages the most.
Bare numbers
A very few CSS properties accept a value in the form of a number without a unit, like 3. The most common are line-height and z-index (see “Setting the Line Height” in Chapter 10 and “Positioning Elements in 3D” in Chapter 11, respectively). (The others are mostly for print and aural style sheets and are not yet well supported.)
Percentages are generally relative to the parent element. So in this example, the font would be set to 80 percent of the parent’s font size.
Don’t confuse numbers and integers with length. A number or integer has no unit (like px). In this case, the value shown here is a factor that will be multiplied by the font size to get the line height.
URLs
Some CSS properties allow you to specify the URL of another file, particularly images. In that case, use url(file.ext), where file.ext is the path and file name of the desired asset . Note that the specifications state that relative URLs should be relative to the style sheet and not to the HTML document.
While you may use quotation marks around the file name, they’re not required. On the other hand, there should be no space between the word url and the opening parentheses. White space between the parenthesis and the address is allowed but not required (or customary).
For more information on writing the URLs themselves, consult “URLs” in Chapter 1.
CSS colors
You can specify colors for CSS properties in several ways. First, and easiest, the value can be one of the predefined color keywords. CSS3 specifies a basic list of 16 names and adds 131 more to align with the 147 SVG 1.0 color keyword names. The full list is available at www.w3.org/TR/css3-color/#svg-color.
Of course, no one remembers any of those color names outside of the obvious ones anyway. Also, you typically grab the colors from tools like Adobe Photoshop, and they don’t use the CSS color name. So in practice, it’s more common to define your CSS colors with the hexadecimal format (the most common by far) or the RGB format. As you will learn, you can also specify a color with the HSL format, and the level of color transparency with RGBA and HSLA, all of which are new in CSS3.
URLs in CSS properties do not need to be enclosed in quotation marks.
The most common way in CSS to define a color is by specifying, with hexadecimal numbers, the amounts of red, green, and blue that it contains.
RGB
You can construct your own color by specifying its amount of red, green, and blue (hence the name RGB). You can give the values of each of these contributing colors as a number from 0–255, a percentage, or a hexadecimal representation of the number. For example, if you wanted to create a dark purple, you might use 89 red, no green, and 127 blue. That color could be written rgb(89, 0, 127), as shown in .
Alternatively, you could represent each value as a percentage, though it is far less common to do so, likely because image editors like Photoshop tend to provide you numerical RGB values. But if you do want to use percentages, you could write the same color as rgb(35%, 0%, 50%), since 89 is 35% of 255 and 127 is 50% of 255.
Hexadecimal
I’ve saved the most common method for last . Convert those numerical values to hexadecimals, join them together, and prefix the value with a #, as in #59007F. (59 is the hexadecimal equivalent of 89, 00 is the hexadecimal equivalent of 0, and 7F is the hex equivalent of 127.) You can also write 7F as 7f (my preference, but plenty of developers and designers go the other way).
When a hexadecimal color is composed of three pairs of repeating digits, as in #ff3344, you may abbreviate the color to #f34. In fact, it’s a best practice to do so, since there’s no reason to make your code longer than it needs to be.
If you’re scratching your head about hexadecimals, don’t fret. Just as Photoshop and the like include tools for choosing colors and displaying their RGB values, so, too, do they for hex.
Another way to express color in CSS is with RGB numeric values from 0–255. Define red first, followed by green, and then blue.
The most common way in CSS to define a color is by specifying, with hexadecimal numbers, the amounts of red, green, and blue that it contains.
More color options in CSS3: RGBA, HSLA, and HSL
CSS3 introduces another way to specify colors—HSL—and the ability to set alpha transparency via RGBA and HSLA. (You can’t indicate alpha transparency with hexadecimal notation.)
RGBA
RGBA is the same as RGB except the A stands for alpha transparency. You can specify the amount of transparency with a decimal from 0 to 1 after the red, green, and blue values. So, the syntax is the following:
property: rgba(red, green, blue, alpha transparency);
The closer to 0 the alpha setting, the more transparent the color becomes. If it is 0, it’s completely transparent, as if you hadn’t set a color at all. Similarly, 1 is completely opaque, meaning it’s not transparent at all. Here are some example declarations to illustrate the point:
/* no transparency, so the same as rgb(89, 0, 127); */
background: rgba(89,0,127,1);
/* completely transparent */
background: rgba(89,0,127,0);
/* 25% transparent */
background: rgba(89,0,127,0.75);
/* 60% transparent */
background: rgba(89,0,127,0.4);
This simple style sheet applies a repeating background image and default text color to the whole page, with slightly different background treatments for the h1–h3 headings. Modern browsers display the result shown in . As you’ll learn later, versions of Internet Explorer prior to IE9 don’t support RGBA, so they ignore the declarations on the h1 and h2.
Of course, in order to make those work, you’ll need to include them in one or more rules . As shown, it’s common to leverage alpha transparency on the background color of an element, because alpha transparency allows whatever is behind the element—an image, other colors, text, and so on—to peek through and blend with it . To be clear, though, you can also set alpha transparency on other color-based properties, such as color, border, border-color, box-shadow, and text-shadow, with varying degrees of browser support (you’re in the clear with modern browsers).
As you can see, the RGB color values are the same in the code, but the colors themselves appear different in the browser because of their different levels of transparency .
HSL and HSLA
HSL and HSLA are the other new additions in CSS3. The latter is the alternative to RGBA for setting alpha transparency on a color. You specify the alpha the same way you do with RGBA. You’ll see that in a second, but first take a look at how HSL works.
HSL stands for hue, saturation, and lightness, where hue is a number from 0–360, and both saturation and lightness are percentages from 0 to 100 . In CSS, the syntax is:
property: hsl(hue, saturation, lightness);
In this gaudy but effective example, you can see the page background image peeking through the background of the first two headings but not of the last one. The background color for all three is the same, but they look like three different shades of purple because of their different alpha transparency settings. (The text is yellow because the color property set on the body element cascades down to all text on a page unless it is overridden by a style rule for another element.)
The breakdown of the HSL formatting.
And, as you guessed it, the HSLA format is this:
property:hsla(hue, saturation, lightness, alpha transparency);
For instance, here’s the same purple from the RGBA and RGB example expressed in HSLA and HSL instead:
/* 25% transparent */
h1 {
background: hsla(282,100%, 25%,0.75);
}
/* 60% transparent */
h2 {
background: hsla(282,100%, 25%,0.4);
}
/* Solid background (not transparent) */
h3 {
background: hsl(282,100%,25%);
}
The result in modern browsers is the same as before .
Think of the hue value as a degree on a circle, with 0 and 360 meeting at the top. This means that both 0 and 360 are the same color—red. (Don’t confuse HSL with HSB or HSV. They are similar, but not the same.)
How to Think in HSL
Learning HSL’s logic takes some time, but once you get a feel for it you may find it easier to work with than other formats. In the “Why?” section of his HSL Color Picker site (http://hslpicker.com), Brandon Mathis provides a nice explanation of HSL. He writes:
“Pick a hue from 0 to 360, and with saturation at 100 and luminosity at 50 you’ll have the purest form of that color. Reduce the saturation and you move toward gray. Increasing the luminosity moves you toward white, decreasing it moves you toward black.”
For example, here are some core colors as you move around the circle:
¦ Red is hsl(0,100%,50%);
¦ Yellow is hsl(60,100%,50%);
¦ Green is hsl(120,100%,50%);
¦ Cyan is hsl(180,100%,50%);
¦ Blue is hsl(240,100%,50%);
¦ Magenta is hsl(300,100%,50%);
Not all image editors specify HSL out of the box (you can get a plugin for Photoshop). However, Mathis’s HSL Color Picker is a great, free online tool that allows you to pick a color and get its HSL, hex, and RGB values, or you can type in values for any of the formats to see the color change. Another such tool is located at www.workwithcolor.com/hsl-color-picker-01.htm. It shows the colors on a circle, which may help you get a better feel for HSL. (In contrast, HSL Color Picker shows them on a line.) You can find other color tools by searching online.
Modern browsers render the RGBA declaration because it comes after the default hexadecimal background setting (which it also understands, so the order is important). Meanwhile, versions of IE prior to IE9 ignore the RGBA setting because they don’t understand it, so the hex background stands. You could use RGB (but not RGBA) instead of hex in the first line, but as noted, hex is the most common way to denote non-transparent colors.
Look away before your eyes burn! This verbose mixture of code sandwiches declarations for pre-IE9 versions (highlighted) around the standard RGBA notation (not highlighted). As usual, the older versions of IE ignore what they don’t understand. Similarly, modern browsers ignore the -ms-filter, filter, and zoom values since they don’t understand them. The order of the declarations is essential to making this technique work.
RGBA, HSL, and HSLA in Internet Explorer
Sadly, as is often the case with the latest developments in the standards world, no version of Internet Explorer prior to IE9 supports these features. Instead, they ignore any declaration they don’t understand.
There is a workaround for pre-IE9 versions regarding RGBA and HSLA. But in terms of HSL, you’ll want to stick with hexadecimal (or RGB) to specify your colors.
For RGBA and HSLA in pre-IE9 versions, you’re left with three options (but only one at a time):
¦ Do nothing and let your page look fairly different in these versions.
¦ Provide a fallback color declaration for them, meaning they will display a solid color not a transparent one .
¦ Mimic the alpha transparency by including declarations specifically for them, most of which are proprietary IE CSS; modern browsers will still use the standard CSS, though .
This last option uses Internet Explorer’s Gradient filter in conjunction with proprietary code that no other browser understands. That means modern browsers will ignore it and use the standard notation instead, which in this case is background: rgba(89,0,127,0.75); (it overrides the previous background value). Be aware that the declarations must be in the order shown for the transparency effect to apply properly across browsers both modern and otherwise .
I won’t bother to explain how IE’s Gradient filter syntax works, since it’s so convoluted you’ll probably never write it by hand. I don’t. Instead, another free online tool rescues you (you’ll see more and more of these as you learn more about CSS3).
This one comes in the form of Michael Bester’s RGBa & HSLa CSS Generator for Internet Explorer (http://kimili.com/journal/rgba-hsla-css-generator-for-internet-explorer). As he explains, you enter an RGBA or HSLA declaration, and the tool creates the equivalent CSS for pre-IE9. Then you copy and paste it into your style sheet. One important note: The code that the tool generates does not include the standard RGBA or HSLA declaration for modern browsers. So, you’ll have to add that yourself directly afterbackground: transparent, just as it is shown in the example . Alternatively (and often preferably), as Michael notes, you can place the pre-IE9 CSS in its own style sheet and load it inside what are known as conditional comments. (See http://reference.sitepoint.com/css/conditionalcomments for more information.)
Internet Explorer’s filters, such as the Gradient filter , can affect the browser’s performance because they require extra processing power. You likely won’t have any noticeable issues if a filter is applied to a reasonable number of elements on a page, but a delay can sometimes be seen beyond that. It can depend on what’s on the rest of your page, too. So be mindful of this as you’re building a page, and if you’re seeing a slowdown in IE you may want to turn off the filter to see if that’s the issue. IE filters can sometimes have other unexpected side effects, like adversely affecting the quality of text rendering. To clarify, these won’t affect other browsers since they don’t understand filters.
(Castro 179)
Castro, Elizabeth, Bruce Hyslop. HTML5 & CSS3 Visual QuickStart Guide, Vitalsource for Capella University, 7th Edition. Pearson Learning Solutions.<vbk:9781269204576#outline(11)>.
ORDER THIS ESSAY HERE NOW AND GET A DISCOUNT !!!
You can place an order similar to this with us. You are assured of an authentic custom paper delivered within the given deadline besides our 24/7 customer support all through.
Latest completed orders:
# | topic title | discipline | academic level | pages | delivered |
---|---|---|---|---|---|
6
|
Writer's choice
|
Business
|
University
|
2
|
1 hour 32 min
|
7
|
Wise Approach to
|
Philosophy
|
College
|
2
|
2 hours 19 min
|
8
|
1980's and 1990
|
History
|
College
|
3
|
2 hours 20 min
|
9
|
pick the best topic
|
Finance
|
School
|
2
|
2 hours 27 min
|
10
|
finance for leisure
|
Finance
|
University
|
12
|
2 hours 36 min
|