<tag style="property:
value"> or <tag style="property:
value; property: value">
Examples: <P
style="font-family: Arial"> or <P style="font-family: Arial; font-size:
12pt">
Note that the property is followed by a colon (:) and multiple
styles are separated by a semi-colon(;). The semi-colon can be omitted for the
last style applied or when there is only one style being applied.
Here are some of the styles that you can apply:
Property | Value | Description of Property | Examples |
---|---|---|---|
margin margin-left margin-right margin-top margin-bottom |
100px, 0.5in, 40mm, 14cm | Specifies the size of the margin(s). The margin property sets all four margins. | <P style="margin: 100px"> |
padding padding-left padding-right padding-top padding-bottom |
10px, 0.25in, 4mm, 1cm | Specifies the space between the margin and the text. The padding property sets all four paddings. | <P style="padding: 10px"> |
text-indent | 100px, 0.5in, 40mm, 14cm | Specifies the amount to indent the first line of text. | <P style="text-indent: 100px"> |
text-align | left, center, right or justify. | Specifies the alignment. Replaces the <CENTER> tag and the ALIGN attribute in the <P> tag. | <P style="text-align: center"> |
vertical-align | top, middle, bottom, etc. | Specifies the vertical alignment. | <TD style="vertical-align: top"> |
position top bottom left right z-index |
absolute, relative, a distance, e.g., 25px, a 3-D position, e.g., 0. |
Positions an object on the screen at a specific place. Can be used to stack objects with z-index: 0 at the bottom layer. | <IMG style="position: absolute; bottom: 25px;
right: 25px;" SRC="image1.gif"> <IMG style="position: absolute; top: 25px; left: 25px; z-index: 0" SRC="image1.gif"> |
font-family | Verdana, Arial, Helvetica, Geneva, sans-serif, Times New Roman, Georgia, Times, serif, cursive, etc. | Specifies which font to apply. It may be specific, e.g., Arial, or generic, e.g., serif, sans-serif, or cursive. Replaces the FACE attribute in the <FONT> tag. | <P style="font-family: Arial"> <H1 style="font-family:sans-serif"> |
font-size | 16pt., 300%, 0.5in., 40mm, 14cm, xx-small, x-small, small, medium, large, x-large, xx-large, 3em (3 times the size), 2ex (2 times the height), etc. | Specifies font size to apply. Replaces the SIZE attribute in the <FONT> tag. | <P style="font-size: 16pt."> |
font-weight | normal, bold, bolder, lighter, or 100-900 | Specifies the font weight to apply. Replaces the <B> tag. | <P style="font-weight: bold"> |
font-style | normal, italic (for serif fonts), or oblique (for sans-serif fonts). | Specifies the font style to apply. Replaces the <I> tag. | <P style="font-style: italic"> |
text-decoration | underline, line-through, overline, or blink. | Specifies a special text effect. Replaces the <U> tag, <BLINK> tag, etc. | <P style="text-decoration: underline"> |
text-transform | capitalize, uppercase, or lowercase. | Transforms text from lowercase to uppercase, etc. | <P style="text-transform: uppercase"> |
color | red, green, blue, #FF0000, #00FF00, #0000FF,
rgb(255,0,0), or rgb(100%, 0%, 0%). |
Assigns a color to text, etc. Replaces the COLOR attribute in the <FONT> tag. | <P style="color: #FF0000"> |
background-color | red, green, blue, #FF0000, #00FF00, #0000FF,
rgb(255,0,0), or rgb(100%, 0%, 0%). |
Assigns a background color. Replaces the BGCOLOR attribute in the <BODY> and <TR>tags. | <BODY style="background-color: #FFFFFF"> |
background-repeat background-repeat-x background-repeat-y |
url(images/picture1.gif) | Tiles an image in the background. The background-repeat property repeats an image both vertically and horizontally. Repeat-x repeats across the page and repeat-y repeats down the page. Replaces the BACKGROUND attribute in the <BODY> tag. | <BODY style="background-repeat: url(images/picture1.gif"> |
border-style | solid, double, dotted, dashed, ridge, groove, outset, or inset. | Identifies the type of border to place around an element on the page. | <P style="border-style: solid"> |
border-width | 4pt., 0.25in., 4mm, .5cm, etc. | Specifies the size of the border to place around an element on the page. | <P style="border-width: 4pt"> |
border-color | red, green, blue, #FF0000, #00FF00, #0000FF,
rgb(255,0,0), or rgb(100%, 0%, 0%). |
Specifies the color of the border to place around an element on the page. Replaces the BORDERCOLOR attribute in the <TABLE> tag. | <P style="border-color: #FF0000"> |
list-style-type | disc, circle, square, decimal, lower-roman, upper-roman, lower-alpha, upper-alpha, inside, outside, or url(image1.gif). | Creates a list and allows specification of the type of bullet. Not supported in Netscape. The bullet can also be an image. | <P style="text-decoration: underline"> |
Styles can be combined in a single tag. Separate multiple
styles within the same tag with a semi-colon (;). For example: <P style="font-family: Arial; font-size: 12pt"> |
Top | Style Attribute | Style Element | External Style Sheets | Week 15
SELECTOR {property:
value;} or SELECTOR {property:
value; property: value; property:
value;}
Note the use of the semi-colon (;) at the end of the
style and the curly braces ({ }) surrounding the style. The braces are the same
as those used to surround JavaScript functions, which are also coded in the HEAD
section. The comment codes (<!-- and -->) are included in the code to hide
it from older browsers that do not support styles.
An example of how to code the selector in the HEAD
section using the <STYLE> tag:
<STYLE
TYPE=text/css>
<!- -
H1 {font-family: Arial; font-size: 18pt;}
H2
{font-family: Arial; font-size: 16pt;}
- ->
</STYLE>
ID selectors are a unique name for an element. The name must begin with a
pound sign (#), be in uppercase or lowercase letter, be alphanumeric, and can
only identify one element.
Example:
HEAD section: #small
{font-size: 6pt;}
BODY section: <P ID=small>.
Pseudo-classes use a selector followed by a colon (:) and a pre-determined pseudo-class name. They are case-sensitive. Examples are shown below.
Pseudo-Class | What it does | Examples to code in the HEAD section |
---|---|---|
:hover | Changes the element when the mouse is over it. | A:hover {color: #0000FF;} |
:active | Changes the element when the mouse is clicked. | A:active {color: #FF0000;} |
:focus | Changes the element when the element is ready for keyboard input. | A:focus {color: #000000;} |
:link | Changes the appearance of a link. | A:link {color: #0000FF;} |
:visited | Changes the appearance of a visited link. | A:visited {color: #FF0000;} |
Top | Style Attribute | Style Element | External Style Sheets | Week 15
/* STYLESHEET.CSS */
H1 {font-family:
Arial; font-size: 18pt;}
H2 {font-family: Arial; font-size: 16pt;}
The syntax (except for the <STYLE> tags, which are not coded) is the same as for the style element. So, why have external style sheets? To save time. External style sheets can be applied to multiple documents, enabling you to use the same fonts and margins on all of your web pages. To use the external style sheet on a Web page, you must link to it or import it. Because the import rule is not supported by Netscape Navigator, it is best to use the <LINK> tag.
The <LINK> tag is coded in the HEAD section. You may apply multiple
external style sheets. If two or more external style sheets contain the same
property, the last external style sheet applied will have precedence.
An
example of a <LINK> tag:
<LINK HREF=stylesheet.css TYPE=text/css REL=stylesheet>
To use the import rule in the HEAD section:<STYLE TYPE=text/css>
<!-
-
@import url(stylesheet.css)
- ->
</STYLE>
Good resources for CSS: HTML Goodies and CSS Pointers Group
Top | Style Attribute | Style Element | External Style Sheets | Week 15