Cascading Style Sheets: The Definitive Guide, 2nd. Edition. By Eric Meyer In the case of (X)HTML and CSS, many changes are afoot even as these words (U+E; for direction: rtl) at the start of the element and a PDF (U+C) at the. Author: Eric A. Meyer CSS Cookbook, 3rd Edition (Animal Guide) · Read more The Definitive Guide to MySQL 5, Third Edition (Definitive Guide). Read more. O Reilly Media, p. ISBN CSS has come a long way since its early days, when it provided a bit of help to a mostly HTML-oriented .

Css The Definitive Guide By Eric Meyer Pdf

Language:English, Portuguese, French
Genre:Health & Fitness
Published (Last):17.05.2015
ePub File Size:18.48 MB
PDF File Size:16.20 MB
Distribution:Free* [*Sign up for free]
Uploaded by: REID

ample: “CSS Pocket Reference by Eric A. Meyer (O'Reilly). Copyright O' Reilly .. explored in detail in Chapter 7 of CSS: The Definitive Guide, third edition. Cascading Style Sheets: The Definitive Guide by Eric A. Meyer ISBN First edition, published May (See the catalog page for this book.). CSS: The Definitive Guide: Visual Presentation for the Web (4th ed.) by Eric A. Meyer. Read online, or download in DRM-free EPUB or DRM-free PDF format.

This value describes the type of data that is to be loaded using the LINK tag. That way, the web browser knows that the style sheet is a CSS style sheet, a fact that will determine how the browser deals with the data it imports. After all, there may be other style languages in the future, so it will be important to say which language you're using. Next we find the HREF attribute.

The value of this attribute is the URL of your style sheet. This URL can be either absolute or relative, depending on what works for you. In our example, of course, the URL is relative. It could as easily have been something like http: This attribute is not often used, but it could become important in the future.

It becomes important when there is more than one LINK tag -- and there can be more than one. Thus, if you wanted to link in two style sheets with the names basic. We'll see exactly how the sheets are combined in the next chapter, but for now, let's just accept that they're combined. For example: Combining linked style sheets It's also possible to define alternate style sheets.

These are marked with a REL of alternate stylesheet and come into play only if they're selected by the reader. Alternate style sheets Unfortunately, as of this writing, browsers don't make it very easy to select alternate style sheets, assuming that they can do so at all.

Should a browser be able to use alternate style sheets, it will use the values of the TITLE attributes to generate a list of style alternatives. So you could write the following: Figure shows one way in which this selection mechanism might be accomplished.

A browser offering alternate style sheet selection WARNING Alternate styles sheets are only supported by one browser as of this writing -- Internet Explorer for Macintosh -- and that only with a JavaScript widget, which does not ship with the browser. None of the three major browsers natively supports the selection of alternate style sheets shown in Figure As of this writing, the one browser that does recognize alternate style sheets Internet Explorer for Macintosh will not apply the styles from any LINK element with a REL of alternate stylesheet unless that style sheet is selected by the user.

The styles between the opening and closing STYLE tags are referred to as the document style sheet or the embedded style sheet , since this style sheet is embedded within the document. It contains styles that apply to the document, but it can also contain multiple links to external style sheets using the import directive.

First, we have something very similar to LINK: The only real difference is in the actual syntax of the command and its placement. It must be placed there, before the other CSS rules, or else it won't work at all. Unlike LINK, however, the style sheets of every import directive will always be loaded and used. So given the following, all three external style sheets will be loaded, and all of their style rules will be used in the display of this document: This can actually be used to one's advantage in "hiding" styles from these browsers.

What they mean doesn't actually matter for this discussion, although you can probably guess that they set H1 elements to be maroon and BODY elements to have a yellow background. Styles such as these comprise the bulk of any embedded style sheet -- style rules both simple and complex, short and long. The Definitive Guide For those of you concerned about making your documents accessible to older browsers, there is an important warning to be made.

You're probably aware that browsers ignore tags they don't recognize; for example, if a web page contains a BLOOPER tag, browsers will completely ignore the tag because it isn't a tag they recognize.

The same will be true with style sheets. However, the declarations within those tags will not be ignored, because they will appear to be ordinary text so far as the browser is concerned.

So your style declarations will appear at the top of your page! Of course, the browser should ignore the text because it isn't part of the BODY element, but this is never the case. This problem is illustrated in Figure Older browsers will literally display your style sheets In order to combat this problem, it is recommended that you enclose your declarations in a comment tag.

Meanwhile, those browsers that understand CSS will still be able to read the style sheet. The problems ranged from mangled display to browser crashes.

Follow the author

This happened with only a very few browser versions, and it's safe to say that very few of these browsers are still being operated. Be aware that there are some people out there using these particular browsers, and they may well have major problems viewing your page if you use these comment tags. CSS Comments file: So, for example, this would not be correct: If you wish to place comments on the same line as markup, then you need to be careful about how you place them.

For example, this is the correct way to do it: The rest of the rules, as part of the comment, are ignored by the browser's rendering engine. In fact, it looks very much like the declarations found in the STYLE container, except here the curly brackets are replaced by double quotation marks.

No other part of the document will be affected by this declaration.

Limitations of CSS 1. Summary In order to facilitate a return to structural HTML, something was needed to permit authors to specify how a document should be displayed. CSS fills that need very nicely, and far better than the various presentational HTML elements ever did or probably could have done. For the first time in years, there is hope that web pages can become more structural, not less, and at the same time the promise that they can have a more sophisticated look than ever before.

This allows authors to simplify document appearance management and maximize their effectiveness, thereby making their jobs a little easier. The further benefits of improving accessibility and positioning documents for a switch to an XML world make CSS a compelling technology. This is not such a major tragedy, annoying though it might be, since the LINK element will still let you bring external style sheets into play.

In order to fully understand how CSS can do all of this, authors need a firm grasp of how CSS handles document structure, how one writes rules that behave as expected, and most of all, what the "Cascading" part of the name really means. The Definitive Guide Chapter 2. Selectors and Structure Contents: How many times have you slaved over a new design, guided it through 17 committees and 4 major revisions, and finally come up with something that everyone seems to like, when a vice-presidential voice suddenly says, "I'm concerned about the shade of green we're using for our headings.

Could we see some versions of the site using a few lighter shades, and maybe a couple of darker shades as well? Meanwhile, all the other administrators have started to think of their own ways to nitpick the design to death.

Maybe the headings should be dark blue instead of green, or perhaps the sidebar's background is the wrong color, or maybe the company logo ought to be used for list-item bullets instead of those little black dots that everyone else uses. So, at the next design meeting, after everyone's agreed that such-and-so shade of green is a good one, all these new revision ideas start popping up, and all the administrators are nodding gravely and saying, why yes, maybe we should see a design which uses shades of red, not green.

The downward spiral has begun. Even if you're lucky enough to work in a place where you don't have to tolerate such nonsense, you probably pose similar questions to yourself while you're working on your design. Does the particular blue you're using for your sidebar's background contrast enough with your yellow links?

Would the headings all look better if they were red instead of green? What if the paragraphs were in one font and the headings in another? That can take forever if you have a lot of documents, or a really complicated design, and if you suddenly head in a different direction, you can spend almost as much time cleaning up the residue of your old assumptions as you do on actual creative design work.

The Definitive Guide Style sheets offer an easy, convenient, and powerful way to break out of this morass. One of the primary advantages that CSS offers to designers is its ability to easily apply a set of styles to all elements of the same type.

This may not sound like much, but consider: Don't like the blue you're using? Change that one line of code, and the headings can all be purple, or yellow, or maroon, or any other color you desire. Design time is reduced by cutting out the grunt work, allowing you to focus on being creative.

The next time you're in a meeting and someone wants to see headings with a different shade of green, just edit your page's styles and hit reload. The results of that change are right there for everyone to see, and it only took seconds to accomplish, instead of requiring another meeting. Of course, CSS can't solve all your problems -- you can't use it to change the color of your GIFs, for example -- but it can make your life a lot easier than it has been.

It does this with selectors and structure, the first of which is used to make changes that take advantage of the second. Basic Rules Central to CSS is the ability to apply certain rules to the same types of elements in a document, which can drastically reduce the amount of work an author has to undertake.

For example, let's say that you wish to make the text of all H2 elements appear gray. If you later decide that you want to change all the H2s to be green instead of gray, then the task becomes even worse because you have to find all of those H2s and change the value of each and every FONT tag to be gray. In CSS, you can avoid all that hassle, and still get the effects you want in a way that makes them easy to change.

In the document's style sheet, you need only define the following to get the same result: This single rule is enough to cause all H2 elements to be colored gray.

If you want to change this to another color, then the alteration of this single rule will affect all H2s in the document: Rule Structure In order to understand this in more detail, let's break down the structure of a rule.

Each rule has two parts, the selector and the declaration. At a finer level, each declaration is actually a combination of properties and values. Every style sheet is made up of a series of rules, but rules do not always appear in style sheets. First, however, let's break down our example rule into its various parts, as shown in Figure The structure of a rule As you can see, on the left side of the rule, we find the selector.

A selector is simply the part of the rule that selects the parts of the document to which the styles should be applied. In this case, H1 elements are selected. If the selector shown here were P, then all P paragraph elements would be selected, and H1 elements would not.

On the right side of the rule, we have the declaration. This is a combination of a CSS property and a value of that property. In Figure , the declaration says that this rule will cause parts of the document to have a color of purple.

The parts that will be purple are those shown in the selector in this case, all H1 elements. In XML, this could be anything. Simple styling of a document This ability to apply styles to elements is obviously very powerful. It also makes it simple to shift styles from one type of element to another. Let's say we have a page design where H2 elements are gray: You eventually decide that you actually want your paragraph text to be gray, not your H2 text.

No problem! All you have to do is change the selector from H2 to P, and you'll have shifted the style from H2 elements to P paragraph elements: Let's examine the right side, where the declaration lives, before we return to selectors for some added features. Declarations A declaration is always formatted as a property followed by a colon, and then a value. Finally, the declaration is terminated with a semicolon ;.

The value can be a single keyword, or a space-separated list of one or more keywords that are permitted for that property. If an incorrect property is used in a declaration, the entire declaration is ignored. Thus, the following declaration would be ignored because, while the value is correct, the property is not: This is less likely with many browsers, however, because most of them are fairly tolerant of mistakes in CSS: While this sounds like a polite thing to do, it unfortunately makes it much easier for authors to pick up bad authoring habits.

In an instance where you can use more than one keyword for a property's value, then the keywords are usually separated by spaces. Not every property can accept multiple keywords, but many can: Should you wish to define paragraphs to use medium-size Helvetica for their text, then the rule would be as follows: The space allows the user agent to distinguish between the two keywords and apply them correctly.

The final semicolon indicates that the rule has been concluded. The reason we refer to these space-separated words as keywords is that, taken together, they all form the value of the property in question. For instance, consider the following fictional rule: The Definitive Guide There is no such property as rainbow, of course, and many of the colors used aren't valid either, but it will be useful for illustrative purposes.

What we have is a case where the value of rainbow is red orange yellow green blue indigo violet. The seven keywords add up to a single, unique value. We can redefine the value for rainbow as follows: Despite their seeming similarity, these two values are as unique and different as the values zero and one.

There are a few instances where keywords are separated by something other than a space. Here's an example: This is the only place the slash can appear in the font declaration. All of the other keywords allowed for font are separated by spaces.

That's basically all there is to simple declarations, just as there wasn't much to say about simple selectors. We aren't limited to such simple operations, though. In fact, let's find out just how powerful CSS can be. Summary 2. The Definitive Guide 2.

Grouping So far, things have been pretty simple -- as long as you only want to apply a single style to a single selector, that is. No doubt you'll want to go further than that, though: Grouping Selectors Let's say you have a document in which both H2 elements and paragraphs should have gray text. The easiest way to accomplish this is to use the following.

The comma tells the browser that there are two different selectors involved in the rule. Leaving out the comma would give the rule a completely different meaning, which we'll explore later, in Section 2. It is possible to group any number of selectors together. If you wanted to make sure that every element in your document had a color of gray, you might use the following rule: The following two alternatives will have exactly the same result, but it's pretty obvious which one is easier to type: The Definitive Guide Grouping can make for some interesting choices.

For example, all of the style sheets in the following example are equivalent -- each merely shows a different way of grouping both selectors and declarations -- and any of them will yield the result shown in Figure The result of equivalent style sheets 2. Grouping Declarations Since it is possible to group selectors together into a single rule, it makes sense that you can also group declarations.

The importance of using the semicolon to end each declaration becomes even more clear once we consider the fact that there can be more than one declaration in a given rule. This helps keep style sheets compact, organized, and easier to read. For example, assume that you want all H1 elements to have purple text on an aqua background, and use point Helvetica for the font. You could write your styles like this: Instead, you can group your declarations together: Grouping declarations When you group declarations together into a single rule, the semicolons are used to separate the declarations.

This is especially important since whitespace is ignored in style sheets, so the user agent UA must rely on correct syntax to parse a style sheet. There is nothing wrong with formatting your styles like this: A user agent in this case might incorrectly render H1s as purple text without an aqua background. However, since only a single keyword is permitted for color, the user agent should ignore the entire declaration as being invalid, meaning that you won't even get purple H1s; instead, they'll be the default color usually black with no background at all.

The declaration font: TIP While it is not technically necessary to follow the last declaration of a rule with a semicolon, it is generally good practice to do so. In the first place, it will keep you in the habit of terminating your declarations with semicolons, the lack of which is one of the most common ways to cause errors in rendering. Second, if you decide to add another declaration to a rule, you don't have to worry about forgetting to insert an extra semicolon.

Finally, some older browsers such as Internet Explorer 3. My advice is to avoid all these file: The Definitive Guide problems and always follow a declaration with a semicolon. As with selector grouping, declaration grouping is a convenient way to keep your style sheets short, expressive, and easy to maintain. The following example shows two ways of assigning six different styles to H1 elements. The first uses a separate rule for each property, and the second uses declaration grouping to apply all six styles to a single selector.

The primary advantage of the second method is that it allows you to change the element to which the styles are applied by editing one selector, instead of six. Either way, though, you'll get the result depicted in Figure With or without grouping, the results are the same 2. Grouping Everything All right, so we can group selectors, and we can group declarations.

By combining both kinds of grouping in single rules, it is possible to define very complex styles using only a few statements. Let's assume that we wish to assign some complex styles to all of the headings in a document.

The result of this rule is shown in Figure This approach is obviously preferable to the alternative, which would start out something like this and would continue for quite a while: The problems you'd face in trying to edit your styles would be almost as bad as if you were back to using FONT tags everywhere! Grouping both selectors and rules It's largely thanks to grouping that CSS files look the way they do.

Here is a typical style sheet, with Figure showing the result: Helvetica, sans-serif; color: The result of a typical style sheet For all the power and complexity we've introduced, there is even more with regard to selectors.

It's possible to add more expression to selectors, and in so doing, apply styles in a way that cuts across elements in favor of types of information. Of course, to get something so powerful, you'll have to do a little work in return, but it's well worth it. Selectors and Structure 2. Class and ID Selectors So far, we've been grouping selectors and declarations together in a variety of ways, but for all that, the selectors we've been using are very simple ones.

They only refer to document elements, and while that's often just what you need, there are times when something a little more specialized is required. In addition to raw document elements, there are two other kinds of selectors: These selectors can be used on their own or in conjunction with element selectors. However, they only work if you've marked up your document appropriately, so using them generally involves a little forethought and planning.

Of course, you probably want to know why you should go to the effort. What will it get you? Let's consider a document about the handling of plutonium, in which there are various warnings on things to do or not do while working with such a dangerous substance. You want to make each warning use boldface text so that it will stand out from everything else. However, you don't know what kind of elements these warnings will be. Sometimes there will be a whole paragraph of warning text, but other times there might be a single warning list item out of a lengthy list, or just a given section of a paragraph.

In any case, you can't define a rule using simple selectors of any kind. If you did this: You need a way to only select the paragraphs that are warnings, or more precisely, a way to select only those elements that are warnings.

Another scenario involves ways of styling different kinds of links. You might wish to set a different color for links that point to pages not found on your site. Once more, you can't just write: So what we need is a way to apply styles to parts of the document that have been marked in a certain way, independent of the elements involved -- and that's just what CSS gives us.

Class Selectors The most common way to apply styles without worrying about the elements involved is to use class selectors. Before you can use them, however, you need to modify your actual document markup so that the class selectors will work. Since we aren't going to be selecting according to the elements, we need something else to grab onto. The Definitive Guide attribute: This can be accomplished by keeping the various masses separate In the previous code, we've assigned a class value of warning to two elements: All we need now is a way to apply styles to these classed elements.

Here it is: In fact, given the style sheet shown, the style of font-weight: Results of using a class selector As you can see, the class selector works by directly referencing a value that will be found in the class attribute of an element. This reference is always preceded by a period. The period is necessary because it helps keep the class selector separate from anything with which it might be combined -- like an element selector.

For example, maybe we only want boldface text when an entire paragraph is a warning. The simplest way to translate the selector P. See Figure for the results of these styles: Making selectors more specific In this case, the warning paragraph is boldfaced, while the warning SPAN is italicized. Each rule applies only to a specific type of element-class combination, and so does not "leak over" to other elements.

In addition, you could use a combination of a general class selector and an element-specific class selector to make the styles even more useful, as shown in Figure Using generic and specific selectors to combine styles Now we have a situation where any warning text will be italic, but only text within a SPAN element with a class of file: The Definitive Guide warning will be boldface as well. ID Selectors In many ways, ID selectors are just like class selectors -- but there are a few crucial differences.

The first difference is the fact that ID selectors are preceded by an octothorpe -- otherwise known as a pound sign, hash mark, or tic-tac-toe board -- instead of a period. Thus, you might see a rule like this one: That's the second difference: This likely does not come as a surprise to you. Here's an example of an ID selector in action: Using an ID selector Note that the value first-para didn't have to be applied to a paragraph.

It could have been assigned to any element within the document. What's the Difference? Up until now, we've been using both class and ID selectors in very similar ways. In effect, we've been using both to usurp the structural nature of HTML, since styles can be applied to any element, regardless of its function in the structure of the document.

This is not generally recommended, since one could in theory use class and ID selectors to make any element behave like any other element, which runs somewhat counter to the purpose of HTML as a structural language. But all this aside, what's the difference between classes and IDs? In the first place, classes may be assigned to any number of elements, as we saw earlier -- the class warning was applied to both a paragraph and some SPAN text, and it could have been applied to many more elements.

This isn't correct behavior, but it happens anyway. Other markup languages may not have the same restrictions on ID values, although there's no way to know that until you actually go look at a given language's specification. The other difference between class and ID is that IDs carry a higher weight when trying to determine which styles should be applied to a given element.

This is explained in greater detail in Section 2. Like classes, IDs can also be declared independently of an element, although since they are supposed to be unique anyway, this generally makes less sense.

Still, there may be circumstances in which you know that a certain ID value will appear in a document, but not the element on which it will appear, so it is useful to be able to declare standalone ID selectors.

For example, you may know that in any given document, there will be an element with an ID value of mostImportant. You don't know whether that most important thing will be a paragraph, a short phrase, a list item, or a section heading. You know only that it will exist in each document, that it will be on an arbitrary element, and that it will appear no more than once in a given document. In that case, you would write a rule like this: Grouping 2.

Pseudo-Classes and Pseudo-Elements Even more interesting, at least in terms of syntax, are the pseudo-class and pseudo-element selectors. These allow the author to assign styles to structures that don't necessarily exist in the document, or to things that are inferred by the state of certain elements, or even the state of the document itself. In other words, the styles are applied to pieces of a document based on something other than the structure of the document, and in a way that cannot be precisely deduced simply by studying the document's markup.

This may sound like we're applying styles at random. Not at all! Instead, the styles are applied based on somewhat ephemeral conditions that can't be predicted in advance. Nonetheless, the circumstances under which the styles will appear are well-defined.


This is sort of like saying, "During a sporting event, whenever the home team scores, the crowd will cheer. The fact that you can't predict the exact second or inning, or quarter in which this will happen doesn't make the behavior any less expected whenever the home team does score. Pseudo-Class Selectors First we'll look at pseudo-class selectors, since they're better supported by browsers and therefore more widely used.

Let's use an example to see how they work; this will take a minute before I reach the actual point, so bear with me. Consider the anchor, which is used to set up a link from one document to another.

Anchors are always anchors, of course, but some anchors refer to pages that have already been visited, while others refer to pages that haven't been visited yet. You can't tell which are which by simply looking at the HTML markup, because in the markup, all anchors look the same.

Only by comparing the links in a document to the user's browser history is it possible to tell which links have been visited and which have not. So, in a certain sense, there are two basic types of anchors: In fact, these types are known as pseudo-classes, and the selectors that use them are called pseudo-class selectors.

These cause, in a certain sense, ghostly classes to be applied to elements. To see what I mean, let's consider for a moment how browsers behave with regard to links. The Mosaic convention was that links to pages you hadn't visited were blue, and links to already visited pages were red this was modified to purple in succeeding browsers such as Internet Explorer. If you could only insert classes into anchors, such that any anchor you had visited would have a class of, say, "visited," then you could write a style to make such anchors red: Instead, CSS defines pseudo-classes that make the anchors to visited pages act as though they have classes of "visited.

The Definitive Guide There you have it -- any anchor that points to a visited page will now be red, and you don't even have to add CLASS attributes to any of the anchors. Note the colon: The colon separating the A and the visited is the calling card of a pseudo- class or pseudo-element. All pseudo-class and pseudo-element keywords are preceded by a colon. Here's another example: Pseudo-classes in action In CSS1, there are only three pseudo-classes. We've already seen: Each refers to a different kind of anchor, as shown in Table Table Psuedo-classes Name Description Refers to any anchor that is a hyperlink i.

Note that some browsers may interpret: Refers to any anchor that is in the process of being activated e. In CSS1, this applies: The first of the pseudo-classes in Table may seem a little bit redundant. After all, if an anchor hasn't been visited, then it must be unvisited, right? Therefore, all we should need is the following: The first of the rules shown here will apply not only to unvisited links, but also target anchors such as this one: The Definitive Guide The text shown would be blue, because the A element will match the first rule shown before.

Therefore, in order to avoid applying your link styles to target anchors, use the: In the case of the CSS pseudo-classes, of course, you can apply more than just colors. Assume that an author wishes to create a page in which all anchors are purple if the linked pages haven't been visited, red if they have been, and yellow as the user clicks them.

In HTML, this could be done as follows: For example, let's say you wish to change the color of any link that points outside your own site. If you assign a class to each of these anchors, it's easy. Real-world issues There are some interesting issues with the anchor pseudo-classes.

For example, it would be possible to set visited and unvisited links to one font size, and active links to have a larger size: A user agent that supports this behavior is forced to redraw the document as an anchor is being clicked. However, the CSS specifications specifically state the user agents are not required to do redraw a document once it's been drawn for initial display, so you can't absolutely rely on this happening.

Whatever you do, avoid designs that depend on such behavior! Pseudo-Element Selectors In a fashion similar to the way that pseudo-classes cause ghostly classes to be assigned to anchors, pseudo-elements trigger the insertion of fictional elements into a document in order to achieve certain effects.

In CSS1, there are two pseudo-elements, called: These are used to apply styles to the first letter or first line, respectively, of a block-level element such as a paragraph.

Another possibility is to make the first letter of each H2 twice as big as the rest of the heading, with the result seen in Figure For example, we could make the first line of each paragraph in a document gray: This is true no matter how wide or narrow the display region may be.

If the first line contains only the first five words of the paragraph, then only those five words will be gray. If the first line contains the first 30 words, then all 30 will be gray. This is most easily visualized with an example employing "fictional tags," as the CSS1 specification calls them. Assume the following markup: That style causes the first line to be gray. No other text in the paragraph is affected by this rule at least, it shouldn't be. This is a paragraph of text which has only one style applied to it.

No other Since the text from "This" to "only" should be gray, the user agent employs a fictional markup that looks something like this: This fictional tag sequence causes the effect seen in Figure How the pseudo-element selector: It isn't even a valid element at all.

Instead, its existence is constructed on the fly by the user agent and used to apply the: Remember, you don't have to add any new tags. The user agent will do it for you. This time it uses a first-letter effect. These are given in Table 2- 2. Properties Permitted on Psuedo-elements: As we will see, this changes to file: It is possible to combine pseudo-class and -element selectors with class and ID selectors, although the syntax for doing so is fairly rigid.

Class and ID Selectors 2. Structure As I've mentioned before, CSS is powerful because of the way in which it uses the structure of HTML documents to determine which styles should be applied in what ways. That's really only a part of the story, though, since it implies that the only way CSS uses document structure is to determine which rules apply to which elements. The truth is that structure plays a much bigger role in the way styles are applied to a document.

In order to understand this role, we need to understand how documents are structured. HTML documents, and indeed most structured documents of any kind, are based on a hierarchy of elements, which is visible in the "tree" view of the document in Figure In this hierarchy, each element fits somewhere into the overall structure of the document, and every element is either the parent or child of another element, and often both.

The "tree view" of a simple HTML document An element is said to be the parent of another element if it encloses the other element. Conversely, an element is the child of another element if it is enclosed by the other element. The terms parent and child are often generalized to the terms ancestor and descendant. There is a difference: Of course, the child is also a descendant, and the parent is an ancestor.

If, however, the path from one element to another moves through two or more levels, then the elements have an ancestor-descendant relationship. For this reason, the HTML element is also sometimes called the root element. Contextual Selectors The first benefit we derive from this model is the ability to define contextual selectors.

This is simply the act of creating rules that operate in certain structural circumstances but not others. As an example, let's say you want to set EM text to be file: The Definitive Guide gray, but only if it's found within an H1 element. It would obviously be far better to declare rules that only match EM elements which are found inside H1 elements.

To do so, you would write the following: Other EM text, such as that found in a paragraph or a block quote, will not be selected by this rule, as Figure makes clear. Selecting an element based on its context In a contextual selector, the selector side of a rule is composed of two or more space-separated selectors. Each space can be translated as "found within," "which is part of," or "that is a descendant of," but only if you read the selector backwards.

This is obviously a very specific selection criterion. A very specific contextual selector Contextual selectors can be very, very powerful. Let's consider a common example. Assume you have a document that has a sidebar and a main area. The sidebar has a blue background, and the main area has a white background. You have a list of links in the sidebar, but you also have links which appear in the main text.

CSS: The Definitive Guide, 4th Edition

You can't set all links to be blue, because they'd be impossible to read in the sidebar blue text on a blue background. The Definitive Guide The solution? Contextual selectors. In this case, you set the table cell that contains your sidebar to have a class of sidebar, and the main area to have a class of main-page. Then you write styles something like this: Using contextual selectors to apply different styles to the same type of element Here's another example: Context and Style TIP Thankfully, contextual selectors are one of the things that almost everyone got right the first time they tried -- although there are some minor random quirks to be found in Navigator 4.

In general, though, like grouping, contextual selectors are as safe and stable an operation as you're going to find. Pseudo-Classes and Pseudo- 2. Inheritance Viewing a document as a tree is very important for one other reason: Inheritance is simply the mechanism by which styles are applied not only to a specified element, but also to its descendants.

If a color is applied to an H1 element, for example, then that color is applied to all text in the H1, even the text enclosed within child elements of that H1: This is very likely what the author intended, which is why inheritance is a part of CSS. Inheritance of styles The alternative would be a hypothetical situation where inheritance does not operate; in that case, the EM text would be black, not gray.

Another good example of how inheritance works is with unordered lists. Let's say we apply a style of color: What we expect is that a style that is applied to a UL will be applied to its list items as well, and to any content of those list items. Thanks to inheritance, that's exactly what does happen, as Figure demonstrates: Inheritance of styles Inheritance is one of those things about CSS that are so basic that you almost never think about them unless you have to -- rather like the way we tend to take the convenience of the highway system for granted until part of it is closed or otherwise rendered difficult to use.

However, there are a few things to keep in mind about inheritance. Limitations of Inheritance As with any great thing, there are a few blemishes if you look closely enough.

Смотри также

First off, some properties are not inherited. This may be for any number of reasons, but they generally have to do with simple common sense. For example, the property border which is used to set borders on elements, oddly enough does not inherit.

A quick glance at Figure 2- 25 will reveal why this is the case. Were borders inherited, documents would become much more "cluttered" unless the author took the extra effort to turn off the inherited borders. Why borders aren't inherited As it happens, most of the box-model properties, including margins, padding, backgrounds, and borders, are not inherited for this very reason. Inherit the Bugs Inheritance can be a tricky thing, unfortunately. Thanks to problems in various browser implementations, an author cannot always rely on inheritance to operate as expected in all circumstances.

For example, Navigator 4 and, to a lesser extent, Explorer 4 does not inherit styles into tables. Thus the following rule would result in a document with purple text everywhere outside of tables: This is not technically correct behavior, but it does exist, so authors often resort to tricks such as: Despite this potential problem, the rest of this text will treat inheritance as though it operated perfectly in all cases, since an exhaustive documentation of the ways and reasons it might fail could be a book in itself.

Structure 2. Specificity Given the existence of inheritance, one might well wonder what happens in a circumstance such as this: As it happens,. This happens because of the specificity of the two rules, and the rules CSS has to deal with such situations. Specificity describes the relative weights of various rules. According to the specification, a simple selector e.

Thus the following rules would have the noted specificity: In cases where more than one rule can apply to an element, the styles with the higher weight win out.

Inheritance and Specificity Within the framework of specificity, inherited values have, effectively, a specificity of 0.

This means that any explicitly declared rule will override an inherited style. Therefore, no matter how much weight a rule might have, it is only inherited if no other rule can be applied to the inheriting element. For example, consider the following: The Definitive Guide You would likely expect that all list items would be gray except for those which are found in lists with a class of vital, in which case they'll be white.

However, as Figure demonstrates, this is not the case. Apparently incorrect behavior Why does this happen? Because the explicit declaration with the selector LI wins out over the value which might have been inherited from the UL. Let's look at this process in a little more detail.

Given the following markup, the emphasized text will be gray, not black, since the rule for EM outweighs the value inherited from the H1: The fact that the original specificity of the H1 id3 rule is has no effect on the inherited value, whose weight is still 0. If the intention is to have H1s be consistently black, while EM text in all other circumstances should be red, then the following would be a good solution: However, EM text inside H1 elements will be black, because the specificity of their selector 2 is greater than that of the second rule 1.

Note that since, due to selector grouping, there are effectively two rules in the first statement one for H1 and one for H1 EM , there are also two specificities -- one for each rule.

In practice, however, this specificity value is somewhat higher, since the value of a STYLE element seems to outweigh most normal rules, even those which technically have a higher specificity such as H1 id3 EM.

In other words, the following markup will generally have the result shown in Figure Inline styles have high specificity You might choose to treat STYLE value as having a specificity value of, say, 1,, although this interpretation is not supported by the CSS specification and so cannot be relied upon. Finally, pseudo-elements are ignored altogether when calculating specificity, but pseudo-classes are treated like regular classes. There is one other wrinkle in the specificity picture, which is a way to pretty much override the entire specificity mechanism.

Importance Ever felt like something is so important that it outweighs all other considerations? Well, it's possible to mark certain rules as being more important than others. These are called important rules due to the way in which they are declared and also because of their very nature.

An important rule is marked by inserting the phrase! If you wish to mark both rules as important, then each rule will need its own! This is especially important -- no pun intended -- when it comes to properties that allow values which contain multiple keywords, such as font: Rules that are marked! Note that while author- defined styles are treated as having a greater weight than reader-defined styles see Section 2.

Indeed, an! Thus, given the following code, the result will be gray text, not black: This is true even if the rule from which the value comes has an! All of its importance is lost outside the elements which match that rule.

Internet Explorer 5 and Opera 3. On the other hand,! Inheritance 2. The Cascade Through all this, we've skirted one rather important issue: How does the browser resolve the conflict?

For example, let's say we have the following rules: Both have a specificity of 1, so they have equal weight and should both apply. That simply can't be the case, though, because the element can't be both red and blue. It has to be one or the other. But which?

This is where the name "Cascading Style Sheets" finally makes some sense. CSS is based on a method of causing styles to cascade together -- a method that is made possible by the rules of inheritance and specificity, as it happens.

Book Details

The cascade rules are simple enough: Find all declarations that contain a selector that matches a given element. Sort by explicit weight all declarations applying to given element. Those rules marked! Also sort by origin all declarations applying to a given element.

There are three origins: Under normal circumstances, the author's styles win out over the reader's styles. Either author or reader styles will override user agent styles. Sort by specificity all declarations applying to a given element. Those elements with a higher specificity have more weight than those with lower specificity.

Sort by order all declarations applying to a given element. The later a declaration appears in the style sheet or document, the more weight it is given. Declarations that appear in an imported style sheet are considered to come before all declarations within the style sheet that imports them, and declarations within STYLE attributes come later than those in the document's embedded style sheet.

In order to be perfectly clear about how this all works, let's consider an example that illustrates each of the four cascade steps. The first one is simple enough: Under the second step, if two rules apply to an element, and one is marked!

This gray is inherited by the EM element as well. Sorting styles by their importance Furthermore, the origin of a rule is considered. If an element is matched by styles in both the author's style sheet and the reader's style sheet, then the author's styles are used. For example, assume that the following styles come from the indicated origins: Sorting styles by origin As it happens, the browser's default styles -- which are often influenced by its preferences -- are figured into this step.

The browser's default styles are the least influential of all. Therefore, if an author-defined rule applies to anchors e. If readers wish to enforce certain rules at all costs, then they must define them in a local style sheet and declare them to be! In this case, though, it will be a case of the reader's styles overriding both the author's and browser's styles due to their importance.

According to the third step, if multiple rules apply to an element, then they should be sorted by specificity, with the most specific rule winning out. Sorting styles by specificity As we can see from Figure , the text of the paragraph is silver, except for the EM text, which is gray.

Because the specificity of P bright overrode the specificity of P 1 , even though the latter rule comes later in the style sheet. However, the EM text does not inherit the value silver, because it has an explicit rule associated with it, and the rule's specificity 2 overrides the inherited value of silver.

This is not a trivial point. For example, assume that a style sheet has been written such that all text in a toolbar is to be white on black: If, however, the text within this element is all hyperlinks A elements , then the user agent's styles for hyperlinks will take over -- because despite the fact the they're imported, they are explicit style assignments, so they override any inherited values.

Usually, this means that they'll be colored blue, since the browser's style sheet probably contains an entry like this: Using contextual selectors to overcome specificity Lastly, under the fourth step, if two rules have exactly the same weight, origin, and specificity, then the one that occurs later in the style sheet wins out. Thus, let us return to our earlier example, where we find the following two rules in the document's style sheet: The Definitive Guide Any rule that is contained in the document, having a higher weight than the imported rule, wins out.

This is true even if the rule is part of the document's style sheet and not part of an element's STYLE attribute. For the purposes of this rule, styles that are given in the STYLE attribute of an element are considered to be at the end of the document's style sheet, which places them after all other rules.

Thus, the following code would result in black text, as shown in Figure Sorting styles by their position Here, the weight of the inline style is equal to the weight of the rule hello , so the winner is the rule that occurs later in the document. Specificity 2. Classification of Elements As we have already discussed, elements in a document occur in a sort of hierarchy.

At the most basic level, block-level elements contain other block-level elements, inline elements, and replaced elements.

A part of this hierarchy scheme depends on the relationships between these types of elements; for example, while inline elements can be children of block- level elements, the reverse is not true. In CSS, elements are grouped into three types: Replaced elements, such as images and form inputs, can be block-level elements but usually are not. Each block-level element is displayed on a line by itself, so to speak, beginning on a new line and forcing any element after it to do the same. Block-level elements can only be children of other block-level elements, and then only in certain circumstances.

They do not force anything to start on a new line, not even themselves, and can be the children of any other element. These are specially defined to have presentation aspects such as a "marker" a bullet, letter, or number and a certain sense of ordering, if such an element appears within an ordered list of some kind. Thus, list items within such a list can be automatically numbered, based on their context within the document. These terms are, as it happens, the basic three of the four values for the property display.

The Definitive Guide Display Values block inline list-item none Initial value block Inherited no Applies to all elements Unlike almost every other property in CSS, display is often evaluated to a value other than its default.

Instead, the value of display for a given element is defined in the document type definition[1] DTD for the language being used. A and EM, on the other hand, are inline elements, and of course LI is a list item. Thus, the default display values for these elements would be: The DTD provides a rigorous way of defining what elements mean and how they fit into the language's hierarchy.

It's similar to describing English as a collection of nouns, verbs, adverbs, and so on, but with the added bonus of describing what each of the parts means and how they relate to each other. DTDs are not easily readable by the untrained eye. A floated image is considered to be block-level, for example, but images are usually inline.

Total price: Add all three to Cart Add all three to List. These items are shipped from and sold by different sellers. Show details. download the selected items together This item: The Definitive Guide by Eric A. The Definitive Guide: Ships from and sold by site. FREE Shipping. Customers who bought this item also bought. Page 1 of 1 Start over Page 1 of 1. David Flanagan. The Definitive Guide 6th Edition.

Chuck Musciano. Up and Running: Dive into the Future of Web Development. Mark Pilgrim. The Good Parts. Douglas Crockford. CSS Pocket Reference: Visual Presentation for the Web.

Eric A. Don't Make Me Think, Revisited: Steve Krug. Read more. Product details Paperback: English ISBN Tell the Publisher!

I'd like to read this book on site Don't have a site? Share your thoughts with other customers. Write a customer review. Read reviews that mention definitive guide eric meyer style sheets need to know css the definitive cascading style box model cover to cover web pages great introduction property reference recommend this book download this book learn css good reference understand css css properties reference book css design book is good.

Top Reviews Most recent Top Reviews. There was a problem filtering reviews right now. Please try again later. Paperback Verified download. download this book if you're new to CSS. This will become your 'bible', up to and including 2. Be advised, however, that it is excruciatingly technical and thorough.

Like I said: Every question I had about CSS was answered by this text. All of 'em. Good stuff, as it teaches one to utilize CSS in the way it was designed: This book has not aged well - aside from examples that look hideous by contemporary standards, it doesn't cover CSS3 at all.

To be fair, the book predates CSS3. This book is more of a historical document than a guide to actually making web sites - no, nothing in here is quite wrong per se, but this is from an era before transitions, transforms, and gradients, but also an era when there was no LESS or Sass or Bootstrap or Foundation. Don't download. I wasted my money One person found this helpful.

site Edition Verified download. This book is very well written and very thorough. I already knew css but thought that I learned it a while ago so I needed a brush up and an update. Turns out there was a lot that I never learned so a brush up lesson turned into a full blown lesson. Had I never bought this book I never would have known that, I definitely would suggest this book to anyone who wants to learn styling for the web.

He takes every element of CSS and defines context and specifics with an amazing amount of detail, but supremely well organized so that it's actually readable in a conversational way rather than a textbook or reference book way. If you are planning to regularly deal with ALL of the ramifications of designing web sites, this book is a must-have to provide a thorough foundation.This allows authors to simplify document appearance management and maximize their effectiveness, thereby making their jobs a little easier.

Selectors, specificity, and the cascade Values, units, fonts, and text properties Padding, borders, outlines, and margins Colors, backgrounds, and gradients Floats and positioning tricks Flexible box layout The new Grid layout system 2D and 3D transforms, transitions, and animation Filters, blending, clipping, and masking Media and feature queries. My advice is to avoid all these file: If you want to change this to another color, then the alteration of this single rule will affect all H2s in the document: As we'll soon see, all colors can be specified in a consistent manner, which would seem to solve the issue of whether two different user agents will display the same color.

FLORANCE from Monterey
Look through my other articles. One of my hobbies is f1 powerboat world championship. I fancy sharing PDF docs seldom .