L2 CSS
L2 CSS
Machbah Uddin
Assistant Professor
Department of Computer Science and
Mathematics
Bangladesh Agricultural University,
Mymensingh
Email: [email protected]
The good, the bad and
2
the… ugly!
<p>
<font face="Arial">Shashdot.</font>
News for <b>nerds!!</b> You will <i>never</i>, <u>EVER</u>
be
<font size="+4" color="red">BORED</font> here!
</p> HTML
output
Tags such as b, i, u, and font are
discouraged in strict XHTML
Why is this bad?
Cascading Style Sheets
3
(CSS)
Describes the appearance, layout, and
presentation of information on a web
page
HTML describes the content of the page
Describes how information is to be
displayed, not what is being displayed
Can be embedded in HTML document or
placed into separate .css file
Basic CSS rule syntax
4
selector {
property: value;
property: value;
...
property: value;
}
CSS
p {
font-family: sans-serif;
color: red;
}
CSS
A CSS file consists of one or more rules
Each rule starts with a selector
A selector specifies an HTML element(s) and then
applies style properties to them
a selector of * selects all elements
Attaching a CSS file <link>
5
<head>
...
<link href="filename" type="text/css" rel="stylesheet" />
...
</head> HTML
HTML
A page can link to multiple style sheet files
In case of a conflict (two sheets define a style for the
same HTML element), the latter sheet's properties will
be used
Embedding style sheets:
6
<style>
<head>
<style type="text/css">
p { font-family: sans-serif; color: red; }
h2 { background-color: yellow; }
</style>
</head>
HTML
HTML
This is a paragraph
output
p {
color: red;
background-color: yellow;
}
CSS
This paragraph uses the style above
output
property description
color color of the element's text
color that will appear behind
background-color
the element
Specifying colors
9
p { color: red; }
h2 { color: rgb(128, 0, 196); }
h4 { color: #FF8800; }
CSS
p, h1, h2 {
color: green;
}
h2 {
background-color: yellow;
} CSS
output
A style can select multiple elements separated by
commas
The individual elements can also have their own
styles
CSS comments /*…*/
11
/* This is a comment.
It can span many lines in the CSS file. */
p {
color: red; background-color: aqua;
} CSS
property description
font-family which font will be used
how large the letters will be
font-size
drawn
used to enable/disable italic
font-style
style
used to enable/disable bold
font-weight
style
omplete list of font properties (http://www.w3schools.com/css/css_reference.asp#
font-family
13
p {
font-family: Georgia;
}
h2 {
font-family: "Courier New";
} CSS
p {
font-family: Garamond, "Times New Roman", serif;
} CSS
output
We can specify multiple fonts from highest to
lowest priority
Generic font names:
serif, sans-serif, cursive, fantasy, monospace
p {
font-size: 24pt;
} CSS
p {
font-size: 24pt;
} CSS
CS380
font-weight, font-style
17
p {
font-weight: bold;
font-style: italic;
} CSS
output
Either of the above can be set to normal to turn
them off (e.g. headings)
CS380
CSS properties for text
18
property description
alignment of text within its
text-align
element
decorations such as
text-decoration
underlining
line-height,
gaps between the various
word-spacing,
portions of the text
letter-spacing
indents the first letter of each
text-indent
paragraph
omplete list of text properties (http://www.w3schools.com/css/css_reference.asp#
text-align
19
We wants it, we needs it. Must have the precious. They stole it from us.
Sneaky little hobbitses. Wicked, tricksy, false!
output
text-align can be left, right, center, or
justify
text-decoration
20
p {
text-decoration: underline;
}
CSS
This paragraph uses the style above.
output
can also be overline, line-through, blink,
or none
effects can be combined:
ol { list-style-type: lower-roman; }
CSS
Possible values:
i. none : No marker
ii. disc (default), circle, square
iii. Decimal: 1, 2, 3, etc.
iv. decimal-leading-zero: 01, 02, 03, etc.
v. lower-roman: i, ii, iii, iv, v, etc.
vi. upper-roman: I, II, III, IV, V, etc.
vii. lower-alpha: a, b, c, d, e, etc.
viii. upper-alpha: A, B, C, D, E, etc.
x. lower-greek: alpha, beta, gamma, etc.
others: hebrew, armenian, georgian, cjk-ideographic,
Body styles
22
body {
font-size: 16px;
}
CSS
CSS
This is a heading
A styled paragraph. Previous slides are available on the website.
• A bulleted list
output
when multiple styles apply to an element, they
are inherited
a more tightly matching rule can override a more
general inherited rule
Styles that conflict
25
CSS
This paragraph uses the first style above.
This heading uses both styles above.
output
<p>
<a
href="http://jigsaw.w3.org/css-validator/check/referer">
<img src="http://jigsaw.w3.org/css-validator/images/vcss"
alt="Valid CSS!" /></a>
</p> CSS
output
jigsaw.w3.org/css-validator/
checks your CSS to make sure it meets the
official CSS specifications
CSS properties for
27
backgrounds
property description
background-color color to fill background
background-image image to place in background
placement of bg image within
background-position
element
whether/how bg image should
background-repeat
be repeated
whether bg image scrolls with
background-attachment
page
shorthand to set all
background
background properties
background-image
28
body {
background-image: url("images/draft.jpg");
}
CSS
body {
background-image: url("images/draft.jpg");
background-repeat: repeat-x;
}
CSS
body {
background-image: url("images/draft.jpg");
background-repeat: no-repeat;
background-position: 370px 20px;
} CSS
Fixedwidth.css
Fixedwidth.html