
CSS, which stands for Cascading Style Sheets, is a stylistic language that defines how web pages are presented. It complements HTML, which is the language used for describing the structure of web site content. Because HTML predates CSS, it includes a number of limited stylistic elements and attributes, but they have largely been deprecated in favor of the much greater design control offered by CSS.
One of the main features of CSS is that it enables the complete separation of a web site’s presentation from its content. This separation reduces the complexity and repetition associated with including stylistic information in the structural content. Furthermore, this separation makes it easy to enforce site-wide consistency in the presentation of a web site because the entire look and feel of a site can be controlled from a single style sheet document.
As one of the core languages of the Web, CSS is used today by almost all web sites to enhance the web experience. It has been a revolution in the World Wide Web and is a must-learn language for anyone working with web design. Like HTML, the CSS language is easy to learn and use, as is shown in this book.
The CSS language is under ongoing development by the World Wide Web Consortium (W3C), which is the international standards organization for the Internet. The W3C writes the specifications that web browsers implement to display web pages consistently in compliance with those specifications. Each new specification extends the language with new features while maintaining backward compatibility.
The first specification, CSS level 1 (or CSS 1), became a W3C recommendation in 1996. In 1998, CSS 2 was finalized, extending CSS 1 with additional features. Because all widely used web browsers currently implement the features of both these specifications, it is seldom necessary to make a distinction between them, and this book does so only when relevant.
Since 1998, the W3C has been working on CSS 3. Unlike the two earlier levels of CSS, this level became considerably larger and was therefore split into several separate specifications called modules. This split allowed the modules to mature independently at their own pace. As a result of the ongoing development, support for CSS 3 varies. Some features are stable and have widespread browser support; other features are supported only by the latest browser versions or are not supported at all. This book focuses mainly on the CSS 3 features that are supported in the major browsers at the time of writing.
CSS is commonly used within a style sheet document, which consists of a list of rules. For example, a rule to color all paragraph elements red is shown here:
This rule has two parts: a selector and a declaration block. The selector is the link between the HTML document and the style sheet that specifies the element to which the rule is applied. In this case, it is the type selector p that applies to all paragraph elements ( <p> ). Any HTML element can be used as a type selector in this way.
The declaration block, which is enclosed within curly braces, defines the styling applied to the selected elements. The block can contain one or more declarations, each of which is made up of a style property followed by a colon and a valid value for that property. Each declaration is terminated with a semicolon, although this is optional for the last one.
Although the last semicolon is not necessary, it is a good practice to include it because it is easy to forget the missing semicolon when you add more styles to the rule. Another general practice is to use lowercase letters when writing CSS, even though selectors and properties are case insensitive.


is a Senior Software Engineer at Desire2Learn Inc., helping to build and maintain an integrated learning platform. As a self-taught developer, he is always interested in emerging technologies and enjoys working on and solving problems that are outside his comfort zone.
When not at the office, Victor has a number of hobbies, including photography, horseback riding, and gaming. He lives in Ontario, Canada, with his wife, Alicia, and their two children.
There are three ways to insert CSS into an HTML document: by using an internal style sheet, inline styles, or an external style sheet. An internal style sheet applies to a single page, an inline style to a single element, and an external style sheet to potentially an entire web site.
This approach should be used sparingly because it mixes style with content and therefore makes future changes more cumbersome. It can be useful as a quick way to test styles before they are moved out to an external style sheet.
Using an external style sheet is often preferred because it completely separates CSS from the HTML document. It is then possible to quickly create a consistent look for an entire web site and to change its appearance just by editing a single CSS document. It also has performance benefits because external style sheets are cached and therefore need to be downloaded only once—for the first page a visitor views at your site .
Go ahead and save this text file as “MyPage.html” in the same folder as the CSS file. You have now created a simple environment in which you can test CSS. To view the page, open MyPage.html with your web browser. You will see that the background is indeed colored red because of the rule in the style sheet.
While you have the browser opened, you can view the HTML markup that makes up the page by pressing Ctrl+U on a PC or Cmd+U on a Mac. This shortcut works in all major browsers, including Chrome, Firefox, Safari, Opera, Edge, and Internet Explorer (IE). You can also find the view source window by right-clicking on the page and selecting “View Source.” In Firefox and Chrome, the style sheet is clickable, allowing you to view the external style sheet rules that apply to the web page.
Viewing the source code of web pages like this provides a great way to learn from other web developers. Whenever you find an interesting element on a web page—whether it is created with HTML, CSS or JavaScript—the page source will reveal how it was created.
The formatting you use is a matter of preference. Choose the one that makes sense to you and aim to keep it consistent.
To keep style sheets short and easy to edit, similar rules can be grouped together. This grouping provides several ways to specify a set of related rules. For example, you can color the text red and the background black for two header elements in four different ways, as described in the following sections.
Rules should be grouped whenever possible to make the style sheet more succinct. This has a small performance benefit because concise rules reduce the size of the style sheet, which makes the CSS file load more quickly. Moreover, it is convenient to specify the properties in only one place, in case they have to be changed later. Additionally, grouping selectors with similar styles makes it easier to maintain consistency between them.
Class and id selectors define rules that apply to only a selected set of HTML elements. They allow you to identify individual elements, or groups of elements, without having to style all instances of the element type.
Specific classes make it easier to identify where a class is used, whereas general classes can allow for greater code reuse.
In many instances, using classes is the preferred method of selecting elements in CSS because classes are both flexible and reusable. Ids, on the other hand, are often used for structural elements of a site, such as #content and #footer, to highlight that those elements serve a unique role.
Attribute selectors allow style to be added to elements based on their attributes and attribute values.
The pseudo-classes and pseudo-elements are keywords that can be appended to selectors to make them more specific. They are easy to recognize because they are always preceded by one or two colons.
Pseudo-elements enable parts of an element to be styled. Only one of these can be applied to any one selector.
The preceding first rule makes the initial letter in a paragraph render 20% larger than other text. The second rule makes the first line of text in a paragraph bold.
Pseudo-classes permit styling based on element relationships and on information not found in the HTML document. Most of them fall into three categories: dynamic, structural, and user interface pseudo-classes. Unlike pseudo-elements, multiple pseudo-classes can be applied to a selector to make it even more specific.
The first category of pseudo-classes is used to apply styles to links or other interactive elements when their state is changed. There are five of them, all of which were introduced in CSS 2.
The structural pseudo-classes target elements based on their relation with other elements. CSS 2 included only one structural pseudo-class in this category, :first-child, whereas CSS 3 introduced a wide array of new ones. The CSS 3 structural pseudo-classes are supported in all major browsers.
The math and arguments used together with :nth-child() are also valid for the next three pseudo-classes, all of which start with :nth .
This pseudo-class is mainly useful when CSS is used with other languages, such as XML, in which the root element can vary.
CSS 3 introduced a number of user interface pseudo-classes that are used to style interactive elements based on their current state.
Note that these pseudo-classes are in no way a substitution for proper form validation, using JavaScript or PHP, for example. Browser support for these two pseudo-classes exists in Chrome 10+, Firefox 4+, Safari 5+, Opera 10+, and IE 10+.
Like the :valid and :invalid pseudo-classes, support for :required and :optional exists in Chrome 10+, Firefox 4+, Safari 5+, Opera 10+, and IE 10+.
Some pseudo-classes do not fit into any of the earlier categories, namely the :target, :lang” :not pseudo-classes.
Relationship selectors match elements based on their relation with other elements. To understand these selectors, it is important to recognize how elements in a web document are related to each other.

Example HTML hierarchy
Now that you have an understanding of the HTML hierarchy and the inheritance concept, the relationship selectors of CSS can be discussed.
The general sibling selector is supported by all major browsers, including Chrome 1+, Firefox 3.5+, Safari 3.2+, Opera 10+, and IE 7+.
When more than one rule applies to the same element and they specify the same property, there is a priority scheme that determines which rule is given precedence. In short, CSS gives priority to the rule that has the most specific selector.
If the specificity between two conflicting rules is the same, the cascade is what determines which rule is applied.
In CSS, more than one style sheet can influence a document’s presentation. This feature is known as cascading (the “C” part of CSS) because the browser merges all style sheets to resolve any conflicts before the styles are applied.
Web documents can have style sheets that come from three different sources: the browser, site designer and user reading the document. The designer’s style sheet usually has the highest priority, followed by the user’s personal style sheet (if any) and then the browser’s default one.
As discussed earlier, web designers have three ways to include CSS rules: inline styles, internal style sheets, and external style sheets. Among these three, inline styles are given the highest precedence, followed by internal style sheets and then external style sheets.
As shown in this chapter, the style of an element can be specified in many different places and with different priorities. The cascading feature gives a lot of flexibility to CSS, but it can also result in unnecessary complexity if not managed well.
In general, you want to keep specificity low to make it easier to know which rules will take precedence. This way, you can allow the cascade to work for you by adjusting the order in which your style rules appear, instead of needlessly increasing the specificity with id and !important to override conflicting styles.
There are several different ways to specify a color in CSS, which are described in the following sections.
The HTML and CSS color specification includes 140 predefined color names, such as white, lime, and olive. These colors are all supported by the major browsers.
Although this color notation is the most obfuscated one, it is also the most common one because of its precision, conciseness, and browser support. An easy way to discover the hexadecimal value of a color is to use a color picker tool, for instance the one provided by the Google search engine when searching for “color picker.”
The short hexadecimal notation is a useful shortcut when the full precision provided by the longer hexadecimal notation is not needed.
This notation allows the same color precision as the hexadecimal notation. What notation to use comes down to a matter of preference, but the hexadecimal notation is often preferred because it is shorter and can easily be copied from an image editor, for example.
A browser that does not support RGBA ignores the second declaration and continues to apply the opaque version .
HSL colors are arguably more intuitive to adjust compared with RGB colors, but keep in mind that legacy browsers such as IE 8 do not support them. HSL is a CSS 3 value and is supported in Chrome 1+, Firefox 1+, Safari 3.1+, Opera 10+, and IE 9+.
HSLA is supported in Chrome 1+, Firefox 3+, Safari 3.1+, Opera 10+, and IE 9+, same as the RGBA function.
There are several units to choose from when specifying the size of a property’s value.
These units are mainly useful when the size of the output medium is known, such as for content that will be printed to paper. They are not recommended for screen displays since screen sizes can vary a lot.
Pixels and percentages are two of the most useful units in CSS for onscreen displays. Pixels are fixed size, so they allow very precise control over the layout in a web document. Percentages, on the other hand, are useful for defining font sizes for text content because the text remains scalable, which is important for small devices and accessibility purposes. When the text is part of the design and needs to match other elements, it can be sized in pixels for greater control. Modern browsers all support full-page zooming, which has made pixel-based font sizes more acceptable. Note that for high-resolution screens, a CSS pixel renders as multiple screen pixels. For example, the first Apple Retina display renders all pixel dimensions as four physical pixels .
Like percentage, em-height is a good relative unit that is commonly used for setting the font size of web document text. They both respect the user’s choice of font size in their browser and are easier to read on small-screen devices than pixel-based font sizes. The ex-height unit on the other hand is not commonly used.
The ch unit is supported as of Chrome 27+, Firefox 19+, Safari 7+, Opera 15+, and IE 9+, so it should be used only together with a fallback. The rem unit has slightly better support and works in Chrome 4+, Firefox 3.6+, IE 9+, Safari 4.1+, and Opera 11.6+.
Chrome 26+, Firefox 19+, Safari 6.1+, Opera 15+, and Edge 16+ support all the viewport units. The vh, vw, and vmin units have greater support than vmax, going back to Chrome 20+, Safari 6, and IE 10+ .
Whenever a CSS declaration contains an error, it is ignored by the browser. Any other valid declarations in the rule still apply.
Variables are supported by Chrome 49+, Firefox 31+, Safari 1+, Opera 36+, and Edge 15+. Note that IE does not support variables. If legacy browser support is needed another way to achieve the benefits of variables, along with other programming functionalities, is to use a CSS preprocessor such as SASS.1 However, this adds a layer of complexity since the SASS code will need to be compiled into CSS to be used.
Browsers supporting the calc function include Chrome 26+, Firefox 16+, Safari 7+, Opera 15+, and IE 9+.
This notation means that the text-shadow property can have one of three different kinds of values. The default value is listed first; in this case, it is inherit. Because the inherit keyword can be set for any property, it will not be included in the list unless it is the default value. The second value, none, is also a keyword. It is the initial value for this property and can be applied to disable an inherited property effect.
In addition to inherit, there are two other generic property keywords you might come across in CSS: initial and unset. Both generic keywords were introduced in CSS 3 and can be set on any properties.
The initial keyword applies a property’s initial value to an element, as defined by the CSS specification. It is supported in Chrome 4+, Firefox 19+, Safari 3.2+, Opera 15+, and Edge 12+. IE 6-11 has no support for this keyword, but Microsoft’s Edge browser started out with support for it. Keep in mind that Edge started out with version 12 superseding IE 11, so a CSS feature supported in IE is also supported in Edge. Until IE usage drops sufficiently low the usefulness of the initial keyword is limited. It is recommended to instead explicitly specify the initial value for a given property to reset it.
The third generic keyword is unset, which is a combination of the initial and inherit keywords. It resets the property to its inherited value, if there is one; otherwise, it sets the property to the initial value. Support for the unset keyword is limited to Chrome 41+, Firefox 27+, Safari 9.1+, Opera 28+, and Edge 13+. No version of IE supports this keyword.
When HTML and CSS became standardized by the World Wide Web Consortium (W3C), web browsers could not just comply with the standards because doing so would break most web sites already in existence. Browsers instead created two separate rendering modes: one for new standard compliant sites (full standards mode) and one for old legacy sites (quirks mode).
This doctype triggers full standards mode in all major browsers, dating back as far as IE 6.
Many browsers begin incorporating new CSS properties long before their specification becomes stable. Because these implementations are experimental, their property names include a vendor prefix to indicate that the specification could potentially change in the future.
As time goes on, the new property’s specification becomes stable, and browsers drop the vendor prefix. Given more time, web users abandon old browsers in favor of new versions, and the need for vendor prefixes diminishes. This has already occurred for the border-radius property, and developers are now encouraged to drop the prefixes, making things a little easier for web developers worldwide .
When deciding whether to use a recent CSS feature, it is important to consider how your site will look without it. If the feature enhances the appearance of your site, such as the CSS 3 border-radius property, you might want to start using the feature, even when it is viewable by only a small percentage of your visitors. Time works in your favor, and as people abandon old browsers, a greater number of your visitors can see the feature, which enhances their experience on your site. This is the essence of progressive enhancement.
On the other hand, if your site depends on the feature and appears broken without it, you need to carefully consider how well supported the feature is and whether there are fallbacks or scripts you can make use of to increase this support. There are often many ways to achieve the same result in CSS, so it is a good idea to choose a method that is well supported by all major browsers for the key elements of your site, such as the layout.
The text properties serve to format the visual appearance of text content.
This property does not inherit, but its effect renders across descendent inline elements in a way that is similar to inheritance.
The text-align property inherits, so it needs to be explicitly changed in child elements to restore default left alignment.
This property does inherit, so it can be set once for the <body> element to apply to the whole web page.
The shadow is defined using two offset values, followed by two optional values for the blur radius and color. The x and y offsets are specified as length values relative to the text. Positive values move the shadow right and down; negative values move it left and up.
text-shadow is a CSS 3 property that is supported by most major browsers, including Chrome 2+, Firefox 3.5+, Safari 1.2+, Opera 9.5+, and, IE 10+.
The text-rendering property is not defined by any CSS standard but is supported by WebKit and Gecko browsers, including Chrome 4+, Firefox 3+, Safari 5+, and Opera 15+ .
The following properties deal with the space between text content. They are all inherited by default.
Line height has no effect on replaced inline elements such as <img>. When used on other (nonreplaced) inline elements, it sets the line height as expected. For block elements, line-height sets the minimal height of line boxes within the element .
Setting whitespace to nowrap prevents text from wrapping for anything other than the line break tag <br>. The pre (preformatted) value also prevents wrapping, as well as preserving all whitespace characters. Its behavior is the same as the <pre> element in HTML.
Both the pre-wrap and pre-line values allow text to wrap as normal, with pre-wrap preserving sequences of whitespace and pre-line collapsing them. The difference between pre-line and normal is that pre-line preserves newline characters.
The font properties can be used to change aspects of the font and to load custom fonts. They can be applied to any element and they all inherit.
It is recommended to end the list with a family name to make sure that at least the font family is consistent across browsers. Note that if a font name includes spaces, it must be surrounded by double quotes, as in the previous example.
The larger and smaller values are relative to the parent’s font size; the other predefined values refer to absolute sizes. The initial size is medium, which is 1 em (16 pixels) for normal text.
This font declaration sets the font-style, font-size, line-height, and font-family properties in one declaration. Because font-variant and font-weight are not included, a side effect of using this declaration is that they are both re-set to inherit the parent’s value.
This use of the @font-face rule is supported in Chrome 5+, Firefox 3.5+, Safari 3.1+, Opera 10+, and IE 9+. If the browser does not support the custom font, it reverts to the next standard font in the list.
The background properties can add background effects. None of these properties inherits and they can be applied to any elements.
CSS 3 introduced a third value for this property, local, which fixes the background relative to the element’s content instead of the whole viewport. With this value, the background scrolls along with the element’s content only when that element is scrolled (achieved by using the overflow property). Support for this value was introduced in Chrome 4+, Firefox 25+, Safari 5+, Opera 10.5+, and IE 9+.
This four-value notation is supported only in Chrome 25+, Firefox 13+, Safari 5.28+, Opera 10.5+, and IE 9+.

Backgrounds sized with cover and contain keywords
This property was added in CSS 3 and is supported in Chrome 4+, Firefox 4+, Safari 5+, Opera 10.5+, and IE 9+. Use of the -webkit and -moz prefixes expand support to Chrome 1+, Safari 3+, and Firefox 3.6+.
background-clip is supported in Chrome 1+, Firefox 4+, Safari 3+, Opera 12+, and IE 9+.
A background image is ordinarily rendered starting from the top left of the element’s padding area (padding-box). It can be changed so that the background either starts at the top-left edge of the border area (border-box) or the content area (content-box).
A gradient is a color fill that blends smoothly from one color to another. Introduced in CSS 3, the gradient functions can be used anywhere an image is required according to specification, but they are mainly used together with the background or background-image properties to create a background gradient.

Simple linear gradient

Bottom-right linear gradient

Gradient with multiple color stops

Simple radial gradient

Radial gradient with set stop positions

Circular radial gradient

Resized radial gradient

Size keywords

Bottom-right origin
Linear and radial gradients do not allow gradient patterns to repeat because they naturally stretch to fill the element on which they are defined. Two additional functions are used for creating repeating gradients : repeating-linear-gradient() and repeating-radial-gradient().

Repeating linear gradient

Repeating radial gradient
The syntax for defining gradients is notably more complex than many other CSS features. For this reason, it can be preferable to use an online tool to graphically design the desired gradient. One such tool can be found on Colorzilla.com.1 In addition to the standard compliant gradient code, it also provides the prefixed versions necessary for maximum browser compatibility.

CSS box model
Each of the three boxes surrounding the content can have different sizes on the top, right, bottom, and left of the element. Any or all of these sizes can also be set to zero.
HTML has two primary categories of elements: block and inline. The box model applies differently to these two kinds of elements, so it is important to know the difference between them. Examples of inline elements include <a>, <strong> and <em>, whereas <p>, <h1>, and <form> are block elements.
Inline elements flow along with text content and are split as necessary to fit the width of their container. They may not contain block elements, with the exception of the <a> element, which can wrap any type of element.

Block and inline elements
The boxes surrounding inline and block elements have different features. A block element can manipulate all properties in the box model, including the width and height of the content area, as well as the border, padding, and margin. If no width is set, a block element expands horizontally to the maximum allowed by the containing element.
An inline element is more limited in that it cannot set the vertical margins (top or bottom). It also cannot change the width or height of its inline box. For an inline element, the minimum height can be set with the line-height property, but the width and height adjust automatically to fit the content that the element holds.
There is a subcategory of inline elements, called replaced inline elements, that use external objects such as <img>, <video>, and <object>; and form elements such as <input> and <textarea>. With these inline elements, all box properties can be manipulated the same way as block elements.
In HTML 4, the generic <div> element was the main element used for defining sections of a web page to be formatted with CSS. It did not convey any semantic meaning, which was considered a shortcoming of the language. The HTML 5 specification introduced a number of other structural elements you are encouraged to use, such as <header>, <footer>, <section>, <article>, and <nav>.
These new container elements are preferred when they are appropriate given the context, for accessibility and maintainability reasons. Whenever there is not a more semantically suitable element available, the <div> element is still appropriate and continues to be widely used as a generic container. HTML 5 elements became supported in Chrome 6+, Firefox 4+, Safari 5+, Opera 11.5+ and IE 9+.
The border properties are used to format the border around elements. They can be applied to any element and they do not inherit.

border-style appearances

1-to-4-value syntax explained
A width of zero means that no border is displayed. This value has the same effect as setting the style of the border to none.

Border-radius examples
The outline is a line drawn around an element, outside the border edge. It is typically rendered as a dotted line around interactive elements to show which element has focus. Although similar to the border, the outline differs in that it does not take up any space in the box model. Furthermore, unlike the border, all four sides of the outline must be the same. The outline properties can be applied to any element, and none of them inherits.

Outline-style appearances
CSS does not define the numerical thickness of these three keywords, but they typically render as 1px, 3px, and 5px, respectively. Like border-width, the initial value for this property is medium.
To ensure proper contrast, the specification suggests that the default value be invert, which sets the outline to the opposite of the color underneath. However, only IE 8+ and Opera 7+ actually support this optional value, so it is not commonly used.
Although this property is not supported in IE, it works in all modern browsers, including Chrome 4+, Firefox 2+, Safari 3.1+, Opera 12.1+, and Edge 15+.
Margins and padding are used to adjust the position of an element and to create space around it.

1-to-4-value syntax explained
Keep in mind that the padding is part of the element’s background and is affected by the background properties, whereas the margin is always transparent.
Margin and padding can both use percentage values, which are relative to the width and height of the containing element. In contrast with padding, margins can be negative, which allows for element areas to overlap. The auto keyword lets the browser automatically calculate the margin.
The dimension properties control the size of an element, as well as its minimum and maximum dimensions. They do not inherit and can be applied only to block elements and replaced inline elements.
Keep in mind that the fixed width and height properties should not be used together with the min- and max- properties. The four min- and max- properties are supported by all major browsers, including Chrome 1+, Firefox 1+, Safari 1+, Opera 8+, and IE 7+. They are popularly used together with media rules for creating fluid layouts that work well across different screen sizes.
Browser support for the box-sizing property has become good enough that all major browsers now support it. As such, many websites employ this property to simplify the grid calculations for their layouts.
The positioning properties can change how and where elements are displayed. They enable very precise control over the web page layout.
Because the position property is not inherited, and static is the default, there is no need to explicitly set the position property to static.
Relatively positioned elements are considered part of the normal page flow, so other elements do not move to fill in the gap left by the moved element.
Keep in mind that changing the margin affects the layout and fills in gaps, whereas relative positioning does not.

Shaded region is removed
Browser support for the clip property is almost universal: Chrome 1+, Firefox 1+, Safari 1+, Opera 7+, and IE 8+.

Customized stacking order

Common vertical alignment values

Other vertical alignment values
When applied to table cell elements <th> and <td>, the vertical-align property behaves as the deprecated valign attribute in HTML. Valid values for table cells are baseline, bottom, middle, and top. Other values, including lengths and percentages, should not be used with table cells.
The top. value aligns the cell’s top padding edge with the top of the row. Likewise, bottom aligns the cell’s bottom padding edge with the bottom of the row. More notably, the baseline value aligns the cell’s content so that it shares the same baseline as other cells that are baseline-aligned.
In contrast with inline elements that default to baseline, table cell elements are normally aligned in the middle. For table cells, the middle value behaves in a more intuitive way by aligning the cell’s padding box in the middle of the row, making the cell’s content appear centered.
Simpler control over alignment has been added to CSS with the flexbox module, which will be looked at in a later chapter.

Transformed boxes

Matrix transformation
2d transformations, as well as the transform-origin property, are supported in Chrome 4+, Firefox 3.5+, Safari 3.1+, Opera 11.5+ and IE 9+. The 3d transformations were added later and are available as of Chrome 12+, Firefox 10+, Safari 4+, Opera 15+ and IE 10+.
The classification properties specify how an element is displayed and whether it is visible.
One of the more useful values for display is inline-block, which combines features of both block and inline. An inline-block element is like an inline element, except that it can also manipulate the width, height, and vertical margin properties of the box model as a block element does. These features are the same as those of replaced inline elements, such as <img> and <button>. As such, these elements were formally redefined as inline-block elements in HTML 5.

The inline-block value demonstrated
The collapse value is meant to be used only on certain table elements: rows (<tr>), columns (<col>), column groups (<colgroup>), and row groups (<thead>, <tbody>, and <tfoot>). According to specification, collapse should remove the hidden element (same as display: none) and make the space available for other elements to claim. Regrettably, not all major browsers follow the specification for this value. Setting the display property to none results in more consistent browser behavior and should be used instead.

Floated boxes
A side effect of using floats. is that any element that follows these floated boxes also lines up horizontally. The clear property is designed to stop this behavior.
Because floated layouts tend to be complex and fragile they have largely been superseded by other more modern layout methods, such as the flexbox and grid modules.
|
The CSS list properties deal with the list elements, specifically the <ul>, <ol>, and <li> elements.
The color of the marker is the same as the text color of the list element. Keep in mind that any element can be made to display list markers by changing its display type to list-item.
This property overshadows any marker type selected with the list-style-type property. Even so, it is a good idea to specify a list-style-type as a fallback in case the custom bullet image is unavailable for any reason.

Outside and inside marker placement
Keep in mind that list properties cannot only style list containers <ul> and <ol>, but also individual list items <li>.
Counters are supported in all major browsers, including all versions of Chrome, Firefox, Safari, and Opera, as well as IE 8+.
CSS has a number of properties that are used specifically with table elements. These properties offer control over how browsers render tabular data.

Example table
Table cells have borders and padding, but they do not have any margins; they have border-spacing instead. Padding works the same as for other elements and behaves like the cellpadding attribute in HTML.
Setting the value for this property to hide causes the cell’s border and background to be hidden. The layout of the table is not affected.
A fixed table layout has the added benefit that the browser can render the table more quickly because it knows the dimension of the table as soon as the first row is received .
This style sheet contains the print condition, so it is applied only when the document is sent to print media. Keep in mind that browsers still download a style sheet, even if its media condition is false.
The most important media features, min-width and max-width, allow you to create responsive designs in which the site layout changes based on the viewport of the device’s browser.
This media query. is true if the viewing device has a max width of 500 pixels and at least a 1:1 aspect ratio (square or landscape viewport). Notice that the media type is left out here, so the rule applies to all media types.
Note that both the not and only operators require the use of an explicit media type, whereas the logical or (,) and logical and operators do not.
Support for media queries has become widespread in all major browsers. The min-width and max-width queries, for example, are supported in Chrome 1+, Firefox 3.5+, Safari 4+, Opera 8+, and IE 9+.
It is important to test your media queries to make sure that your site looks good in as many devices as possible. The latest web browsers all re-evaluate media queries as the browser environment is changed (when the window is resized, for example). You can therefore test how your design responds to different device dimensions just by resizing your browser window. Chrome also has a built-in toolbar for testing how your site will look on different devices. To show the device selection toolbar first bring up the Inspect window (Ctrl+Shift+I) and then click the Toggle device toolbar icon in the upper left corner (Ctrl+Shift+M).
There are many ways to create a layout in CSS. This chapter will look at some of these methods and how they compare when creating a simple layout.

Floating layout
Support for the multi-column properties is available in Chrome 50+, Firefox 50+, Safari 10+, Opera 37+, and IE 10+. The -webkit- and -moz- prefixes can be used to extend support to Chrome 4+, Firefox 2+, Safari 3.1+, and Opera 11.5+.
The flexbox properties are supported in all modern browsers, including: Chrome 29+, Firefox 22+, Safari 10+, Opera 48+, and IE 11+.
Support for the grid layout properties is available in: Chrome 57+, Firefox 52+, Safari 10.1+, Opera 44+, and Edge 16+.
You now have an understanding of the fundamentals of CSS. This final chapter takes a step back to look at good coding practices and standards for style sheet development. Following these guidelines can help you write robust CSS code that is easy to maintain, reuse, and extend upon.
A key idea to a manageable style sheet is to avoid duplicate code. Classes help achieve this goal because they are reusable and can be combined in different ways, giving you a flexible design that is easy to evolve.
When optimizing classes for reuse, it is important to consider their size. The goal is to find the middle ground between classes that are not too broad or too narrow. Too-broad classes lead to unnecessary repetition; too-narrow classes make it difficult to change the design.
Global modifiers such as these can be very useful when just a single style property is needed. However, you should avoid combining more than a few of them because it can become difficult to adjust your design if all page items are composed of such small classes.
By organizing your style sheets, you can make it easier for yourself and other developers to quickly understand the structure of your CSS. The larger a site becomes, and the more developers are involved, the greater is the need to keep things well organized. But it is good practice to always keep your style sheets well structured, regardless of the size of the web site.
The equal signs after the section name help visually distinguish the sections from other comments. They also act as a marker that you can search for to easily traverse the sections.
Within each section, you should declare your most generic rules first, followed by rules with increasing specificity. Your elements can inherit styles, and it is easier for you to override specific styles when needed.
Keep in mind that these are only guidelines; choose a structure that works for you and aim to keep it consistent.
It is helpful to name classes and ids in a way that clarifies their intended use. This structural naming convention means that the name should describe what the class or id is used for instead of what it looks like or where it is used in the web document.
The title class could also have been written as .post.title to ensure that the .title class can be used only within a container using the .post class. However, the .post-title name helps avoid naming conflicts and does not increase specificity, so that naming convention is often preferable. Notice that the relationship between the rules is further emphasized using indentation, which can significantly improve the code’s readability.
Different browsers render some elements slightly differently, mainly because of variations in their default style sheets. To get a shared baseline, it is common to include a group of rules that normalize these browser inconsistencies and set reasonable defaults. The most popular choice for this is the GitHub Normalize.css project.1 By including these rules at the top of your style sheet (or a subset of them per your site’s requirements), you have a consistent starting point across all browsers from which you can build. The Normalize.css style sheet includes ample comments that explain each browser inconsistency that it resolves.
There are many useful debugging tools available that can significantly simplify your work as a web developer. Chrome and Firefox for instance have built-in browser support to debug CSS, HTML and JavaScript live on any web page for testing purposes. To take advantage of this you bring up the development tools window using the shortcut Ctrl+Shift+I on Windows or Cmd+Opt+I on Mac.
Alternatively, you can right-click an element on a page and select Inspect Element to bring up the same window in element inspection mode. One of the features of this window is to discover exactly which styles apply to a selected element and how the element changes if you toggle specific styles on or off. To get even more sophisticated tools for debugging and web development you can download the Developer Edition of the Firefox browser.2
It is a good idea to check that your CSS complies with the W3C standard. Improper code may cause unexpected results in how your site appears in different browsers. Moreover, having error-free code is a sign of a quality web site.
The W3C provides its own online tool for validating CSS.3 It checks a submitted page and returns any errors and warnings found on the page for you to fix. It also has a similar tool for validating HTML documents,4 which is just as important to do. To make validation even more convenient, you can download a browser plug-in that checks the page’s code for you, such as the Web Developer plug-in available on Chrome, Firefox, and Opera.5
For performance reasons, it is best to include a site’s style rules in a single external style sheet. Doing so minimizes the number of HTTP requests necessary to load the web site, while allowing the CSS file to be cached so that the visitor’s browser has to download it only once.
During development of a large site, it is often preferable to separate style rules into several more manageable CSS files. To have the best of both worlds, these development files can be combined into a single file as part of the site’s build process, using a CSS preprocessor.
As a website grows so too does its style sheet and it can quickly reach a point when the style sheet becomes difficult to overview and maintain. A way to reduce complexity of large style sheets is to use a CSS preprocessor, such as SASS.6 The preprocessor perceivably extends the CSS language with basic programming features, such as variables, loops, conditions, and functions. This allows you to better organize the style sheet and make use of reusable components to achieve greater results with less code. Note that preprocessors do not extend the CSS standard itself. The style sheets in the extended syntax are processed by a program and turned into regular CSS style sheets.
Minification is the process of removing unnecessary characters from code to reduce its size. When a CSS file is minified, whitespace characters are removed, and the rules are optimized and restructured to load more quickly. This compression can greatly reduce the size of the file, which improves site performance at the cost of code readability. Because of the reduced readability, it is preferable to work with the uncompressed style sheet and have the minification step repeated whenever the CSS file is updated. Minification can be done automatically using the CSS preprocessor mentioned earlier or manually with an online tool such as Clean CSS.7
One optimization that minification tools cannot do is to find and remove unused CSS rules. A useful Firefox plug-in that can help you perform this task is Dust-Me Selectors.8 This plug-in can test pages individually and also scan through an entire site in search of unused selectors.
Even with your code normalized and validated, there can still be some differences in the way a web page is rendered in various browsers, especially in older versions. It is therefore necessary to test your site in all the browser versions you want your site to support.
To make this testing process easier, you can use BrowserStack,9 which is an online tool for checking browser compatibility. It shows you how your site will look on different versions of the browsers you select. You can also see how your site will look on mobile devices and tablets.