1
Cascading Style Sheets (CSS)
- Asst. Prof. Dr. Kanda
Runapongsa Saikaew (krunapon@kku.ac.th)
- Dept. of Computer Engineering
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
1
2 2
3
CSS stands for Cascading Style
Styles define how to display
Styles are normally stored in
Multiple style definitions will
3
4
Style sheets define HOW HTML
Styles are normally saved in
External style sheets enable you
By editing only one single CSS
4
5
Style sheets allow style information
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
6
* 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!
7
The CSS syntax is made up of
A selector: the HTML element/tag
A property: the attribute you wish
A value: the value of the property
Format
selector {property: value }
7
8
The property and value are
body {color: black}
If the value is multiple words, put
p {font-family: “sans serif”}
8
9
If wish to specify more than one
p {text-align:center; color:red}
To make the style definitions more
p {
text-align:center; color:red }
9
10
You can group separators by
Example
All header elements will be
h1, h2, h3, h4, h5, h6
10
11
With the class selector you can
Example
Two types of paragraphs: one
p.right {text-align: right} p.center {text-align: center}
11
12
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
12
13
You can also omit the tag name in
In the example below, all HTML
.center {text-align: center} <h1 class=“center”>Center-aligned
heading</h1>
<p class=“center”>Center-aligned
paragraph</p>
13
14
Add Styles to Elements with Particular Attributes
You can also apply styles to
The style rule below will match
Example
input[type="text"] {background-
14
15
You can also define styles for
The style rule below will match
#green {color: green}
15
16
The style rule below will match
p#green
Note: Do NOT start an ID name
16
17
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
18
An external style sheet is ideal
With an external style sheet, you
Each page must link to the style
18
19
In an HTML page
<head> <link rel="stylesheet"
In file mystyle.css
hr {color: sienna}
19
20
20
21
21
22
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
23
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
Example <p style="color: sienna; margin-left:
20px"> This is a paragraph </p>
23
24
The CSS background properties
Some styles for background
Set the background color for an
Set an image as the background
25
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
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
27
The CSS text properties define
Some properties that we can set
Color Alignment Lowercase or uppercase
28
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
29
CSS font properties define the font
In CSS, there is two types of font
Generic family - a group of font
Font family - a specific font family (like
30
<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
31
The font-style property is mostly
This property has three values:
normal - The text is shown normally italic - The text is shown in italics oblique - The text is "leaning"
32
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
33
Being able to manage the text size is
However, you should not use font size
Always use the proper HTML tags, like
The font-size value can be an
34
Sets the text to a specified size Does not allow a user to change
Absolute size is useful when the
35
Sets the size relative to surrounding
Allows a user to change the text size
Remark If you do not specify a font
To avoid the resizing problem with
The em size unit is recommended
36
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
37
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
38
All HTML elements can be considered as
In order to set the width and height of
The box model illustrates how the CSS
39
40
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
Content - The content of the box, where text
and images appear
41
When you specify the width and
To know the full size of the
42
An Element
width:250px; padding:10px; border:5px solid gray; margin:10px;
What is a total width of this
250 + 10*2 + 5*2 + 10*2 = 300
43
IE includes padding and border in the
To fix this problem, just add a
<!DOCTYPE html PUBLIC
44
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
45
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
thin, medium, or thick.
45
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>
47
<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
to set the borders first.</p> </body> </html>
48
<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
49
50
<html><head><style type="text/css"> ul.circle {list-style-type:circle} ul.square {list-style-type:square}
roman}
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>
51
W3Schools, “CSS Tutorial”,
http://www.w3schools.com/css/default.asp
51