Cascading Style Sheets (CSS) Asst. Prof. Dr. Kanda Runapongsa - - PowerPoint PPT Presentation

cascading style sheets css
SMART_READER_LITE
LIVE PREVIEW

Cascading Style Sheets (CSS) Asst. Prof. Dr. Kanda Runapongsa - - PowerPoint PPT Presentation

Cascading Style Sheets (CSS) Asst. Prof. Dr. Kanda Runapongsa Saikaew (krunapon@kku.ac.th) Dept. of Computer Engineering Khon Kaen University 1 Agenda What is CSS? CSS Syntax CSS Basic 2 2 What is CSS? CSS stands for C


slide-1
SLIDE 1

1

Cascading Style Sheets (CSS)

  • Asst. Prof. Dr. Kanda

Runapongsa Saikaew (krunapon@kku.ac.th)

  • Dept. of Computer Engineering

Khon Kaen University

slide-2
SLIDE 2

2 2

Agenda

What is CSS? CSS Syntax CSS Basic

slide-3
SLIDE 3

3

What is CSS?

CSS stands for Cascading Style

Sheets

Styles define how to display

HTML elements

Styles are normally stored in

Style Sheets

Multiple style definitions will

cascade into one

3

slide-4
SLIDE 4

4

Why Style Sheets?

Style sheets define HOW HTML

elements are to be displayed

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

By editing only one single CSS

document

4

slide-5
SLIDE 5

5

Multiple styles will cascade into one

Style sheets allow style information

to be specified in many ways.

Styles can be specified:

 Inside an HTML element  Inside the head section of an HTML

page

 In an external CSS file

Cascading order - What style will be

used when there is more than one style specified for an HTML element?

slide-6
SLIDE 6

6

Priority in Applying Styles

  • 1. Inline style (inside an HTML element)
  • 2. Internal style sheet (in the head

section)

  • 3. External style sheet

* If the link to the external style sheet is placed after the internal style sheet, the external style sheet will override the internal style sheet!

  • 4. Browser default
slide-7
SLIDE 7

7

CSS Syntax

The CSS syntax is made up of

three parts

A selector: the HTML element/tag

you wish to define

A property: the attribute you wish

to change

A value: the value of the property

Format

selector {property: value }

7

slide-8
SLIDE 8

8

CSS Syntax Samples (1/2)

The property and value are

separated by a colon, and surrounded by curly braces:

body {color: black}

If the value is multiple words, put

quotes around the value

p {font-family: “sans serif”}

8

slide-9
SLIDE 9

9

CSS Syntax Samples (2/2)

If wish to specify more than one

property, you must separate each property with a semicolon

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

To make the style definitions more

readable, describe one property on each line

 p {

text-align:center; color:red }

9

slide-10
SLIDE 10

10

Grouping

You can group separators by

separating each selector with a comma

Example

All header elements will be

displayed in green text color

h1, h2, h3, h4, h5, h6

{ color:green }

10

slide-11
SLIDE 11

11

The class Selector (1/3)

With the class selector you can

define different styles for the same type of HTML element

Example

Two types of paragraphs: one

right-aligned paragraph and one center-aligned paragraph

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

11

slide-12
SLIDE 12

12

The class Selector (2/3)

 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>

 To apply more than one class per given

element, the syntax is:

 <p class="center bold"> This is a

  • paragraph. </p>

12

slide-13
SLIDE 13

13

The class Selector (3/3)

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}  <h1 class=“center”>Center-aligned

heading</h1>

 <p class=“center”>Center-aligned

paragraph</p>

13

slide-14
SLIDE 14

14

Add Styles to Elements with Particular Attributes

You can also apply styles to

HTML elements with particular attributes.

The style rule below will match

all input elements that has a type attribute with a value of "text":

Example

input[type="text"] {background-

color: blue}

14

slide-15
SLIDE 15

15

The id Selector (1/2)

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 that has an id attribute with a value of "green":

#green {color: green}

15

slide-16
SLIDE 16

16

The id Selector (2/2)

The style rule below will match

the p element that has an id with a value of "para1":

p#green

{ text-align: center}

Note: Do NOT start an ID name

with a number! It will not work in Mozilla/Firefox.

16

slide-17
SLIDE 17

17

CSS Comments

 A comment will be ignored by browsers. A

CSS comment begins with "/*", and ends with "*/“

 Example  /* This is a comment */

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

17

slide-18
SLIDE 18

18

External Style Sheet (1/2)

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

18

slide-19
SLIDE 19

19

External Style Sheet (2/2)

In an HTML page

<head> <link rel="stylesheet"

type="text/css" href="mystyle.css" /> </head>

In file mystyle.css

hr {color: sienna}

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

19

slide-20
SLIDE 20

20

CSS Example1

20

slide-21
SLIDE 21

21

CSS Example2

21

slide-22
SLIDE 22

22

Internal Style Sheet

 An internal style sheet should be used when a

single document has a unique style.

 You define internal styles in the head section

by using the <style> tag

 In an HTML page  <head><style type="text/css"> hr {color:

sienna} p {margin-left: 20px} body {background-image: url("images/back40.gif")} </style> </head>

22

slide-23
SLIDE 23

23

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 to be applied to a single

  • ccurrence of an element

 Example  <p style="color: sienna; margin-left:

20px"> This is a paragraph </p>

23

slide-24
SLIDE 24

24

CSS Background

The CSS background properties

define the background effects of an element

Some styles for background

Set the background color for an

element

Set an image as the background

slide-25
SLIDE 25

25

CSS Example3: Background Color

HTML Code

<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>

HTML Presentation

25

slide-26
SLIDE 26

26

CSS Example4: Image as the Background

HTML Code

<html><head> <style type="text/css"> body { background-image: url('images/wall-paper.3.gif') }</style> </head><body> <h1>10 Programming Languages You Should Learn</ h1> </body> </html>

HTML Presentation

26

slide-27
SLIDE 27

27

CSS Text

 The CSS text properties define

the appearance of text

Some properties that we can set

Color Alignment Lowercase or uppercase

slide-28
SLIDE 28

28

CSS Example 5: Text Color

HTML Code

<html> <head> <style type="text/css"> h1 {color: #00ff00} h2 {color: #dda0dd} p {color: rgb(0,0,255)} </style> </head> <body> <h1>PHP</h1> <h2>C#</h2> <p>Java</p> </body> </html>

HTML Presentation

28

slide-29
SLIDE 29

29

CSS Font Family

CSS font properties define the font

family, boldness, size, and the style

  • f a text

In CSS, there is two types of font

family names:

 Generic family - a group of font

families with a similar look (like "Serif"

  • r"Monospace")

 Font family - a specific font family (like

"Times New Roman" or "Arial")

slide-30
SLIDE 30

30

CSS Example 6: Font Family

<html> <head> <style type="text/css"> h3 {font-family: times} p {font-family: courier} p.sansserif {font-family: sans- serif} </style> </head> <body> <h3>This is header 3</h3> <p>This is a paragraph</p> <p class="sansserif">This is a paragraph</p> </body> </html>

HTML Presentation

30

HTML Code

slide-31
SLIDE 31

31

CSS Font Style

The font-style property is mostly

used to specify italic text

This property has three values:

normal - The text is shown normally italic - The text is shown in italics oblique - The text is "leaning"

(oblique is very similar to italic, but less supported)

slide-32
SLIDE 32

32

CSS Example 7: Font Style

 HTML Code

<html> <head> <style type="text/css"> p.normal {font-style:normal} p.italic {font-style:italic} p.oblique {font-style:oblique} </style></head> <body> <p class="normal">This is a paragraph, normal.</p> <p class="italic">This is a paragraph, italic.</p> <p class="oblique">This is a paragraph, oblique.</p> </body></html>

 HTML Presentation

32

slide-33
SLIDE 33

33

CSS Font Size

Being able to manage the text size is

important in web design

However, you should not use font size

adjustments to make paragraphs look like headings, or headings look like paragraphs.

Always use the proper HTML tags, like

<h1> - <h6> for headings and <p> for paragraphs.

The font-size value can be an

absolute, or relative size

slide-34
SLIDE 34

34

CSS Font Absolute Size

Sets the text to a specified size Does not allow a user to change

the text size in all browsers (bad for accessibility reasons)

Absolute size is useful when the

physical size of the output is known

slide-35
SLIDE 35

35

CSS Font Relative Size

Sets the size relative to surrounding

elements

 Allows a user to change the text size

in browsers

Remark If you do not specify a font

size, the default size for normal text, like paragraphs, is 16px (16px=1em)

To avoid the resizing problem with

Internet Explorer, many developers use em instead of pixels

The em size unit is recommended

by the W3C.

slide-36
SLIDE 36

36

CSS Example 8: Font Size

 HTML Code

<html> <head> <style type="text/css"> h1 {font-size: 150%} h2 {font-size: 130%} p {font-size: 100%} </style> </head> <body> <h1>This is header 1</h1> <h2>This is header 2</h2> <p>This is a paragraph</p> </body></html>

 HTML Presentation

36

slide-37
SLIDE 37

37

CSS Example 9: Font Size

 HTML Code

<html> <head> <style> h1 {font-size:2.5em} /* 40px/16=2.5em */ h2 {font-size:1.875em} /* 30px/ 16=1.875em */ p {font-size:0.875em} /* 14px/16=0.875em */ </style> </head> <body> <h1>This is heading 1</h1> <h2>This is heading 2</h2> <p>This is a paragraph.</p> </body></html>

 HTML Presentation

37

slide-38
SLIDE 38

38

CSS Box Model

All HTML elements can be considered as

  • boxes. In CSS, the term "box model" is

used when talking about design and layout

In order to set the width and height of

an element correctly in all browsers, you need to know how the box model works.

The box model illustrates how the CSS

properties: margin, border, and padding, affects the width and height of an element

slide-39
SLIDE 39

39

CSS Box Model Layout

slide-40
SLIDE 40

40

Box Model Explanation

Margin - Clears an area around the border. The

margin does not have a background color, and it is completely transparent

Border - A border that lies around the padding

and content. The border is affected by the background color of the box

Padding - Clears an area around the content.

The padding is affected by the background color

  • f the box

 Content - The content of the box, where text

and images appear

slide-41
SLIDE 41

41

Width & Height of An Element

 When you specify the width and

height properties of an element with CSS, you are just setting the width and height of the content area

To know the full size of the

element, you must also add the padding, border and margin

slide-42
SLIDE 42

42

Width Box Model Example

An Element

width:250px; padding:10px; border:5px solid gray; margin:10px;

What is a total width of this

element?

250 + 10*2 + 5*2 + 10*2 = 300

slide-43
SLIDE 43

43

Browsers Compatibility Issue

IE includes padding and border in the

width, when the width property is set, unless a DOCTYPE is declared

To fix this problem, just add a

DOCTYPE to the code:

<!DOCTYPE html PUBLIC

"-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xht ml1-transitional.dtd">

slide-44
SLIDE 44

44

CSS Example 10: Box Model

 HTML Code

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml 1-transitional.dtd"> <html><head> <style type="text/css"> div.ex {width:220px; padding:10px; border:5px solid gray; margin:0px;} </style></head><body> <div class="ex">The line above is 250px wide.<br /> Now the total width of this element is also 250px.</div> <p><b>Note:</b> In this example we have added a DOCTYPE<br/> declaration (above the html element),<br/> so it displays correctly in all browsers.</p> </body> </html>

 HTML Presentation

44

slide-45
SLIDE 45

45

CSS Border

 The CSS border properties allow you to

specify the style and color of an element's border

 The border-style property specifies what

kind of border to display

 The border-width property is used to set

the width of the border

  • The width is set in pixels, or by using
  • ne of the three pre-defined values:

thin, medium, or thick.

45

slide-46
SLIDE 46

46

CSS Example 11: Border Style and Width

<html><head><style type="text/css"> p.one {border-style:solid;border-width:5px;} p.two { border-style:solid; border-width:medium;} </style></head><body> <p class="one">Some text.</p> <p class="two">Some text.</p> <p><b>Note:</b> The "border-width" property does not work if it is used alone. Use the "border- style" property to set the borders first.</p> </body></html>

slide-47
SLIDE 47

47

CSS Example 12: Border Color

<html> <head><style type="text/css"> p.one { border-style:solid; border-color:red;} p.two { border-style:solid; border-color:#98bf21; } </style> </head> <body> <p class="one">A solid red border</p> <p class="two">A solid green border</ p> <p><b>Note:</b> The "border-color" property does not work if it is used

  • alone. Use the "border-style" property

to set the borders first.</p> </body> </html>

slide-48
SLIDE 48

48

CSS Example 13: Border Style

<html><head> <style type="text/css"> p.dotted {border-style: dotted} p.dashed {border-style: dashed} p.solid {border-style: solid} p.inset {border-style: inset} p.outset {border-style: outset} </style> </head> <body> <p class="dotted">A dotted border</p> <p class="dashed">A dashed border</p> <p class="solid">A solid border</p> <p class="inset">An inset border</p> <p class="outset">An outset border</p> </body> </html>

 HTML Presentation

48

slide-49
SLIDE 49

49

CSS List

  • In HTML, there are two types of

lists:

– unordered list - the list items are marked with bullets (typically circles or squares) –

  • rdered list - the list items are

marked with numbers or letters

  • With CSS, lists can be styled

further, and images can be used as list item markers.

slide-50
SLIDE 50

50

CSS Example 14: List

<html><head><style type="text/css"> ul.circle {list-style-type:circle} ul.square {list-style-type:square}

  • l.upper-roman {list-style-type:upper-

roman}

  • l.lower-alpha {list-style-type:lower-

alpha} </style></head> <body> <p>Type circle:</p> <ul class="circle"><li>Coffee</li> <li>Tea</li></ul> <p>Type square:</p> <ul class="square"><li>Coffee</li> <li>Tea</li></ul> <p>Type upper-roman:</p> <ol class="upper- roman"><li>Coffee</li> <li>Tea</li></ol> <p>Type lower-alpha:</p> <ol class="lower- alpha"><li>Coffee</li> <li>Tea</li></ol> </body></html>

slide-51
SLIDE 51

51

References

W3Schools, “CSS Tutorial”,

available at

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

51