div.m h3 { text-align: right; }
then this rule applies to all h3 elements within
the start and end tags:
<div class="m"> <h3> ... </h3> </div>You have to be careful not to confuse this notation with the following which has a similar appearance:
div.m, h3 { text-align: right; }
which has the effect described in the previous section.
Here is my list <ul> <li>AAAAAA</li> <li>BBBBBB</li> <li>CCCCCC</li> <li>DDDDDD</li> <li>EEEEEE</li> <li>FFFFFF</li> </ul>which has the appearance:
li { margin-top: 20px; margin-bottom: 20px; }
then all li elements in the entire document
would be affected. We
can avoid such general behavior by creating the rule:
ul.big li { margin-top: 20px; margin-bottom: 20px; }
and then rewrite the list as:
Here is my list <ul class="big"> <li>AAAAAA</li> <li>BBBBBB</li> <li>CCCCCC</li> <li>DDDDDD</li> <li>EEEEEE</li> <li>FFFFFF</li> </ul>which has the desired behavior:
| 1990 | 2000 | type |
|---|---|---|
| 5.31 | 4.2 | percent increase |
| $30,333 | $46,576 |
hardware and software |
<table class="special" summary="" border="1">in conjunction with the rules:
table.special {
width: 400px;
}
table.special th {
text-align: left;
}
table.special td {
padding: 0px 10px 0px 10px;
vertical-align: bottom;
}
we get
| 1990 | 2000 | type |
|---|---|---|
| 5.31 | 4.2 | percent increase |
| $30,333 | $46,576 |
hardware and software |
Suppose that I want to apply a set of style rules to multiple documents in my website. Instead of having to embed these into style sections of multiple documents, I can have the documents access the rules as an external file, called a stylesheet. A stylesheet is simply a file consisting of style rules only. Although not enforced, stylesheets conventionally use the .css extension.
A stylesheet can be accessed within an HTML document either by using the link element or by use of an @import statement in the beginning of the style section. For example, suppose the HTML file mydoc.html wants to import the rules in the stylesheet mystyle.css. Assuming that the files are at the same directory level, they would appear like this:| mydoc.html | mystyle.css | |
|---|---|---|
<html>
<head>
<title></title>
<link rel="stylesheet"
href="mystyle.css" />
<style>
/* other local style rules */
</style>
</head>
or
<html>
<head>
<title></title>
<style>
@import url(mystyle.css);
/* other local style rules */
</style>
</head>
|
h2 { text-align:center; }
h3 { color:red; }
|
This multiple-delivery mechanism is key to the notion of cascading style sheets. In Notes 8 we discussed how conflicting applicable properties are resolved.
Absolute units in inches cm centimeters mm millimeters pt point = 1/72 of an inch pc pica = 12 pt
Relative units px pixel size (on display monitor) % percentage relative to base font em width of capital "M" in current font ex height of lower case "x" in current font
If no unit is given the default is pixels (px).
This property allows you to choose a font to be used for your choice of HTML elements. Generally it is better to choose one or two fonts and use them consistently throughout a web site in order to present a certain continuity to the user. Keep in mind that if you choose some unusual font a browser may not have it and will print a default font. If you're trying to create some artistic effect by using many fonts, then it's better to create a graphical image.
The font-family property selects the font. This can be a single font or a comma-separated list of fonts in case a system doesn't support the first choices. Examples of common fonts used are Times, Courier, Helvetica, Arial, Verdana. It's also possible to use generic fonts such as Monospace, Serif, Sans-Serif. Here are some examples:<div style="font-family: Times"> Times: The quick brown fox jumps over the lazy dog.</div> <div style="font-family: Arial"> Arial: The quick brown fox jumps over the lazy dogs.</div> <div style="font-family: Verdana"> Verdana: The quick brown fox jumps over the lazy dog.</div> <div style="font-family: Serif"> Serif: The quick brown fox jumps over the lazy dog.</div> <div style="font-family: Sans-serif"> Sans-serif: The quick brown fox jumps over the lazy dog.</div> <div style="font-family: Monospace"> Monospace: The quick brown fox jumps over the lazy dog.</div>and here is the result:
As mentioned above, it is common to allow multiple font choices, separated by commas. The system tries to satisfy the choices in the order presented and, if none exist, it uses the system default font. For example, although the verdana font is common on Microsoft platforms, it is not so on Linux. Thus it would be better to make a font choice like this:Times: The quick brown fox jumps over the lazy dog.Arial: The quick brown fox jumps over the lazy dog.Verdana: The quick brown fox jumps over the lazy dog.Serif: The quick brown fox jumps over the lazy dog.Sans-serif: The quick brown fox jumps over the lazy dog.Monospace: The quick brown fox jumps over the lazy dog.
font-family: verdana, sans-serif;so that sans-serif could be used as a fallback in case verdana isn't present.
xx-small, x-small, small, medium, large, x-large, xx-largeas in this example
<div style="font-size: xx-small">using font-size: xx-small </div> <div style="font-size: x-small">using font-size: x-small </div> <div style="font-size: small">using font-size: small </div> <div style="font-size: medium">using font-size: medim </div> <div style="font-size: large">using font-size: large </div> <div style="font-size: x-large">using font-size: x-large </div> <div style="font-size: xx-large">using font-size: xx-large </div>which looks like this:
using font-size: xx-smallusing font-size: x-smallusing font-size: smallusing font-size: medimusing font-size: largeusing font-size: x-largeusing font-size: xx-large
<b><i>a special section of text</i></b>by using the style rule:
.special {
font-weight:bold;
font-style:italic;
}
applied to
<span class="special">a special section of text</span>Although the former rendition is simpler, the latter has the advantage of generality.
<div style="color:red; background-color:yellow; font-size:x-large"> Outside child elements <table border="1"> <tr><td>Inside table</td></tr> </table> <p>Inside paragraph</p> </div>which looks like this:
| Inside table |
Inside paragraph
h3 { text-align:center; color: red; }
div.big { width: 200; height: 200; padding: 10px; margin: 10px; }
Then the following element:
<h3 style="color:green">Some header</h3>retains the center text alignment, but sets the foreground color to green instead of red. Similarly the following element:
<div class="big" style="margin:0px">Some block</div>retains all the properties of div.big except for the margin being reset to 0.
h2 { color: red; font-family: monospace; }
h2 { color: magenta; }
then
the following h2 element
<h2>Testing</h2>retains the font-family property set be the earlier one, but the color will be magenta, not red.
Normally this situation occurs when the earlier rule is introduced through an external style sheet and is overridden in the particular document.
.x { color: red; }
h2 { color: magenta; }
Then the following h2 element will have
color red,
not magenta:
<h2 class="x">Testing</h2>despite the fact that the h2 rule is later.
h2.x { color: blue; }
.x { color: red; }
Then the following h2 element will have
color blue,
not red:
<h2 class="x">Testing</h2>despite the fact that the ".x" rule is later.
<center> <table border="1"> <tr><td>table<br />in a center element</td></tr> </table> </center>
| table in a center element |
<table align="center" border="1">
<tr><td>table<br />with align="center"</td></tr>
</table>
| table with align="center" |
<div style="text-align:center" >
<table border="1">
<tr><td>
center this table<br />
in div with text-align:center
</td></tr>
</table>
</div>
|
center this table in div with text-align:center |
<table style="margin:auto" border="1">
<tr><td>
center this table<br />
with margin:auto style setting
</td></tr>
</table>
|
center this table with margin:auto style setting |
<div style="text-align:center" > <table style="margin:auto" border="1" > <tr><td> center this table <br /> with margin:auto style setting<br /> in div with text-align:center </td></tr> </table> </div>
|
center this table with margin:auto style setting in div with text-align:center |
<table style="text-align:center" border="1"> <tr><td> table <br /> with text-align:center style setting </td></tr> </table>
|
table with text-align:center style setting |
<center> <div style="background-color:yellow; width:200px;"> Here is some text which I want to keep left-aligned. </div> </center>which looks like this:
<center> <div style="text-align:left; background-color:yellow; width:200px;"> Here is some text which I want to keep left-aligned. </div> </center>getting the fix: