CSS For Styling
CSS For Styling
HTML
2
<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
CS380
Cascading Style Sheets (CSS)
3
CS380
With style sheets we can
4
HTML
This is a paragraph
output
CS380
Advantages of CSS
6
CSS saves time - You can write CSS once and then
reuse same sheet in multiple HTML pages. You can
define a style for each HTML element and apply it
to as many web pages as you want.
Pages load faster - If you are using CSS, you do
not need to write HTML tag attributes every time.
Just write one CSS rule of a tag and apply to all the
occurrences of that tag. So, less code means faster
download times.
Advantages of CSS
7
Easy maintenance - To make a global change,
simply change the style(s) in CSS file, and all
elements in all the web pages will be updated
automatically.
Superior styles to HTML - CSS has a much wider
array of attributes than HTML so you can give far
better look to your HTML page in comparison of
HTML attributes. Multiple device compatibility -
Style sheets allow content to be optimized for more
than one type of device.
Create dynamic pages: With CSS, anyone can
easily animate HTML elements with just a couple
Disadvantages of CSS
8
selector {
property: value;
property: value;
...
property: value;
} CSS
p {
font-family: sans-serif;
color: red;
} CSS
Syntax of CSS style rule is as follows:
12
<head>
...
<link href="filename" type="text/css" rel="stylesheet" />
...
</head> HTML
CS380
Ex:
<HTML>
<HEAD>
<STYLE>
P, H2, H3
{
TEXT-ALIGN : CENTER;
COLOR: BULE;
FONT-WEIGHT: BOLD;
}
</STYLE>
</HEAD>
<BODY>
<H2> HEADING-2 IS AFFECTED BY STYLE </H2>
<H3> HEADING-3 IS AFFECTED BY STYLE </H3>
<P> THIS PARAGRAPH IS AFFECTED BY STYLE </P>
</BODY>
15
</HEAD>
16 CS380
CSS properties for colors
17
p {
color: red;
background-color: yellow;
}
CSS
property description
color color of the element's text
color that will appear behind the
background-color
element
CS380
Specifying colors
18
p { color: red; }
h2 { color: rgb(128, 0, 196); }
h4 { color: #FF8800; }
CSS
p, h1, h2 {
color: green;
}
h2 {
background-color: yellow;
} CSS
CS380
CSS comments /*…*/
20
/* This is a comment.
It can span many lines in the CSS file. */
p {
color: red; background-color: aqua;
} CSS
CS380
CSS properties for fonts
21
property description
font-family which font will be used
font-size how large the letters will be drawn
font-style used to enable/disable italic style
font-weight used to enable/disable bold style
CS380
font-family
22
p {
font-family: Georgia;
}
h2 {
font-family: "Courier New";
} CSS
CS380
More about font-family
23
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
26
p {
font-weight: bold;
font-style: italic;
} CSS
Either of the above can be set to normal to turn them off (e.g.
headings)
CS380
CSS properties for text
27
property description
text-align alignment of text within its element
text-decoration decorations such as underlining
line-height,
gaps between the various portions of
word-spacing,
the text
letter-spacing
indents the first letter of each
text-indent
paragraph
CS380
text-align
28
We wants it, we needs it. Must have the precious. They stole it from us.
Sneaky little hobbitses. Wicked, tricksy, false!
output
CS380
text-decoration
29
p {
text-decoration: underline;
} CSS
CS380
The list-style-type property
30
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, hiragana…
Body styles
31
body {
font-size: 16px;
}
CSS
CS380
Cascading Style Sheets
32
CS380
Inheriting styles
33
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
CS380
Styles that conflict
34
CSS
This paragraph uses the first style above.
This heading uses both styles above.
output
when two styles set conflicting values for the same property,
the latter style takes precedence
CS380
CSS properties for backgrounds
35
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 be
background-repeat
repeated
background-attachment whether bg image scrolls with page
shorthand to set all background
background
properties
CS380
background-position
36
body {
background-image: url("images/draft.jpg");
background-repeat: no-repeat;
background-position: 370px 20px;
} CSS
.class1.class2 .name1.name2 Selects all elements with both name1 and name2 set
within its class attribute
.class1 .class2 .name1 .name2 Selects all elements with name2 that is a descendant
of an element with name1
[attribute$= a[href$=".pdf"] Selects every <a> element whose href attribute value
value] ends with ".pdf"
40
Id and Class Selectors
41
42
43 CS380
Class Selector
44
CS380
<HTML>
<HEAD>
<STYLE>
.CLS
{
TEXT-ALIGN: CENTER;
}
</STYLE>
</HEAD>
<BODY>
<H2 CLASS="CLS">
CENTER-ALIGNED HEADING
</H2>
<P CLASS="CLS">CENTER-ALIGNED PARAGRAPH.
</P>
</BODY>
</HTML>
45
46 CS380