web design and development for business lecture 4 cascading style sheet (css)

61
Web Design and Development for Business Lecture 4 Cascading Style Sheet (CSS)

Upload: jeffry-barber

Post on 03-Jan-2016

220 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Web Design and Development for Business Lecture 4 Cascading Style Sheet (CSS)

Web Design and Development for Business

Lecture 4

Cascading Style Sheet (CSS)

Page 2: Web Design and Development for Business Lecture 4 Cascading Style Sheet (CSS)

What is CSS?

Page 3: Web Design and Development for Business Lecture 4 Cascading Style Sheet (CSS)

What is CSS? CSS stands for Cascading Style Sheets Styles define how to display HTML element

s Styles are normally stored in Style Sheets Styles were added to HTML 4.0 to solve a pr

oblem External Style Sheets can save you a lot of

work External Style Sheets are stored in CSS files Multiple style definitions will cascade into on

e

Page 4: Web Design and Development for Business Lecture 4 Cascading Style Sheet (CSS)
Page 5: Web Design and Development for Business Lecture 4 Cascading Style Sheet (CSS)

Styles Solve a Common Problem HTML tags were originally designed to define

the content of a document. They were supposed to say "This is a header

", "This is a paragraph", "This is a table", by using tags like <h1>, <p>, <table>, and so on.

The layout of the document was supposed to be taken care of by the browser, without using any formatting tags.

Page 6: Web Design and Development for Business Lecture 4 Cascading Style Sheet (CSS)

Styles Solve a Common Problem(cont’d) As the two major browsers - Netscape and Int

ernet Explorer - continued to add new HTML tags and attributes (like the <font> tag and the color attribute) to the original HTML specification, it became more and more difficult to create Web sites where the content of HTML documents was clearly separated from the document's presentation layout.

Page 7: Web Design and Development for Business Lecture 4 Cascading Style Sheet (CSS)

Styles Solve a Common Problem(cont’d) To solve this problem, the World Wide Web C

onsortium (W3C) - the non profit, standard setting consortium, responsible for standardizing HTML - created STYLES in addition to HTML 4.0.  

All major browsers support Cascading Style Sheets.

Page 8: Web Design and Development for Business Lecture 4 Cascading Style Sheet (CSS)

Style Sheets Can Save a Lot o f Work

Styles sheets define HOW HTML elements are to be displayed, just like the font tag and the color attribute in HTML 3.2.

Styles are normally saved in external .css files.

External style sheets enable you to change the appearance and layout of all the pages in your Web, just by editing one single CSS document!

Page 9: Web Design and Development for Business Lecture 4 Cascading Style Sheet (CSS)

Style Sheets Can Save a Lot of W ork (cont’d)

CSS is a breakthrough in Web design because it allows developers to control the style and layout of multiple Web pages all at once.

As a Web developer you can define a style for each HTML element and apply it to as many Web pages as you want.

To make a global change, simply change the style, and all elements in the Web are updated automatically.

Page 10: Web Design and Development for Business Lecture 4 Cascading Style Sheet (CSS)

Multiple Styles Will Cascade I nto One

Style sheets allow style information to be specified in many ways.

Styles can be specified inside a single HTML element, inside the <head> element of an HTML page, or in an external CSS file.

Even multiple external style sheets can be referenced inside a single HTML document.

Page 11: Web Design and Development for Business Lecture 4 Cascading Style Sheet (CSS)

Cascading Order What style will be used when there is mor

e than one style specified for an HTML element?

Generally speaking we can say that all the styles will "cascade" into a new "virtual" style sheet by the following rules, where number four has the highest priority: Browser default External style sheet Internal style sheet (inside the <head> tag) Inline style (inside an HTML element)

Page 12: Web Design and Development for Business Lecture 4 Cascading Style Sheet (CSS)

Cascading Order (cont’d)

So, an inline style (inside an HTML element) has the highest priority, which means that it will override a style declared inside the <head> tag, in an external style sheet, or in a browser (a default value).

Page 13: Web Design and Development for Business Lecture 4 Cascading Style Sheet (CSS)

CSS Syntax

Page 14: Web Design and Development for Business Lecture 4 Cascading Style Sheet (CSS)

CSS Syntax The CSS syntax is made up of three parts: a

selector, a property and a value:selector {property: value} The selector is normally the HTML element/ta

g you wish to define, the property is the attribute you wish to change, and each property can take a value.

The property and value are separated by a colon, and surrounded by curly braces:

body {color: black}

Page 15: Web Design and Development for Business Lecture 4 Cascading Style Sheet (CSS)

Note: If  the value is multiple words, put quotes around the value:

p {font-family: "sans serif"} Note: If you wish to specify more than one pr

operty, you must separate each property with a semicolon.

The example below shows how to define a center aligned paragraph, with a red text color:

p {text-align:center;color:red}

Page 16: Web Design and Development for Business Lecture 4 Cascading Style Sheet (CSS)

To make the style definitions more readable, you can describe one property on each line, like this:

p { text-align: center; color: black; font-family: arial }

Page 17: Web Design and Development for Business Lecture 4 Cascading Style Sheet (CSS)

Grouping You can group selectors. Separate each sele

ctor with a comma. In the example below we have grouped all th

e header elements. All header elements will be displayed in gree

n text color:h1,h2,h3,h4,h5,h6 { color: green }

Page 18: Web Design and Development for Business Lecture 4 Cascading Style Sheet (CSS)

The class Selector With the class selector you can define differe

nt styles for the same type of HTML element. Say that you would like to have two types of p

aragraphs in your document: one right-aligned paragraph, and one center-aligned paragraph. Here is how you can do it with styles:

p.right {text-align: right} p.center {text-align: center}

Page 19: Web Design and Development for Business Lecture 4 Cascading Style Sheet (CSS)

You have to use the class attribute in your HTML document:

<p class="right">

This paragraph will be right-aligned.

</p>

<p class="center">

This paragraph will be center-aligned.

</p>

Page 20: Web Design and Development for Business Lecture 4 Cascading Style Sheet (CSS)

Note: To apply more than one class per given element, the syntax is:

<p class="center bold">

This is a paragraph.

</p> The paragraph above will be styled by the cla

ss "center" AND the class "bold".

Page 21: Web Design and Development for Business Lecture 4 Cascading Style Sheet (CSS)

You can also omit the tag name in the selector to define a style that will be used by all HTML elements that have a certain class.

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

.center {text-align: center}

Page 22: Web Design and Development for Business Lecture 4 Cascading Style Sheet (CSS)

In the code below both the h1 element and the p element have class="center".

This means that both elements will follow the rules in the ".center" selector:  

<h1 class="center">

This heading will be center-aligned

</h1>

<p class="center">

This paragraph will also be center-aligned.

</p> Do NOT start a class name with a number! It will not

work in Mozilla/Firefox.

Page 23: Web Design and Development for Business Lecture 4 Cascading Style Sheet (CSS)

Add Styles to Elements with Part icular Attributes

You can also apply styles to HTML elements with particular attributes.

The style rule below will match all input elements that have a type attribute with a value of "text":

input[type="text"] {background-color: blue}

Page 24: Web Design and Development for Business Lecture 4 Cascading Style Sheet (CSS)

The id Selector

You can also define styles for HTML elements with the id selector.

The id selector is defined as a #. The style rule below will match the element th

at has an id attribute with a value of "green":

#green {color: green}

Page 25: Web Design and Development for Business Lecture 4 Cascading Style Sheet (CSS)

The style rule below will match the p element that has an id with a value of "para1":

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

Do NOT start an ID name with a number! It will not

work in Mozilla/Firefox.

Page 26: Web Design and Development for Business Lecture 4 Cascading Style Sheet (CSS)

CSS Comments Comments are used to explain your code, and may

help you when you edit the source code at a later date.

A comment will be ignored by browsers. A CSS comment begins with "/*", and ends with "*/", like this:

/* This is a comment */ p { text-align: center; /* This is another comment */ color: black; font-family: arial }

Page 27: Web Design and Development for Business Lecture 4 Cascading Style Sheet (CSS)

CSS How To...

Page 28: Web Design and Development for Business Lecture 4 Cascading Style Sheet (CSS)

How to Insert a Style Sheet

When a browser reads a style sheet, it will format the document according to it.

There are three ways of inserting a style sheet:

Page 29: Web Design and Development for Business Lecture 4 Cascading Style Sheet (CSS)

External Style Sheet An external style sheet is ideal when the style is app

lied to many pages. With an external style sheet, you can change the loo

k of an entire Web site by changing one file. Each page must link to the style sheet using the <lin

k> tag. The <link> tag goes inside the head section: <head> <link rel="stylesheet" type="text/css" href="mystyle.css

" /> </head>

Page 30: Web Design and Development for Business Lecture 4 Cascading Style Sheet (CSS)

The browser will read the style definitions from the file mystyle.css, and format the document according to it.

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 extensi

on. 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 31: Web Design and Development for Business Lecture 4 Cascading Style Sheet (CSS)

Do NOT leave spaces between the property value and the units!

If you use "margin-left: 20 px" instead of "margin-left: 20px" it will only work properly in IE6 but it will not work in Mozilla/Firefox or Netscape.

Page 32: Web Design and Development for Business Lecture 4 Cascading Style Sheet (CSS)

Internal Style Sheet An internal style sheet should be used when a singl

e document has a unique style. You define internal styles in the head section by usi

ng the <style> tag, like this: <head> <style type="text/css"> hr {color: sienna} p {margin-left: 20px} body {background-image: url("images/back40.gif")} </style> </head> The browser will now read the style definitions, and f

ormat the document according to it.

Page 33: Web Design and Development for Business Lecture 4 Cascading Style Sheet (CSS)

Note: A browser normally ignores unknown tags. This means that an old browser that does not suppo

rt styles, will ignore the <style> tag, but the content of the <style> tag will be displayed on the page.

It is possible to prevent an old browser from displaying the content by hiding it in the HTML comment element: 

<head> <style type="text/css"> <!-- hr {color: sienna} p {margin-left: 20px} body {background-image: url("images/back40.gif")} --> </style> </head>

Page 34: Web Design and Development for Business Lecture 4 Cascading Style Sheet (CSS)

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

sheets by mixing content with presentation. Use this method sparingly, such as when a style is t

o be applied to a single occurrence of an element. To use inline styles you use the style attribute in the

relevant tag. The style attribute can contain any CSS property. Th

e 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 35: Web Design and Development for Business Lecture 4 Cascading Style Sheet (CSS)

Multiple Style Sheets If some properties have been set for the sam

e selector in different style sheets, the values will be inherited from the more specific style sheet. 

For example, an external style sheet has these properties for the h3 selector:

h3 { color: red; text-align: left; font-size: 8pt } And an internal style sheet has these properti

es for the h3 selector:h3 { text-align: right; font-size: 20pt }

Page 36: Web Design and Development for Business Lecture 4 Cascading Style Sheet (CSS)

If the page with the internal style sheet also links to the external style sheet the properties for h3 will be:

color: red;

text-align: right;

font-size: 20pt The color is inherited from the external style s

heet and the text-alignment and the font-size is replaced by the internal style sheet.

Page 37: Web Design and Development for Business Lecture 4 Cascading Style Sheet (CSS)

CSS Examples

Page 38: Web Design and Development for Business Lecture 4 Cascading Style Sheet (CSS)

Not Apply CSS <h1>This is header 1</h1> <hr><p>This is a paragraph</p><p>This is another paragraph</p> <hr><h2>This is header 2, followed by a table</h2><table border="1" width="100%"><tr> <th>First Name</th><th>Name</th>

<th>Telephone</th> <th>Fax</th> </tr><tr> <td>Hege</td> <td>Svendson</td>

<td>555-777-0001</td><td>555-777-0000</td> </tr>

<tr> <td>Kai Jim</td> <td>Svendson</td><td>555-777-0002</td> <td>&nbsp;</td>

</tr> </table>

Page 39: Web Design and Development for Business Lecture 4 Cascading Style Sheet (CSS)

Not Apply CSS - Output

Page 40: Web Design and Development for Business Lecture 4 Cascading Style Sheet (CSS)

CSS Style #1

p,table, h1,h2,h3 {

font-family: verdana, arial, “sans serif”; }

p, h1, h2, h3, table, hr { margin-left: 10pt }

p, th,td { font-size: 75% }

body { #ffffff }

h1,h2,h3,hr { color: firebrick }

Page 41: Web Design and Development for Business Lecture 4 Cascading Style Sheet (CSS)

CSS Style #1 - Output

Page 42: Web Design and Development for Business Lecture 4 Cascading Style Sheet (CSS)

CSS Style #2

h1,h2,h3 {

font-family: "comic sans ms", arial }

p,table,li,address {

font-family: "comic sans ms", arial;

margin-left: 10pt }

p, th,td { font-size: 75% }

h1,h2,h3,hr { color:saddlebrown }

body { background-color: #EFE7D6 }

Page 43: Web Design and Development for Business Lecture 4 Cascading Style Sheet (CSS)

CSS Style #2 - Output

Page 44: Web Design and Development for Business Lecture 4 Cascading Style Sheet (CSS)

CSS Style #3h1,h2,h3 { font-family: arial, “sans serif” }

p,table,li,address {

margin-left: 15pt;

font-family: arial, “sans serif” }

p, th,td { font-size: 80% }

th { background-color:#FAEBD7}

body { background-color: #ffffff }

h1,h2,h3,hr { color:saddlebrown }

Page 45: Web Design and Development for Business Lecture 4 Cascading Style Sheet (CSS)

CSS Style #3 - Output

Page 46: Web Design and Development for Business Lecture 4 Cascading Style Sheet (CSS)

CSS Background

Page 47: Web Design and Development for Business Lecture 4 Cascading Style Sheet (CSS)

<html><head><style type="text/css">body {background-color: yellow}h1 {background-color: #00ff00}h2 {background-color: transparent}p {background-color: rgb(250,0,255)}</style></head><body><h1>This is header 1</h1><h2>This is header 2</h2><p>This is a paragraph</p></body></html>

Set the background color example

Page 48: Web Design and Development for Business Lecture 4 Cascading Style Sheet (CSS)

<html><head><style type="text/css">body {background-image:url('bgdesert.jpg')}</style></head><body></body></html>

Set an image as the background example

Page 49: Web Design and Development for Business Lecture 4 Cascading Style Sheet (CSS)

<html><head><style type="text/css">body{ background-image: url('bgdesert.jpg');background-repeat: repeat}</style></head><body></body></html>

How to repeat a background image

Page 50: Web Design and Development for Business Lecture 4 Cascading Style Sheet (CSS)

<html><head><style type="text/css">body{ background-image: url('bgdesert.jpg');background-repeat: repeat-y}</style></head><body></body></html>

How to repeat a background image only vertically

Page 51: Web Design and Development for Business Lecture 4 Cascading Style Sheet (CSS)

<html><head><style type="text/css">body{ background-image: url('bgdesert.jpg');background-repeat: repeat-x}</style></head><body></body></html>

How to repeat a background image only horizontally

Page 52: Web Design and Development for Business Lecture 4 Cascading Style Sheet (CSS)

<html><head><style type="text/css">body{ background-image: url('bgdesert.jpg');background-repeat: no-repeat}</style></head><body></body></html>

How to display an image background only one time

Page 53: Web Design and Development for Business Lecture 4 Cascading Style Sheet (CSS)

<html><head><style type="text/css">body{ background-image:url('smiley.gif');background-repeat:no-repeat;background-attachment:fixed;background-position:center; }</style></head><body><p><b>Note:</b> For this to work in Mozilla, the background-

attachment property must be set to "fixed".</p></body></html>

How to place the background image

Page 54: Web Design and Development for Business Lecture 4 Cascading Style Sheet (CSS)

<html><head><style type="text/css">body{ background-image: url('smiley.gif');background-repeat: no-repeat;background-attachment:fixed;background-position: 30% 20%; }</style></head><body><p><b>Note:</b> For this to work in Mozilla, the background-

attachment property must be set to "fixed".</p></body></html>

How to place the background image using %

Page 55: Web Design and Development for Business Lecture 4 Cascading Style Sheet (CSS)

<html><head><style type="text/css">body{ background-image: url('smiley.gif');background-repeat: no-repeat;background-attachment:fixed;background-position: 50px 100px;}</style></head><body><p><b>Note:</b> For this to work in Mozilla, the background-

attachment property must be set to "fixed".</p></body></html>

How to place the background image using pixels

Page 56: Web Design and Development for Business Lecture 4 Cascading Style Sheet (CSS)

<html><head><style type="text/css">body {background-image: url('smiley.gif');background-repeat: no-repeat;background-attachment: fixed}</style></head><body> put many lines of text here to make the page scrollable </body></html>

How to set a fixed background image

Page 57: Web Design and Development for Business Lecture 4 Cascading Style Sheet (CSS)

<html><head><style type="text/css">body{ background: #00ff00 url('smiley.gif') no-repeat fixed center; }</style></head><body>put many lines of text here to make the page scrollable</body></html>

All the background properties in one declaration

Page 58: Web Design and Development for Business Lecture 4 Cascading Style Sheet (CSS)

CSS Background Properties

Page 59: Web Design and Development for Business Lecture 4 Cascading Style Sheet (CSS)
Page 60: Web Design and Development for Business Lecture 4 Cascading Style Sheet (CSS)

For more details go to:

http://www.w3schools.com/css/default.asp

Page 61: Web Design and Development for Business Lecture 4 Cascading Style Sheet (CSS)

Reference

http://www.w3schools.com