Java Web Development Course: Writing CSS Codes
Java Web Development Course: Writing CSS Codes
Chapter 4
Writing CSS codes
Contents
What is CSS?
Syntax of CSS
Inclusion
CSS Rules Overriding
Selector Types
Measurement Units
CSS Color
CSS Background
CSS Font
CSS Text
CSS Image
CSS Link
</body>
</html>
Try it out (ID selector)
<!DOCTYPE html>
<html>
<head>
<style>
#para1 {
text-align: center;
color: red;
}
</style>
</head>
<body>
</body>
</html>
Try it out (Class selector)
<!DOCTYPE html>
<html>
<head>
<style>
.center {
text-align: center;
color: red;
}
</style>
</head>
<body>
</body>
</html>
Try it out (Class selector)
<!DOCTYPE html>
<html>
<head>
<style>
p.center {
text-align: center;
color: red;
}
</style>
</head>
<body>
</body>
</html>
Try it out (Descendant selector)
<!DOCTYPE html>
<html>
<head>
<style>
div p {
background-color: yellow;
}
</style>
</head>
<body>
<div>
<p>Paragraph 1 in the div.</p>
<p>Paragraph 2 in the div.</p>
<span><p>Paragraph 3 in the div.</p></span>
</div>
</body>
</html>
Try it out (Child selector)
<!DOCTYPE html>
<html>
<head>
<style>
div > p {
background-color: yellow;
}
</style>
</head>
<body>
<div>
<p>Paragraph 1 in the div.</p>
<p>Paragraph 2 in the div.</p>
<span><p>Paragraph 3 in the div.</p></span> <!-- not Child but Descendant -->
</div>
</body>
</html>
Try it out (Attribute selector)
<!DOCTYPE html>
<html>
<head>
<style>
a[target] {
background-color: yellow;
}
</style>
</head>
<body>
<a href="http://www.w3schools.com">w3schools.com</a>
<a href="http://www.disney.com" target="_blank">disney.com</a>
<a href="http://www.wikipedia.org" target="_top">wikipedia.org</a>
<p><b>Note:</b> For [<i>attribute</i>] to work in IE8 and earlier, a DOCTYPE must be declared.</p>
</body>
</html>
Try it out (Attribute selector)
<!DOCTYPE html>
<html>
<head>
<style>
a[target=_blank] {
background-color: yellow;
}
</style>
</head>
<body>
<a href="http://www.w3schools.com">w3schools.com</a>
<a href="http://www.disney.com" target="_blank">disney.com</a>
<a href="http://www.wikipedia.org" target="_top">wikipedia.org</a>
<p><b>Note:</b> For [<i>attribute</i>] to work in IE8 and earlier, a DOCTYPE must be declared.</p>
</body>
</html>
Measurement Units
Unit Description Example
% Defines a measurement as a p{font-size:16pt; line-
percentage relative to another value, height:125%;}
typically an enclosing element.
cm Defines a measurement in div{margin-bottom:2cm;}
centimeters.
em A relative measurement for the p{letter-spacing:7em;}
height of a font in em spaces.
Because an em unit is equivalent to
the size of a given font, if you assign
a font to 12pt,each "em" unit would
be 12pt; thus, 2em would be 24pt.
ex This value defines a measurement p{font-size:24pt;line-
relative to a font’s x-height. The x- height:3ex;}
height is determined by the height of
the font’s lowercase letter x.
Measurement Units
Unit Description Example
in Defines a measurement in inches. p{word-spacing:.15in;}
mm Defines a measurement in p{word-spacing:15mm;}
millimeters.
pc Defines a measurement in picas. A p{font-size:20pc;}
pica is equivalent to 12 points; thus,
there are 6 picas per inch.
pt Defines a measurement in points. A body{font-size:18pt;}
point is defined as 1/72nd of an inch.
px Defines a measurement in screen p{padding:25px;}
pixels.
Try it out (cm unit)
<!DOCTYPE html>
<html>
<head>
<style>
h1 {font-size: 1.5cm;}
h2 {font-size: 1cm;}
p{
font-size: 0.5cm;
line-height: 1cm;
}
</style>
</head>
<body>
</body>
</html>
Try it out (em unit)
<!DOCTYPE html>
<html>
<head>
<style>
p{
font-size: 16px;
line-height: 1em;
}
div {
font-size: 30px;
border: 1px solid black;
}
span {
font-size: 0.5em;
}
</style>
</head>
<body>
<p>These paragraphs have a calculated line-height of: 2x16px = 32px.</p>
<p>These paragraphs have a calculated line-height of: 2x16px = 32px.</p>
<p>These paragraphs have a calculated line-height of: 2x16px = 32px.</p>
<div>The font-size of the div element is set to 30px. <span>The span element inside the div element has a font-size of 0.5em, which equals to
0.5x30 = 15px</span>.</div>
</body>
</html>
Try it out (ex unit)
<!DOCTYPE html>
<html>
<head>
<style>
div {
font-size: 30px;
border: 1px solid black;
}
span {
font-size: 2ex;
}
</style>
</head>
<body>
<div>The font-size of the div element is set to 20px. <span>The span element inside the div element has a font-size of
1ex</span>.</div>
</body>
</html>
Try it out (px unit)
<!DOCTYPE html>
<html>
<head>
<style>
h1 {font-size: 50px;}
h2 {font-size: 30px;}
p{
font-size: 15px;
line-height: 20px;
}
</style>
</head>
<body>
</body>
</html>
Try it out (px unit)
<!DOCTYPE html>
<html>
<head>
<style>
h1 {font-size: 50px;}
h2 {font-size: 30px;}
p{
font-size: 15px;
line-height: 20px;
}
</style>
</head>
<body>
</body>
</html>
Try it out (pt unit)
<!DOCTYPE html>
<html>
<head>
<style>
h1 {font-size: 50pt;}
h2 {font-size: 25pt;}
p{
font-size: 15pt;
line-height: 25pt;
}
</style>
</head>
<body>
</body>
</html>
CSS Color
Format Syntax Example
Hex Code #RRGGBB p{color:#FF0000;}
Short Hex Code #RGB p{color:#6A7;}
RGB % rgb(rrr%,ggg%,bbb%) p{color:rgb(50%,50%,50
%);}
RGB Absolute rgb(rrr,ggg,bbb) p{color:rgb(0,0,255);}
Keyword Aqua, black, etc. p{color:teal;}
CSS Background
Syntax Description
background-color To set the background color of an element.
background-image To set the background image of an element.
background-repeat To control the repetition of an image in the
background.
background-position To control the position of an image in the
background.
background-attachment To control the scrolling of an image in the
background.
background To define the background.
CSS Font
Syntax Description
font-family To change the face of a font.
font-style To make a font italic or oblique.
font-variant To create a small-caps effect.
font-weight To increase or decrease how bold or light a
font appears.
font-size To increase or decrease the size of a font.
font To specify a number of other font
properties.
CSS Text
Syntax Description
color To change the face of a font.
direction To make a font italic or oblique.
letter-spacing To create a small-caps effect.
word-spacing To increase or decrease how bold or light a font
appears.