← C3: The Internet | ↑Table of Contents ↑ | C4: The History of Computers → |
CSS is considered to be one of the easiest languages to learn. Its basic rules and syntax are simple, and you could start styling web pages after a single day of learning (if you're already familiar with HTML). — codecademy
If you're going to do something, do it with style. — Jason Statham
Chapter X2 described the basics of HTML, the language for specifying the look of Web pages. In creating a simple Web page, you used HTML tags to organize text into paragraphs and sections with headings and format the text to appear in bold or italics. You learned to organize text into lists and tables and finally add hyperlinks and images to complete the standard collection of page elements.
While the pages you developed in Chapter X2 may have been serviceable, they lacked style. Text elements were in black and white, and everything was left justified. In general, Web browsers rely heavily on user preferences for displaying the contents of a page. Each browser has a set of defaults (e.g., language, font, text size, color scheme) that it uses to automatically format the contents of a page. However, you can override some of these defaults and style
the display of the page to look more interesting. In this chapter, you will learn to style
a page using style
attributes and Cascading Style Sheets (CSS).
Recall from Chapter X2 that an attribute is qualifier that can be assigned to an HTML element as part of its opening tag. For example, an img
tag requires a src
attribute to specify the image file address, while an a
tag requires an href
attribute to specify the link address. All the HTML tags we have seen have an optional attribute named style that can be used to customize the display of the element.
A style
attribute is added to the opening tag of an element in the following general form:
style="PROPERTY:VALUE"
where PROPERTY is a property of that element and VALUE is the value for that property. Note that different symbols are used for attributes and properties: an attribute (such as style
) is assigned a value using an equals sign; a property is set within that attribute assignment using a colon.
A common use of a style
attribute is to change the text color of an element. By setting the color property within a style
attribute, the browser is directed to display the text inside that element in a specific color (instead of the default black). For example, text in the paragraph below would appear in a red.
<p style="color:red"> Here is some red text. </p>
Modern browsers support 140+ color names, all of which can be set to the color
property of an element within a style
attribute. Some of the most common colors are shown in FIGURE 1 below:
black | gray | lightgray | darkgray | dimgray | slategray | silver | |||||||
white | ivory | beige | seashell | linen | antiquewhite | azure | |||||||
red | darkred | crimson | salmon | firebrick | pink | hotpink | |||||||
orange | darkorange | tomato | coral | brown | tan | wheat | |||||||
yellow | lightyellow | gold | goldenrod | moccasin | khaki | peachpuff | |||||||
green | lightgreen | darkgreen | olive | seagreen | limegreen | teal | |||||||
blue | lightblue | darkblue | aqua | turquoise | royalblue | cadetblue | |||||||
purple | lavender | plum | violet | magenta | darkmagenta | indigo |
FIGURE 1. Common colors supported by Web browsers.
The HTML document in EXAMPLE 3.1 demonstrates the use of the style
attribute to change the color
of page elements. The heading will appear in blue since its opening tag has the attribute style="color:blue"
. The first second paragraph appears in red since its opening tag has the attribute style="color:red"
.
EXERCISE 3.1: Click on the "See in Browser" button to confirm that the colors appear as described. Then, add a style
attribute to the first paragraph that sets the text to the color of your choice.
In addition to changing the color of text within an element, you can change the background color by setting the background-color property within the style
attribute. For example, the following paragraph would be displayed as black text (the default color) on a yellow background.
<p style="background-color:yellow">Highlighted text.</p>
An HTML element can only have one style
element. If you want to set more than one property for an element, the properties must appear together in the same style
attribute, separated by semi-colons. For example, the following paragraph would be displayed as white text on an olive background.
<p style="color:white; background-color:olive">Highlighted and colored text.</p>
The HTML document in EXAMPLE 3.2 demonstrates the use of the style
attribute to change the color properties of paragraphs. The first paragraph will have black text (the default) on a light blue background, while the second paragraph will have white text on a gray background.
EXERCISE 3.2: Click on the "See in Browser" button to confirm that the colors appear as intended. Modify the style
attribute in the first paragraph so that text appears in purple on a light blue background.
EXERCISE 3.3: What would you expect to happen if you specify a color name that is not known to the browser? For example, you might misspell orange in style="color:orenge"
. Make this modification to one of the elements in EXAMPLE 3.2 and see how that is handled by the browser.
Designer Secrets
In general, you should be conservative in your use of color within a page. Just because you can display the page using pink text on a purple background doesn't mean that you should! Bright colors, when viewed on a computer screen, can be hard on the viewer's eyes. Bad color choices can also make the content of the page difficult to read. Plus, about 4.5% of the world's population is red/green color blind, so you will want to be careful with those colors to avoid accessibility issues.
If you want your page to have something other than the default white background, it is best to stick with subtle background colors, such as light gray or light blue. The color of text and other elements in the page should complement the background color (e.g., black text on a white or light gray background, white text on an olive background). High contrast colors (e.g., red or purple) should only be used to highlight small pieces of text for effect.
If you wanted to change the text or background color for an entire Web page, that can easily be accomplished by setting the desired property in the style
attribute of the opening body
tag. Since the body
element contains all the elements that appear in the page, setting its property (e.g., text or background color) automatically sets that property for all the enclosed elements as well. For example, the Web page in EXAMPLE 3.3 will have all its text appear in white on an olive background:
EXERCISE 3.4: Click on the "See in Browser" button to confirm that the colors appear as intended.
Modify the style
attribute in the body tag so that text appear as salmon on a seagreen background. What is the ugliest color combination you can find?
When you set the color
and/or background-color
properties in the style
attribute of a body tag, it sets the defaults for that page. However, you can still add style
attributes to individual elements to override those defaults. For example, if you placed the attribute style="color:brown"
in a heading or paragraph, it would override the default for that element and display its text in brown. Similarly, if you placed the attribute style="color:black;background-color:white"
in a heading or paragraph, it would override both defaults and display its text as black on white.
EXERCISE 3.5: Add a style
attribute to the first paragraph in EXAMPLE 3.3 so that the text in that paragraph appears in orange (but still using the default background color). Similarly, add a style
attribute to the second paragraph so that the text appears in black on a white background.
Designer Secrets
As you surf the Web, you may have noticed that some Web pages have backgrounds comprised of textured patterns or even photographic images. While this feature is rarely used (since it often makes the text on the page hard to read), it can easily be accomplished by setting the background-image
property of the style
attribute. For example, the opening body
tag
<body style="background-image:url(http://freecsc.com/Images/paper.jpg)">
would cause the image located at the specified URL to be displayed in the background of the page. In this case, the background would have a yellow, paper-like texture. Note that, if the image is too small to fill the entire browser window (which is usually the case), then the image is repeated to fill the space. Therefore simple, repetitive patterns make the best background images.
By default, all text in a Web page is displayed left-justified on the page. This means that each heading, paragraph, or line of text begins at the page's left edge. However, it is possible to align text-based elements so that they are centered or right-justified by setting the text-align property in the style
attribute. For example, the heading in EXAMPLE 3.4 will be centered in the page, since it has the attribute style="text-align:center"
. The first paragraph will be left-justified in the page, since this is the default alignment, while the second paragraph will be right-justified since it has the attribute style="text-align:right"
.
Closely related to the text-align
property, the text-indent property specifies how much the first line of a paragraph should be indented. For example, the first paragraph in EXAMPLE 3.4 has its first line indented 10 pixels, due to the attribute style="text-indent:10px"
. Subsequent lines in the paragraph are not indented — they extend all the way to the left edge of the page.
EXERCISE 3.6: Click on the "See in Browser" button to confirm that the page elements are aligned as described. Resize the window to confirm that the centered heading and right-justified paragraph adjust to match the new window size.
If you want to indent an entire paragraph, not just the first line, then the margin-left property can be set. For example, the first paragraph in EXAMPLE 3.5 has the attribute style="margin-left:20px"
. As a result, every line in that paragraph will be displayed with a 20-pixel margin to the left. There is a corresponding margin-right property as well, in case you want indentation from the right. For example, the second paragraph in EXAMPLE 3.5 has both properties set via style="margin-left:20px;margin-right:20px"
. As a result, it will be displayed with a 20-pixel margin on both the left and right. In cases such as this, where the margin is the same on the left and right, there is a shortcut that can be used. Setting the margin property has the effect of setting both the margin-left
and margin-right
properties at the same time. For example, the third paragraph in EXAMPLE 3.5 has the attribute style="margin:20px"
, which has the same effect as in the second paragraph.
EXERCISE 3.7: Add a paragraph to the document in EXAMPLE 3.5 that is right justified. Include a few break tags to ensure that the paragraph spans several lines, all of which are right-justified.
EXERCISE 3.8: Add margin-right:20px
to the style
attribute of your new paragraph. Confirm that the paragraph now has a margin on the right side of the page.
In printing terminology, a font defines a particular typeface (such as Times New Roman, Courier, or Arial) and size (such as 11 pt. or 12 pt.) used to display text. Each Web browser has a default font for displaying text in a page. For example, in Google Chrome, the default font is typically Times New Roman with size 16 pixels. Using the style
attribute, a Web developer can assert control and override the default typeface and/or size for fonts in the page.
To set the font typeface for an element, the font-family property must be set in the style
attribute. For example, the first paragraph below would be displayed using the Helvetica typeface, while the second would be displayed using the Garamond typeface:
<p style="font-family:Helvetica">This text appears in Helvetica.</p>
<p style="font-family:Garamond">This text appears in Garamond.</p>
The name font-family
refers to the fact that typefaces are grouped into families that share basic characteristics: serif typefaces utilize small lines or strokes to embellish characters, sans serif typefaces are simpler with no embellishments, and monospace typefaces have uniform-width characters. Examples of font typefaces that are supported by all browsers are listed in FIGURE 2.
Serif | Sans serif | Monospace |
---|---|---|
Times New Roman | Arial | Courier |
Garamond | Helvetica | Courier New |
Georgia | Verdana | Menlo |
Palatino | Tahoma | Monaco |
FIGURE 2. Font typefaces supported by all Web browsers.
In EXAMPLE 3.6 below, the attribute style="font-family:Arial"
sets the default font for the page. However, as we have seen before, a default property can be overridden within a specific element. The heading at the top of the page has the attribute style="font-family:Menlo"
, so it will appear in a different font.
EXERCISE 3.9: Change the style
attribute in the body tag from EXAMPLE 3.6 to style="font-family:Palatino"
. Click on the "See in Browser" button to confirm that the paragraphs now appear in this new font typeface.
EXERCISE 3.10: Replace Palatino
in the style
attribute with a misspelling, such as Paladino
. Since this typeface is not known by the browser, it will ignore the property setting and fall back on the default font typeface. Click on the "See in Browser" button to confirm this.
In addition to the typeface, you can also change the size of a font using the font-size property. The value of a font-size property can be absolute, as in a specific number of pixels, or relative, as in a percentage of the default text size. Since the user always has the option of shrinking or expanding font sizes to meet their preference, the more useful approach is to specify a percentage. For example, the second paragraph in EXAMPLE 3.7 displays its text at half the size of the default text. The third paragraph displays its text at twice the default size (using the font typeface Garamond).
EXERCISE 3.11: Add a paragraph to the document from EXAMPLE 3.7 that uses the Courier New
font typeface, with a font size of 80% of the default size.
In addition to the paragraph element, HTML provides two additional text-based elements that are extremely useful with respect to styling. A span identifies a small grouping of text, typically a word or phrase. Because of their small size, spans are not set off from surrounding text like a paragraph. This makes them useful for embedding styled text within a sentence or paragraph. For example, the first paragraph in EXAMPLE 3.8 contains a span
element that contains the word "colorful." Since this span has the attribute style="color:red"
, the contained word will appear in red while the text around it is the default black. A second span in the paragraph contains the phrase "words or phrases". Since this span has the attribute style="font-size:150%"
, the contained phrase will appear 50% larger than the surrounding text.
While a span is a grouping of text smaller than a paragraph, a div (short for a page division) is a larger grouping of text. A div
can be used to group multiple HTML elements into a single unit, such as two or more paragraphs. By placing multiple paragraphs inside a div
element, you can easily format them as one by setting the style
properties of the div
. For example, the second and third paragraphs in EXAMPLE 3.8 are enclosed in a div
element. Since that div
has attribute style="text-align:center;color:blue"
, both of the enclosed paragraphs will appear centered with blue text.
EXERCISE 3.12: Modify the HTML document from EXAMPLE 3.8 so that it displays the following two paragraphs, both centered in the page. Note that the word "zyzzyva" appears in red and the name "Thomas Lincoln Casey Jr." appears in the Menlo font.
A zyzzyva is a South American weevil,
often found on or near palm trees.
It was first described by Thomas Lincoln Casey Jr. in 1922.
(source: Wikipedia)
The text-align
property, when set in a style
attribute, is effective for aligning headings, paragraphs and divs. Unfortunately, it does not work correctly for images, tables and lists. In the case of an image, adding the style="text-align:center"
attribute to an img
tag has no effect since an image is not text-based. Adding style="text-align:center"
to a table
tag does have an effect, but the table itself is not centered, just the text inside the table. Similarly, adding style="text-align:center"
to a list element will center the list items, but the numbers (for an ordered list) or bullets (for an unordered list) still appear at the left edge of the page.
To center or right-justify an image, it suffices to place the img
tag inside a div
element, and then set the desired text-align
property in the style
attribute of the div
. For example, the following element would center an image:
<div style="text-align:center"> <img src="http://freecsc.com/Images/happy.png" alt="Happy Face"> </div>
To align a table, i.e., make the table itself appear in the center of the page, you must set its margin property (introduced earlier in the context of indenting paragraphs). The style
attribute style="margin:auto"
specifies that the margins should be automatically set to center the table:
<table style="margin:auto"> . . . </table>
Finally, to align a list requires a combination of these two approaches: you must embed that list in a div
element that is centered, then add a style
attribute to the table itself to make sure the list contents are left-justified within the centered div
. For example:
<div style="text-align:center"> <ol style="display:inline-block; text-align:left"> . . . </ol> </div>
The HTML document in EXAMPLE 3.9 has three centered elements: an image (with a caption below it), a table, and an ordered list.
EXERCISE 3.13: Make the following modifications to the HTML document in EXAMPLE 3.9:
style
attribute in the first div
element to style="text-align:right"
. This should have the effect of right-justifying both the image and the paragraph below it.There are additional style
properties that can be added to tables and lists to alter their appearance. All the text-formatting properties that we discussed earlier in the chapter (e.g., color, background-color, font-family) can be set in an opening table
tag to style
the text that appears in the table. You can also set those properties in individual tr
or td
tags to only affect those rows and column entries.
You can add a solid border around a table or individual rows or column entries by setting the border-style
property to solid
. For example, the following table would have a solid border around the outside. In addition, the first row would have a light gray background, and the second row would have a dark gray background.
<table style="border-style:solid"> <tr style="background-color:lightgray"> <td>one</td> <td>two</td> </tr> <tr style="background-color:darkgray"> <td>three</td> <td>four</td> </tr> </table>
EXERCISE 3.12: Make the following modifications to the table element in EXAMPLE 3.5:
Creighton Bluejays
should appear as white text on a royalblue background.UConn Huskies
should appear as white text on a darkblue background.By default, ordered list items are displayed with numbers to the left, while unordered lists are displayed with circular bullets. These defaults can be easily changed by setting the list-style
property. For an unordered list, adding style="list-style:square"
to the <ul>
tag will replace the circles with squares. For an ordered list, style="list-style:lower-alpha"
specifies lower-case letters (a, b, c, ...), style="list-style:upper-alpha"
specifies upper-case letters (A, B, C, ...), style="list-style:lower-roman"
specifies lower-case roman numerals (i, ii, iii, iv, ...), and style="list-style:upper-roman"
specifies upper-case roman numerals (I, II, III, IV, ...),
EXERCISE 3.13: Modify the list in EXAMPLE 3.9 so that the items appear with upper-case letters instead of numbers. Then, add an unordered list to the document with at least four items. When displayed in a page, the items should have squares to the left.
This book is not intended to prepare you to be a professional Web site designer. Instead, it focuses on the basics of HTML that will enable you to design and create simple Web pages that can be made dynamic and interactive through programming. If you were interested in developing a professional looking Web site, you probably would not rely on creating HTML documents from scratch using a text editor. There are a number of tools and development environments that make designing and creating Web pages simpler. Many provide templates for Web pages that you can download and modify with your own content. If you were developing a Web site with multiple pages, you would also be concerned about a consistent look and feel to the pages. Having pages on a site that don't share the same color scheme, font family, or heading alignment not only looks disjointed but can also hinder the user's experience.
Fortunately, HTML provides a straight-forward mechanism for applying consistent style across multiple files: cascading style sheets. A cascading style sheet (CSS) is a text file that lists page elements and their desired style
properties. For example, the following text specifies that the body of the page will have white text on an olive background, and every h1
element will be centered.
body { background-color:olive; color:white } h1 { text-align:center }
If this text were saved in a file named mystyle.css
, it could then be linked into any HTML document by placing the following tag in the head of the page:
<link rel="stylesheet" href="mystyle.css">
As a result, every page that linked to this cascading style sheet would set the style
properties consistently. That is, all pages linking to mystyle.css
would have an olive background, white text, and centered h1 elements. If you ever changed your mind about the appearance of your pages, say you decided you liked a seagreen background better than an olive one, you would simply need to update the style
attribute in mystyle.css
. Each page that linked to this file would thus be updated automatically and in a consistent manner.
EXAMPLE 3.10 below shows two text boxes. The top one represents the CSS file mystyle.css
, which contains style
properties for three page elements, body
, h1
and p
. Since the HTML document in the lower box links to this CSS file, the style
definitions will be applied to the page by the browser. Not only does this make it much easier to change styling consistently, but it also makes the body of the page much cleaner as style
attributes are no longer needed in the elements themselves.
mystyle.css
EXERCISE 3.14: Modify the property lists in the mystyle.css
box from EXAMPLE 3.10 so that the background color of the body is set to seagreen instead of olive. Next, modify the h1
list so that, in addition to being centered, all h1
headings appear in brown text. View the page after making your modifications to confirm that the new style
properties are applied.
The CSS properties defined in EXAMPLE 3.6 are universal, in the sense that they apply to all elements of a given type. For example, every h1
heading in a page that is linked to this CSS file will be centered. While that contributes to a consistent style across pages, there are times when you want to be more selective. For example, you might want only the first h1
element in a page to be centered.
You can specify selective styling in a CSS file by defining a distinct class of an element. The class is identified by placing a period followed by the class identifier after the element name. For example, the CSS entry below defines a class of h1
elements named center
and sets the text-align
property for that class.
h1.center { text-align:center }
Within the body of a page, an element identifies itself as belonging to a class using the class
attribute. For example, the first h1
heading below will be centered since it belongs to the h1.center
class, but the second h1
will still use the default alignment.
<h1 class="center">Centered Heading</h1> <h1>Left-justified (Default) Heading</h1>
EXERCISE 3.15: Change the h1
CSS entry in the mysstyle.css
box in EXAMPLE 3.10 to h1.center
. Next add, another h1
element to the page, this time with the class="center"
attribute. Confirm that the new heading is centered, but the existing one is not (since it is not designated as being in the center
class).
This chapter introduced various techniques for styling a Web page. By inserting a style
attribute in HTML elements, those elements could have properties such as alignment, color and size adjusted to produce a more attractive appearance. For example, the page honoring Sir Tim Berners-Lee (which appeared at the end of chapter X2) could be styled as follows:
The HTML document in EXAMPLE 3.11 produces this page.
There are several details to note about this document.
style="color:white;background-color:lightsteelblue"
to the opening body
tag.h1
and img
element into a page division (div
) that has attribute style="text-align:center"
. style="text-indent:10px"
in each of the opening p
tags. style="border-style:solid"
in each of the opening td
tags. style="font-family:Courier New;font-size:80%""
to the opening p
tag. style
attribute can be added to an element to customize the appearance of that element. The general form of a style
attribute assignment is: style="PROPERTY:VALUE"
color
property in a style
attribute will change the color of text in that element. Likewise, setting the background-color
property changes the background color of that element.style
attribute, but multiple properties can be set within that element, separated by semi-colons.style
attribute to the <body>
tag will set the specified properties of all elements contained within the body. text-align
property in the style
attribute of a text-based element (e.g., a heading, paragraph, span or div) will change the alignment of that element. text-indent
property in the style
attribute of a text-based element will indent the first line by a specified amount. Likewise, the margin
, margin-left
and margin-right
properties change the margins.font-family
property in the style
attribute of a text-based element will change the typeface of text in that element. Likewise, the font-size
property changes the font size.span
element is a small grouping of text, typically a word or phrase. A div
element is a large grouping of elements, such as multiple paragraphs that are to be styled as a unit.style
properties can be used to center images, lists and tables, add borders to tables, and alter the bullets and numbers in lists. link
element and so yield consistent styling across the pages.
PROJECT 3.A: Download a copy of the HTML document in EXAMPLE 3.11 (by clicking on the Download
link) and save it on your computer. Using a text editor of your choice, make the following modifications/additions to your document:
Dave Reed
in the comment element, add (modified by X)
, where X
is your name.style
attribute in the body tag so that the default font typeface for the page is Helvetica
.
PROJECT 3.B: Add the following content to the personal Web page you created in PROJECT 2.C:
PROJECT 3.C: If you have access to a Web server account, upload your modified page (and any accompanying image files) to that server. This will make your page accessible to the world. As before, it is important that you name your page index.html
since this is the default name for the home page on a server.
PROJECT 3.D: Create a separate file named mystyle.css
. Within this Cascading Style Sheet, define the following element properties:
body
element should have property settings that specify the text color, background-color and font-family for the entire page.table
element should have a property setting that places a solid border around the table.ol
and ul
elements should have property settings that specify the list item symbols to be displayed.
h1
element should have a class named center
that centers elements of that class.p
element should have a class named marginalized
that displays that paragraph with left and right margins of 20px.p
element should have another class named right
that right-justifies it on the page.Once this is done, add a link
tag to your Web page to link to this CSS file. Then, remove the style
attributes from elements that are no longer needed (due to the CSS file). Likewise, replace style
attributes with the relevant class
attributes when possible.
PROJECT 3.E: Similar to PROJECT 3.B, upload your CSS file and your modified Web page to the server if you are able. Note: since your Web page uses a local address (mystyle.css
) to link to the CSS file, you will need to upload both documents to the same folder on the server.
← C3: The Internet | ↑Table of Contents ↑ | C4: The History of Computers → |