By Christopher Schmitt
Book Price: $44.99 USD
£31.99 GBP
PDF Price: $35.99
Cover | Table of Contents | Colophon
<html> <head> <title>CSS Cookbook</title> <head> <body> <p>Hello, world!</p> </body> </html>
<p style="font-family: sans-serif;">Hello, world!</p>
font-size, using the following example that you see in Figure 1-3:<p style="font-size: 150%">Hello, world!</p>
body tags and save
the file as cookbook.html (see Figure 1-4):<html> <head> <title>CSS Cookbook</title> </head> <body> <h1>Title of Page</h1> <p>This is a sample paragraph with a <a href="http://csscookbook.com">link</a>.</p> </body> </html>
<html> <head> <title>CSS Cookbook</title> <head> <body> <p>Hello, world!</p> </body> </html>
<p style="font-family: sans-serif;">Hello, world!</p>
font-size, using the following example that you see in Figure 1-3:<p style="font-size: 150%">Hello, world!</p>
body tags and save
the file as cookbook.html (see Figure 1-4):<html> <head> <title>CSS Cookbook</title> </head> <body> <h1>Title of Page</h1> <p>This is a sample paragraph with a <a href="http://csscookbook.com">link</a>.</p> </body> </html>
<html>
<head>
<title>CSS Cookbook</title>
<style type="text/css">
<!--
body {
font-family: verdana, arial, sans-serif;
}
h1 {
font-size: 120%;
}
a {
text-decoration: none;
}
p {
font-size: 90%;
}
-->
</style>
</head>
<body>
<h1>Title of Page</h1>
<p>This is a sample paragraph with a
<a href="http://csscookbook.com">link</a>.</p>
</body>
</html>
h1 element in the web
page to 120% of the default size:h1 {
font-size: 120%;
}| Selector | Property | Value | Result |
|---|---|---|---|
h1 |
<html>
<head>
<title>CSS Cookbook</title>
<style type="text/css">
<!--
* {
font-family: verdana, arial, sans-serif;
}
h1 {
font-size: 120%;
}
#navigation {
border: 1px solid black;
padding: 40px;
}
li a {
text-decoration: none;
}
p {
font-size: 90%;
}
-->
</style>
</head>
<body>
<h1>Title of Page</h1>
<p>This is a sample paragraph with a
<a href="http://csscookbook.com">link</a>. Lorem ipsum dolor sit amet,
consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut
laoreet dolore magna <em class="warning">aliquam erat volutpat</em>. Ut
wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit
lobortis nisl ut aliquip ex ea commodo consequat.<p>
<ul id="navigation">
<li><a href="http://csscookbook.com">Apples</a></li>
<li><a href="http://csscookbook.com">Bananas</a></li>
<li><a href="http://csscookbook.com">Cherries</a></li>
</ul>
</body>
</html>
class selectors when you need to apply a style
multiple times within a document and ID selectors for
one-time only appearances of a style within a document.#banner, #sub_banner, #nav1, #nav2, #footer, and #content are ID selectors and
.title and .content are
class selectors.body {
margin: 0;
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: .75em;
padding: 0;
}
#banner {
margin-top: 0;
margin-bottom: 0;
background-color: #900;
border-bottom: solid 1px #000;
padding: 5px 5px 5px 10px;
line-height: 75%;
color: #fff;
}
#sub_banner {
background-color: #ccc;
border-bottom: solid 1px #999;
font-size: .8em;
font-style: italic;
padding: 3px 0 3px 10px;
}
#content {
position: absolute;
margin-left: 18%;
width: 40%;
top: 100px;
padding: 5px;
}
#nav1 {
position: absolute;
width: 30%;
left: 60%;
top: 100px;
padding: 5px;
}
#nav2 {
position: absolute;
padding: 5px 5px 5px 10px;
top: 100px;
width: 15%;
}
#footer {
text-align: center;
padding-top: 7em;
}
.warning {
font-weight: bold;
color: red;
}
.title {
font-size: 120%;
}
.content {
font-family: Verdana, Arial, sans-serif;
margin-left: 20px;
margin-right: 20px;
}
.footer {
font-size: 75%;
}ID and class selectors into the HTML code:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>CSS Cookbook</title> <link href="1-2.css" rel="stylesheet" type="text/css" /> </head> <body> <div id="header"> <h1>CSS Collection</h1> <h2>Showcase of CSS Web Sites</h2> </div> <div id="content"> <h3>Content Page Title</h3> <p class="title">Content Item Title</p> <p class="content">Content goes here.</p> </div> <div id="navigation"> <h3>List Stuff</h3> <a href="http://csscookbook.com/">Submit a site</a><br /> <a href="http://csscookbook.com/">CSS resources</a><br /> <a href="http://csscookbook.com/">RSS</a><br /> <h3>CSS Cookbook Stuff</h3> <a href="http://csscookbook.com/">Home</a><br /> <a href="http://csscookbook.com/">About</a><br /> <a href="http://csscookbook.com/">Blog</a><br /> <a href="http://csscookbook.com/">Services</a><br /> </div> <div id="blipverts"> <h3>Ads go here.</h3> </div> <div id="siteinfo"> <p class="footer">Copyright 2006</p> </div> </body> </html>
color, font-family, font-size, and text-decoration. Properties fall
between the brackets and their values immediately follow as shown here in a generic example:selector {
property: value;
}li {
list-style-type: square;
}li appears in the document, the bullet appears
as a square rather than a traditional bullet.color property means the element’s
color will change, but not what color. That’s the job for value. Table 1-2 showcases a few more properties,
values, and what they do.| Property | Value | Result |
|---|---|---|
font-weight |
bold | Adds bold to text |
border-color | Color name or color hexadecimal HTML value (e.g., #000000 for black and #ffffff for white) | Adds color to border |
border-style |
solid dotted dashed double | Adds solid lineAdds dotted lineAdds dashed lineAdds two lines |
text-align |
left center right justify | Aligns text to the leftAligns text in the centerAligns text to the rightFully expands text from left to right |
http://www.w3.org/TR/CSS21/propidx.html; HTML Dog
CSS Properties at http://www.htmldog.com/reference/cssproperties/; a detailed look at the border
property in Recipe 4.4; a complete listing of CSS
properties in Appendix B.p or div element,
contains a top, right, bottom, and left edge. These sides of block elements are composed of
three layers surrounding the content. So, therefore each block element contains four sections:content
padding
border
margin
margin value is 0, which lines up with the edge of the border. A
border with a value of 0 lines up with the padding edge. padding value of 0 lines flush against the content. Values above 0 expand the boxes. Take a look at Figure 1-21 to see views of a box model.
height and width properties.div {
height: 150px;
width: 150px;
}
<div> <li>Moby Dick</li> <li>The Red Badge of Courage</li> <li>The Catcher in the Rye</li> </div>
div {
border: thin solid #000000;
height: 150px;
width: 150px;
}
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" " http://www.w3.org/TR/1999/REC-html401-19991224/frameset.dtd">
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html> <head> <title>XHTML DTD</title> </head> <body> <p>XHTML requires having a DTD in every document otherwise it won't pass muster with the validators.</p> </body> </html>
html element to ensure your markup and CSS are standards compliant, and that
browsers handle the pages based on the appropriate DTDs.head.<style>
<!--
#header {
width: 100%;
height: 100px;
font-size: 150%
}
#content {
font-family: verdana, arial, sans-serif;
margin-left: 20px;
margin-right: 20px
}
.title {
font-size: 120%
}
-->
</style>/* CSS Document */
h1 {
font-size: 150%;
}
h2 {
font-size: 120%;
}
p {
font-family: Verdana, Arial, Helvetica, sans-serif;
}style element is not present in the
external style sheet. Also, HTML comments are not used in external style sheets.head tags to link to the external style sheet that contains the above styles:<link href="screen.css" rel="stylesheet" type="text/css" media="screen" />
font in that they
appear with the markup they affect.<h1 style="font-family: verdana, arial, sans-serif; font-size: 150%; color: blue;">Page Title</h1> <p style="font-family: sans-serif; font-size: 90%;">Hello, world!</p>
link tag.style
tags.link element with the media type in the web page to link to these
styles. Another option is to use the @media rule.body {
font: 10pt times, georgia, serif;
line-height: 120%
}body {
font: 12pt verdana, arial, sans-serif;
line-height: 120%
} body {
font: 14pt;
line-height: 120%
} head section. Each link has a different media type:<link rel="stylesheet" type="text/css" href="/css/print.css" media="print" /> <link rel="stylesheet" type="text/css" href="/css/screen.css" media="screen" /> <link rel="stylesheet" type="text/css" href="/css/projection.css" media="projection" />
@media rule instead to specific the
different
media rules within the same style sheet:<style type="text/css">
<!--
@media print {
body { font: 10pt times, georgia, serif }
}
@media screen {
body { font: 12pt verdana, arial, sans-serif}
}
@media projection {
body { font-size: 14pt }
}
@media screen, print, projection {
body { line-height: 120% }
}
-->
</style>@media rule allows you to put all the media in one
style sheet./* and */
anywhere in the styles to show the start and end of a comment./* This is a comment */
a {
text-decoration: none;
}
/* This is also a comment */
h1, h2 {
font-size: 100%;
color: #666666;
}/* and */./*
a {
text-decoration: none;
}
*/text-decoration won’t take affect
with the comments taking it out of circulation. Unless there are other styles for a, the underline appears under links until the comment is
removed.http://www.w3.org/TR/CSS21/syndata.html#comments./* Typography & Colors ------------------------------------ */ [css code ] /* Structure ------------------------------------ */ [css code ] /* Headers ------------------------------------ */ [css code ] /* Images ------------------------------------ */ [css code ] /* Lists ------------------------------------ */ [css code ] /* Form Elements ------------------------------------ */ [css code ] /* Comments ------------------------------------ */ [css code ] /* Sidebar ------------------------------------ */ [css code ] /* Common Elements ------------------------------------ */ [css code ]
http://www.stopdesign.com/log/2005/05/03/css-tip-flags.html;
Recipe 1.11 on how to organize style sheet files;
and Recipe 11.7 on how to set up an intelligent
hacking system.css directory./_assets/css/print.css /_assets/css/screen.css
/_assets/css/layout.css /_assets/css/color-imagery.css /_assets/css/type.css
head
element:<link rel="stylesheet" type="text/css" media="print" href="/_assets/css/print.css" /> <link rel="stylesheet" type="text/css" media="screen" href="/_assets/css/screen.css" />
screen.css would include
methods for importing the separate CSS files that dictate the design for screen delivery.
Here’s what the screen.css would look like in this solution:/* import style sheets */
@import url("/_assets/css/layout.css");
@import url("color-imagery.css");
@import url("type.css");<h3>Shorthand Property</h3> <p>Combine properties with shorthand and save time, typing, and a few bytes. Your style sheets will also be easier to read.</p>
font property instead of
three: font-style, font-size, and font-family:h3 {
font: italic 18pt verdana, arial, sans-serif;
}
p {
border: 2pt solid black;
}border property is a shorthand property, which
combines three properties into one. The border property can
cover the values from the following properties:font property is a shorthand property, which
combines three properties into one. The font property can
cover the values from the following properties:font-family and font-size/line
height. With font-family, enter the fonts in the
order you wish for them to have priority and use a comma between each.font-size and line height, then separate their values with a forward slash:h3 {
font: italic 18pt/20pt verdana, arial, sans-serif
}| Property | Values | Example |
|---|---|---|
background
|
background-color background-image background-repeat background-attachment background-position |
background: url(book.gif) #999 no-repeat top; |
border
border-left
border-right
border-top
border-bottom
|
border-width border-style border-color |
link element with a title and link it to the alternate style sheets. The title lets the user see what options are available when viewing the list of
available styles. In Firefox, click View→Page Styles to see the list.<link href="default.css" rel="stylesheet" title="default styles" type="text/css" media="screen" /> <link href="green.css" rel="stylesheet" title="green style" type="text/css" media="screen" /> <link href="blue.css" rel="stylesheet" title="blue style" type="text/css" media="screen" />
link element with a
title (see Figure 1-34).
http://www.alistapart.com/articles/bodyswitchers; and Amit Ghaste’s CSS Style
Switcher tutorial at http://ghaste.com/pubs/styleswitcher.html.
.leftFloat {
float: left
}
.rightFloat {
float: right
}<img src="csscookbook.gif" class="leftFloat" alt="cover" /> <p>This is the book cover for the <em>CSS Cookbook</em>.</p> <img src="csscookbook.gif" class="rightFloat" alt="cover" /> <p>This is the book cover for the <em>CSS Cookbook</em>.</p>
align attribute with the img element to move images to the side with text wrapping. W3C deprecated align and now recommends using float instead.p {
clear: left;
}
clear property tells the paragraph to appear after
the end of the image flow. At the second img, the clear properties push the image down to the first line after the
previous ends. Instead of lining up with the second p
element, the image waits for a new line before showing up.http://www.w3.org/TR/CSS21/visuren.html#floats; Chapter 9 provides three recipes on using
float with page columns; and Eric Meyer’s CSS/edge covers
floats at http://meyerweb.com/eric/css/edge/.position property with the absolute value in the style sheet. Also use bottom, left, or both properties to indicate
where to position an element:.absolute {
position: absolute;
bottom: 50px;
left: 100px;
} absolute places the content
out of the natural flow of the page layout and puts it exactly where the
CSS properties tell it to go within the current box or window. The sample code used in the
solution tells the browser to position the element with the absolute class exactly 100 pixels from the left and 50 pixels off of the bottom edge
of the the browser's viewport.
div that
encompasses the content by adding the class attribute and
the absolute value and take a look at Figure 1-38:
<div class="absolute"> <img src="csscookbook.gif" alt="cover" /> <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat... </p> </div>
right and bottom properties for changing the absolute position. Bottom represents the bottom of the window not matter how big or small you make the
window. Beware that right and bottom aren’t supported in Internet Explorer 5 and Netscape 6.0.top, left or both properties to
indicate where to position an element..relative {
position: relative;
top: 100px;
left: 20px;
}
p elements would be if
left alone. The code tells the browser to position the paragraph 100 pixels down from the top
and 20 pixels over from the left edge of the original paragraph’s position instead of from the
edge of the window. With absolute, the content is placed
exactly where the properties tell it to go from the edges in the current box.http://www.w3.org/TR/CSS21/visuren.html#relative-positioning and W3Schools
tutorial on positioning at http://www.w3schools.com/css/css_positioning.asp.
font element. In older
versions of Dreamweaver, the code would look like the following:
<font face="georgia, times new roman, serif" color="#ff0000" size="2">This is text.</font>
http://www.adobe.com/products/dreamweaver/bestpractices/css/
<font>. In FrontPage, the code would look like the following:<font face="georgia, times new roman, serif" color="#ff0000" size="2">This is text.</font>
<font face="georgia, times new roman, serif" color="#ff0000" size="2"><font face="times new roman, serif">This is text.</font></font>
font tags to set the
color, size, and style of text on different parts of a web page:
<font face="Verdana, Arial, sans-serif" size="+1" color="blue"> Hello, World! </font>
font tags across many, many pages
resulted in time-consuming updates, inflated the overall file size of the web document, and
increased the likelihood that errors would occur in the markup. CSS helps to eliminate these
design and maintenance problems.p
element:<p>Hello, World!</p>
<style type="text/css" media="all">
p {
color: blue;
font-size: small;
font-family: Verdana, Arial, sans-serif;
}
</style>font-family
property:body {
font-family: Georgia, Times, "Times New Roman", serif;
}font-family
property. If the browser can’t find the first font on the list, it tries to find the next font,
and so on, until it finds a font.font tags to set the
color, size, and style of text on different parts of a web page:
<font face="Verdana, Arial, sans-serif" size="+1" color="blue"> Hello, World! </font>
font tags across many, many pages
resulted in time-consuming updates, inflated the overall file size of the web document, and
increased the likelihood that errors would occur in the markup. CSS helps to eliminate these
design and maintenance problems.p
element:<p>Hello, World!</p>
<style type="text/css" media="all">
p {
color: blue;
font-size: small;
font-family: Verdana, Arial, sans-serif;
}
</style>font-family
property:body {
font-family: Georgia, Times, "Times New Roman", serif;
}font-family
property. If the browser can’t find the first font on the list, it tries to find the next font,
and so on, until