chapter 8 cascading style sheets (css). agenda definition of a css style types of css styles css...

26
Chapter 8 Cascading Style Sheets (CSS)

Upload: joan-palmer

Post on 24-Dec-2015

376 views

Category:

Documents


8 download

TRANSCRIPT

Page 1: Chapter 8 Cascading Style Sheets (CSS). Agenda Definition of a CSS style Types of CSS Styles CSS Backgrounds CSS Text CSS Fonts CSS Links CSS Lists CSS

Chapter 8

Cascading Style Sheets (CSS)

Page 2: Chapter 8 Cascading Style Sheets (CSS). Agenda Definition of a CSS style Types of CSS Styles CSS Backgrounds CSS Text CSS Fonts CSS Links CSS Lists CSS

Agenda• Definition of a CSS style• Types of CSS Styles• CSS Backgrounds• CSS Text• CSS Fonts• CSS Links• CSS Lists• CSS Tables• CSS Selectors• CSS Units• CSS Color Values

Page 3: Chapter 8 Cascading Style Sheets (CSS). Agenda Definition of a CSS style Types of CSS Styles CSS Backgrounds CSS Text CSS Fonts CSS Links CSS Lists CSS

What is CSS?Cascading Style Sheets (CSS) is a style sheet language used for describing the presentation semantics (the look and formatting) of a document written in a markup language. Its most common application is to style web pages written in HTML and XHTML, but the language can also be applied to any kind of XML document.• Styles define how to display HTML elements• Styles are added to HTML to solve a problem• External Style Sheets can save a lot of work• External Style Sheets are stored in CSS files

Page 4: Chapter 8 Cascading Style Sheets (CSS). Agenda Definition of a CSS style Types of CSS Styles CSS Backgrounds CSS Text CSS Fonts CSS Links CSS Lists CSS

Types of CSS Styles1. Internal Styles are defined in the <head>

section of the current web page.2. Inline Styles are defined within the HTML

markup of a particular page element. (avoid using)

3. External Styles are defined on the External Style Sheet, which is linked to the web pages(s). (Best choice)

Page 5: Chapter 8 Cascading Style Sheets (CSS). Agenda Definition of a CSS style Types of CSS Styles CSS Backgrounds CSS Text CSS Fonts CSS Links CSS Lists CSS

Internal Style SheetAn internal style sheet should be used when a single document has a unique style. You define internal styles in the head section of an HTML page, by using the <style> tag, like this:<head>< style>hr {color:sienna;}p {margin-left:20px;}body {background image:url("images/back40.gif");}< /style>< /head>

Page 6: Chapter 8 Cascading Style Sheets (CSS). Agenda Definition of a CSS style Types of CSS Styles CSS Backgrounds CSS Text CSS Fonts CSS Links CSS Lists CSS

Inline Styles• An inline style loses many of the advantages of

style sheets by mixing content with presentation. Use this method sparingly!

• To use inline styles you use the style attribute in the relevant tag. The style attribute can contain any CSS property. The example shows how to change the color and the left margin of a paragraph:

• <p style="color:sienna;margin-left:20px;">This is a paragraph.</p>

Page 7: Chapter 8 Cascading Style Sheets (CSS). Agenda Definition of a CSS style Types of CSS Styles CSS Backgrounds CSS Text CSS Fonts CSS Links CSS Lists CSS

External Styles• An external style sheet is ideal when the style is applied to many

pages. With an external style sheet, you can change the look of an entire Web site by changing one file. Each page must link to the style sheet using the <link> tag. The <link> tag goes inside the head section:

• <head>< link rel="stylesheet" type="text/css" href="mystyle.css">< /head>

• An external style sheet can be written in any text editor. The file should not contain any html tags. Your style sheet should be saved with a .css extension. An example of a style sheet file is shown below:

• hr {color:sienna;}p {margin-left:20px;}body {background-image:url("images/back40.gif");}

Page 8: Chapter 8 Cascading Style Sheets (CSS). Agenda Definition of a CSS style Types of CSS Styles CSS Backgrounds CSS Text CSS Fonts CSS Links CSS Lists CSS

Example<html><head><style>

body {color:red;}h1 {color:#00ff00;}p.ex {color:rgb(0,0,255);}

</style>< link rel="stylesheet" type="text/css" href="mystyle.css"></head><body><h1>This is heading 1</h1><p style="color:sienna;margin-left:20px;">This is a paragraph.</p><p>This is an ordinary paragraph. Notice that this text is red. The default text-color for a page is defined in the body selector.</p><p class="ex">This is a paragraph with class="ex". This text is blue.</p></body></html>

Page 9: Chapter 8 Cascading Style Sheets (CSS). Agenda Definition of a CSS style Types of CSS Styles CSS Backgrounds CSS Text CSS Fonts CSS Links CSS Lists CSS

CSS Background PropertiesProperty Description

background Sets all the background properties in one declaration

background-attachment Sets whether a background image is fixed or scrolls with the rest of the page

background-color Sets the background color of an element

background-image Sets the background image for an element

background-position Sets the starting position of a background image

background-repeat Sets how a background image will be repeated

Page 10: Chapter 8 Cascading Style Sheets (CSS). Agenda Definition of a CSS style Types of CSS Styles CSS Backgrounds CSS Text CSS Fonts CSS Links CSS Lists CSS

Example of CSS Background<head>body{background-image:url('img_tree.png');background-repeat:no-repeat;background-position:right top;} h1 {background-color:#6495ed;}p {background-color:#e0ffff;}</head>

Page 11: Chapter 8 Cascading Style Sheets (CSS). Agenda Definition of a CSS style Types of CSS Styles CSS Backgrounds CSS Text CSS Fonts CSS Links CSS Lists CSS

CSS Text PropertiesProperty Description

color Sets the color of text

direction Specifies the text direction/writing direction

letter-spacing Increases or decreases the space between characters in a text

line-height Sets the line height

text-align Specifies the horizontal alignment of text

text-decoration Specifies the decoration added to text

text-indent Specifies the indentation of the first line in a text-block

text-shadow Specifies the shadow effect added to text

text-transform Controls the capitalization of text

vertical-align Sets the vertical alignment of an element

white-space Specifies how white-space inside an element is handled

word-spacing Increases or decreases the space between words in a text

Page 12: Chapter 8 Cascading Style Sheets (CSS). Agenda Definition of a CSS style Types of CSS Styles CSS Backgrounds CSS Text CSS Fonts CSS Links CSS Lists CSS

Example of CSS Text Propertiesbody {color:blue;}h1 {color:#00ff00;}h2 {color:rgb(255,0,0);}h1 {text-align:center;}p.date {text-align:right;}p.main {text-align:justify;} a {text-decoration:none;} h1 {text-decoration:overline;}h2 {text-decoration:line-through;}h3 {text-decoration:underline;}p.uppercase {text-transform:uppercase;}p.lowercase {text-transform:lowercase;}p.capitalize {text-transform:capitalize;}p {text-indent:50px;}

Page 13: Chapter 8 Cascading Style Sheets (CSS). Agenda Definition of a CSS style Types of CSS Styles CSS Backgrounds CSS Text CSS Fonts CSS Links CSS Lists CSS

CSS Font PropertiesProperty Description

font Sets all the font properties in one declaration

font-family Specifies the font family for text

font-size Specifies the font size of text

font-style Specifies the font style for text

font-variant Specifies whether or not a text should be displayed in a small-caps font

font-weight Specifies the weight of a font

Page 14: Chapter 8 Cascading Style Sheets (CSS). Agenda Definition of a CSS style Types of CSS Styles CSS Backgrounds CSS Text CSS Fonts CSS Links CSS Lists CSS

Example of CSS Font Propertiesp{font-family:"Times New Roman", Times, serif;}p.normal {font-style:normal;}p.italic {font-style:italic;}p.oblique {font-style:oblique;}h1 {font-size:40px;}h2 {font-size:30px;}p {font-size:14px;}p.small{font-variant:small-caps;} p.normal {font-weight:normal;}p.thick {font-weight:bold;}p.thicker {font-weight:900;}

Page 15: Chapter 8 Cascading Style Sheets (CSS). Agenda Definition of a CSS style Types of CSS Styles CSS Backgrounds CSS Text CSS Fonts CSS Links CSS Lists CSS

CSS Links• Links can be styled with any CSS property (e.g. color, font-

family, background, etc.).• In addition, links can be styled differently depending on

what state they are in.• The four links states are:

a:link - a normal, unvisited linka:visited - a link the user has visiteda:hover - a link when the user mouses over ita:active - a link the moment it is clicked

• Examplea:link {color:#FF0000;} /* unvisited link */a:visited {color:#00FF00;} /* visited link */a:hover {color:#FF00FF;} /* mouse over link */a:active {color:#0000FF;} /* selected link */

Page 16: Chapter 8 Cascading Style Sheets (CSS). Agenda Definition of a CSS style Types of CSS Styles CSS Backgrounds CSS Text CSS Fonts CSS Links CSS Lists CSS

CSS Links (Continued)Text Decoration• The text-decoration property is mostly used to remove underlines from

links:• Examplea:link {text-decoration:none;}a:visited {text-decoration:none;}a:hover {text-decoration:underline;}a:active {text-decoration:underline;}Background Color• The background-color property specifies the background color for links:• Examplea:link {background-color:#B2FF99;}a:visited {background-color:#FFFF85;}a:hover {background-color:#FF704D;}a:active {background-color:#FF704D;}

Page 17: Chapter 8 Cascading Style Sheets (CSS). Agenda Definition of a CSS style Types of CSS Styles CSS Backgrounds CSS Text CSS Fonts CSS Links CSS Lists CSS

CSS List Properties

Property Description

list-style Sets all the properties for a list in one declaration

list-style-image Specifies an image as the list-item marker

list-style-position Specifies if the list-item markers should appear inside or outside the content flow

list-style-type Specifies the type of list-item marker

Page 18: Chapter 8 Cascading Style Sheets (CSS). Agenda Definition of a CSS style Types of CSS Styles CSS Backgrounds CSS Text CSS Fonts CSS Links CSS Lists CSS

Example of CSS List Propertiesul.a {list-style-type: circle;}ul.b {list-style-type: square;}ol.c {list-style-type: upper-roman;}ol.d {list-style-type: lower-alpha;}ul{list-style-image: url('sqpurple.gif');}ul{list-style-position:inside;}

Page 19: Chapter 8 Cascading Style Sheets (CSS). Agenda Definition of a CSS style Types of CSS Styles CSS Backgrounds CSS Text CSS Fonts CSS Links CSS Lists CSS

CSS Tables properties• Table Borders• To specify table borders in CSS, use the border

property.• The example below specifies a black border for

table, th, and td elements:• Exampletable, th, td{border: 1px solid black;}

Page 20: Chapter 8 Cascading Style Sheets (CSS). Agenda Definition of a CSS style Types of CSS Styles CSS Backgrounds CSS Text CSS Fonts CSS Links CSS Lists CSS

Example of CSS Tables propertiestable {width:100%;}th{height:50px;} td{text-align:right;}td{height:50px;vertical-align:bottom;}td{padding:15px;}table, td, th{border:1px solid green;}th{background-color:green;color:white;}

Page 21: Chapter 8 Cascading Style Sheets (CSS). Agenda Definition of a CSS style Types of CSS Styles CSS Backgrounds CSS Text CSS Fonts CSS Links CSS Lists CSS

The CSS id Selector• The id selector is used to specify a style for a single,

unique element.• The id selector uses the id attribute of the HTML

element, and is defined with a "#".• The style rule below will be applied to the element

with id="para1":• Example

#para1{text-align:center;color:red;}

Page 22: Chapter 8 Cascading Style Sheets (CSS). Agenda Definition of a CSS style Types of CSS Styles CSS Backgrounds CSS Text CSS Fonts CSS Links CSS Lists CSS

The CSS Class Selector• The class selector is used to specify a style for a

group of elements. Unlike the id selector, the class selector is most often used on several elements.

• This allows you to set a particular style for many HTML elements with the same class.

• The class selector uses the HTML class attribute, and is defined with a "."

• In the example below, all HTML elements with class="center" will be center-aligned:

• Example.center {text-align:center;}

Page 23: Chapter 8 Cascading Style Sheets (CSS). Agenda Definition of a CSS style Types of CSS Styles CSS Backgrounds CSS Text CSS Fonts CSS Links CSS Lists CSS

CSS Selectors

Selector Example Example description

.class .intro Selects all elements with class="intro"#id #firstname Selects the element with id="firstname"* * Selects all elementselement p Selects all <p> elements

element,element div,p Selects all <div> elements and all <p> elements

element element div p Selects all <p> elements inside <div> elements

element>element div>p Selects all <p> elements where the parent is a <div> element

element+element div+p Selects all <p> elements that are placed immediately after <div> elements

Page 24: Chapter 8 Cascading Style Sheets (CSS). Agenda Definition of a CSS style Types of CSS Styles CSS Backgrounds CSS Text CSS Fonts CSS Links CSS Lists CSS

CSS UnitsUnit Description

% percentage

in inch

cm centimeter

mm millimeter

em1em is equal to the current font size. 2em means 2 times the size of the current font. E.g., if an element is displayed with a font of 12 pt, then '2em' is 24 pt. The 'em' is a very useful unit in CSS, since it can adapt automatically to the font that the reader uses

ex one ex is the x-height of a font (x-height is usually about half the font-size)

pt point (1 pt is the same as 1/72 inch)

pc pica (1 pc is the same as 12 points)

px pixels (a dot on the computer screen)

Page 25: Chapter 8 Cascading Style Sheets (CSS). Agenda Definition of a CSS style Types of CSS Styles CSS Backgrounds CSS Text CSS Fonts CSS Links CSS Lists CSS

Color ValuesColor Color HEX Color RGB

#000000 rgb(0,0,0)

#FF0000 rgb(255,0,0)

#00FF00 rgb(0,255,0)

#0000FF rgb(0,0,255)

#FFFF00 rgb(255,255,0)

#00FFFF rgb(0,255,255)

#FF00FF rgb(255,0,255)

#C0C0C0 rgb(192,192,192)

#FFFFFF rgb(255,255,255)

Page 26: Chapter 8 Cascading Style Sheets (CSS). Agenda Definition of a CSS style Types of CSS Styles CSS Backgrounds CSS Text CSS Fonts CSS Links CSS Lists CSS

Lab 8

• Types of CSS Styles• CSS Backgrounds• CSS Text• CSS Fonts• CSS Links• CSS Lists• CSS Tables• CSS Selectors• CSS Units• CSS Color Values