~~SLIDESHOW~~
HTML is designed to define only the structure of a web document. The cascading style sheet (CSS) standard provides a way to define how a document, or collection of documents, will look. Cascading style sheets also allow certain user interface effects to be achieved.
Applying a common “look and feel” to a collection of web documents.
Format depends on the level of the style sheet
style
attributestyle = "property_1: value_1; property_2: value_2; . . .; property_n: value_n"
<style>
tag/* … */
)<style> rule list </style>
selector {list of property/values}
property: value
<style>
tag<style>
tag for document-level style sheetstext/css
<link>
tag in the HTML header is used to specify that the browser is to fetch and use an external style sheet file:<head> <link rel="stylesheet" href="http://www.swan.ac.uk/style.css" /> </head>
h1, h3 p
ol ol li
p.narrow {property/value list} p.wide {property/value list}
class
attribute of the tag<p class = "narrow"> <p class = "wide"> . . . . . . </p> </p> . . .
.really-big { . . . }
<h1 class = "really-big"> . . . </h1> . . . <p class = "really-big"> . . . </p>
id
selector allow the application of a style to one specific element#specific-id {property-value list}
#section14 {font-size: 20;}
id
tag must be set on the corresponding element.id
.:hover
classes apply when the mouse cursor is over the element:focus
classes apply when an element has focus<!DOCTYPE html> <!-- pseudo.html Illustrates the :hover and :focus pseudo classes --> <html lang="en"> <head> <meta charset="utf-8" /> <title> Checkboxes </title> <style type="text/css"> input:hover { color: red; } input:focus { color: green; } </style> </head> <body> <form action=""> <p> Your name: <input type="text" placeholder="type something to see the effect" /> </p> </form> </body> </html>
[ Jump to HTML 5 ]
left
, small
, etcurl(protocol://server/pathname)
rgb(n1, n2, n3)
, or Hexadecimal form #XXXXXX
Notes
px -- pixels in -- inches cm -- centimeters mm -- millimeters pt -- points pc -- picas (12 points) em -- height of the letter 'm' ex-height -- height of the letter 'x'
1.5 in
is illegal!font-family
: Arial
, Helvetica
, Courier
serif
, sans-serif
, cursive
, fantasy
, and monospace
(defined in CSS)'Times New Roman
'font-size
– Possible values: a length number or a name such as smaller
, xx-large
, etc.font-style
– italic
, oblique
, normal
font-weight
– degrees of boldness: bolder
, lighter
, bold
, normal
—-
The font-weight
can be specified as a multiple of 100 (100 – 900)
font
property:font: bolder 14pt Arial Helvetica
style
, weight
, size
, name(s)
Example 1
<!DOCTYPE html> <!-- fonts.html An example to illustrate font properties --> <html lang="en"> <head> <meta charset="utf-8" /> <title> Font properties </title> <style> p.big { font-size: 14pt; font-style: italic; font-family: 'Times New Roman'; } p.small { font: 10pt bold 'Courier New'; } h2 { font-family: 'Times New Roman'; font-size: 24pt; font-weight: bold } h3 { font-family: 'Courier New'; font-size: 18pt } </style> </head> <body> <p class = "big"> If a job is worth doing, it's worth doing right. </p> <p class = "small"> Two wrongs don't make a right, but they certainly can get you in a lot of trouble. </p> <h2> Chapter 1 Introduction </h2> <h3> 1.1 The Basics of Computer Networks </h3> </body> </html>
Example 2
The HTML code fonts2.html:
<!DOCTYPE html> <!-- fonts2.html An example to test external style sheets --> <html lang="en"> <head> <meta charset="utf-8" /> <title> External style sheets </title> <link rel = "stylesheet" href = "styles.css" /> </head> <body> <p class = "big"> If a job is worth doing, it's worth doing right. </p> <p class = "small"> Two wrongs don't make a right, but they certainly can get you in a lot of trouble. </p> <h2> Chapter 1 Introduction </h2> <h3> 1.1 The Basics of Computer Networks </h3> </body> </html>
The External Stylesheet styles.css:
/* styles.css - an external style sheet for use with fonts2.html */ p.big { font-size: 14pt; font-style: italic; font-family: 'Times New Roman'; } p.small { font: 10pt bold 'Courier New'; } h2 { font-family: 'Times New Roman'; font-size: 24pt; font-weight: bold } h3 { font-family: 'Courier New'; font-size: 18pt }
Note how much smaller, and easier to understand, is the HTML without the stylesheet. Also, note that the stylesheet can be used with a collection of HTML documents.
letter-spacing
: value is any length property valueExample3
<!DOCTYPE html> <!-- fonts3.html An example to illustrate some additional font properties --> <html lang="en"> <head> <meta charset="utf-8" /> <title> Text decoration </title> <style> p.through { text-decoration: line-through; } p.over { text-decoration: overline; } p.under { text-decoration: underline; } </style> </head> <body> <p class = "through"> This illustrates line-through </p> <p class= "over"> This illustrates overline </p> <p class = "under"> This illustrates underline </p> </body> </html>
disc
(default), a square
, or a circle
<ul>
or <li>
tag<ul>
, it applies to the whole list:<h3> Some Common Single-Engine Aircraft </h3> <ul style = "list-style-type: square"> <li> Cessna Skyhawk </li> <li> Beechcraft Bonanza </li> <li> Piper Cherokee </li> </ul>
<li style = "list-style-image:url(bird.jpg)">
list-style-type
can be used to change the sequence valuesProperty value | Sequence type | First four |
---|---|---|
decimal | Arabic numerals | 1, 2, 3, 4 |
upper-alpha | UC letters | A, B, C, D |
lower-alpha | LC letters | a, b, c, d |
upper-roman | UC Roman | I, II, III, IV |
lower-roman | LC Roman | i, ii, iii, iv |
<!DOCTYPE html> <!-- sequence_types.html An example to illustrate sequence type styles --> <html lang="en"> <head> <meta charset="utf-8" /> <title> Sequence types </title> <style> ol { list-style-type: upper-roman; } ol ol { list-style-type: upper-alpha; } ol ol ol { list-style-type: decimal; } </style> </head> <body> <h3> Aircraft Types </h3> <ol> <li> General Aviation (piston-driven engines) <ol> <li> Single-Engine Aircraft <ol> <li> Tail wheel </li> <li> Tricycle </li> </ol> </li> <li> Dual-Engine Aircraft <ol> <li> Wing-mounted engines </li> <li> Push-pull fuselage-mounted engines </li> </ol> </li> </ol> </li> <li> Commercial Aviation (jet engines) <ol> <li> Dual-Engine <ol> <li> Wing-mounted engines </li> <li> Fuselage-mounted engines </li> </ol> </li> <li> Tri-Engine <ol> <li> Third engine in vertical stabilizer </li> <li> Third engine in fuselage </li> </ol> </li> </ol> </li> </ol> </body> </html>
lower-greek
and hebrew
.There is a set of 16 colours that are guaranteed to be displayable by all graphical browsers on all colour monitors
black | 000000 | green | 008000 |
silver | C0C0C0 | lime | 00FF00 |
gray | 808080 | olive | 808000 |
white | FFFFFF | yellow | FFFF00 |
maroon | 800000 | navy | 000080 |
red | FF0000 | blue | 0000FF |
purple | 800080 | teal | 008080 |
fuchia | FF00FF | aqua | 00FFFF |
00
, 33
, 66
, 99
, CC
, and FF
color
property specifies the foreground colour of elements: color.htmlbackground-color
property specifies the background colour of elements: back_color.html
Example of the use of the color
property:
<!DOCTYPE html> <!-- back_color.html An example to illustrate the color property --> <html lang="en"> <head> <meta charset="utf=8" /> <title> background-color </title> <style> th.red { color: red } th.orange { color: orange } </style> </head> <body> . . . <table border = "5"> <tr> <th class = "red"> Apple </th> <th class = "orange"> Orange </th> <th class = "orange"> Screwdriver </th> </tr> </table> </body> </html>
Example of the use of the background-color
property:
<!DOCTYPE html> <!-- back_color.html An example to illustrate the background-color property --> <html lang="en"> <head> <meta charset="utf-8"> <title> background-color </title> <style> p.redback { font-size: 24pt; color: blue; background-color: red; } </style> </head> <body> <p class = "redback"> To really make it standout, use a red background! </p> </body> </html>
text-indent
property allows indentationtext-align
property has the possible values, left
(the default), center
, right
, or justify
float
propertyfloat
property has the possible values, left
, right
, and none
(the default)
If we have an element we want on the right,
with text flowing on its left, we use the
default text-align
value (left
) for the text and
the right
value for float
on the element we
want on the right
Example: float.html
<!DOCTYPE html> <!-- float.html An example to illustrate the float property --> <html lang="en"> <head> <meta charset="utf-8" /> <title> The float property </title> <style> img { float: right } </style> </head> <body> <p> <img src = "c210new.jpg" alt = "Picture of a Cessna 210" /> </p> <p> This is a picture of a Cessna 210. The 210 is the flagship single-engine Cessna aircraft. Although the 210 began as a four-place aircraft, it soon acquired a third row of seats, stretching it to a six-place plane. The 210 is classified as a high-performance airplane, which means its landing gear is retractable and its engine has more than 200 horsepower. In its first model year, which was 1960, the 210 was powered by a 260-horsepower fuel-injected six-cylinder engine that displaced 471 cubic inches. The 210 is the fastest single-engine airplane ever built by Cessna. </p> </body> </html>
border-style
property which controls whether the element has a border and if so, the style of the borderborder-style
values: none
, dotted
, dashed
, and double
border-width
: thin
, medium
(default), thick
, or a length value in pixelsborder-color
: any colourborder-top-width
, border-top-color
)<!DOCTYPE html> <!-- borders.html An example of a simple table with various borders --> <html lang="en"> <head> <meta charset="utf-8" /> <title> Table borders </title> <style> table { border-top-width: medium; border-bottom-width: thick; border-top-color: red; border-bottom-color: blue; border-top-style: dotted; border-bottom-style: dashed; } p { border-style: dashed; border-width: thin; border-color: green } </style> </head> <body> <table border = "5"> <caption> Fruit Juice Drinks </caption> <tr> <th></th> <th> Apple </th> <th> Orange </th> <th> Screwdriver </th> </tr> <tr> <th> Breakfast </th> <td> 0 </td> <td> 0 </td> </tr> <tr> <th> Lunch </th> <td> 1 </td> <td> 0 </td> <td> 0 </td> </tr> <tr> <th> Dinner </th> <td> 0 </td> <td> 0 </td> <td> 1 </td> </tr> </table> <p> Now is the time for all good Web programmers to learn to use style sheets. </p> </body> </html>
margin-left
, etc. – just assign them a length value<img src = "c210.jpg " style = "float: right; margin-left: 0.35in; margin-bottom: 0.35in" />
padding
, padding-left
, etc.<!DOCTYPE html> <!-- marpads.html An example to illustrate margins and padding --> <html lang="en"> <head> <meta charset="utf-8" /> <title> Table borders </title> <style> p.one { margin: 0.2in; padding: 0.2in; background-color: #C0C0C0; border-style: solid; } p.two { margin: 0.1in; padding: 0.3in; background-color: #C0C0C0; border-style: solid; } p.three { margin: 0.3in; padding: 0.1in; background-color: #C0C0C0; border-style: solid; } p.four { margin: 0.4in; background-color: #C0C0C0; } p.five { padding: 0.4in; background-color: #C0C0C0; } </style> </head> <body> <p> Here is the first line. </p> <p class = "one"> Now is the time for all good Web programmers to learn to use style sheets. <br /> [margin = 0.2in, padding = 0.2in] </p> <p class = "two"> Now is the time for all good Web programmers to learn to use style sheets. <br /> [margin = 0.1in, padding = 0.3in] </p> <p class = "three"> Now is the time for all good Web programmers to learn to use style sheets. <br /> [margin = 0.3in, padding = 0.1in] </p> <p class = "four"> Now is the time for all good Web programmers to learn to use style sheets. <br /> [margin = 0.4in, no padding, no border] </p> <p class = "five"> Now is the time for all good Web programmers to learn to use style sheets. <br /> [padding = 0.4in, no margin, no border] </p> <p> Here is the last line. </p> </body> </html>
background-image
propertybackground-repeat
property: possible values: repeat
(default), no-repeat
, repeat-x
, or repeat-y
background-position property
: possible values: top
, center
, bottom
, left
, or right
<!DOCTYPE html> <!-- back_image.html An example to illustrate background images --> <html lang="en"> <head> <meta charset="utf-8"> <title> Background images </title> <style> body { background-image: url(c172.gif); } p { margin-left: 30px; margin-right: 30px; margin-top: 50px; font-size: 14pt; color: yellow } </style> </head> <body> <p > The Cessna 172 is the most common general aviation airplane in the world. It is an all-metal, single-engine piston, high-wing four-place monoplane. It has fixed-gear and is categorized as a non-high-performance aircraft. The current model is the 172R. The wingspan of the 172R is 36'1". Its fuel capacity is 56 gallons in two tanks, one in each wing. The takeoff weight is 2,450 pounds. Its maximum useful load is 837 pounds. The maximum speed of the 172R at sea level is 142 mph. The plane is powered by a 360 cubic inch gasoline engine that develops 160 horsepower. The climb rate of the 172R at sea level is 720 feet per minute. </p> </body> </html>
<span>
<span>
is to leave the content as it is:<p> Now is the <span> best time </span> ever! </p>
<span>
to apply a document style sheet to its content:<style type = "text/css">? bigred {font-size: 24pt; font-family: Ariel; color: red} </style> <p> Now is the <span class = "bigred"> best time </span> ever! </p>
<span>
tag is similar to other HTML tags, it can be nested and they have id
and class
attributes<div>
By default <div>
is a block-level tag and <span>
is an in-line tag, although these definitions
can be overridden by style declarations.