Cascading Style Sheets (CSS)
Cascading Style Sheets (CSS)
CSS Introduction
• Cascading Style Sheets (CSS)
– Used to describe the presentation of documents
– Define sizes, spacing, fonts, colors, layout, etc.
– Improve content accessibility
– Improve flexibility
• Designed to separate presentation from content
• Due to CSS, all HTML presentation tags and
attributes are deprecated, e.g. font, center,
etc.
Style Sheets Syntax
• Style-sheets consist of rules, selectors, declarations,
properties and values
– By element id:
#element_id { color: #ff0000; }
– By element class name (only for HTML):
.myClass {border: 1px solid red}
This will match <h1> tags, elements with class link, and
element with id top-link
• Pseudo-classes define state
– :hover, :visited, :active , :lang
• Pseudo-elements define element "parts" or
are used to generate content
– :first-line , :before, :after
This will match all <a> tags that are inside of <p>
* – universal selector (avoid or use with care!):
p * {color: black}
a { text-decoration: none }
li em { color: red;
font-weight: bold }
ul { margin-left: 2cm }
ul ul { text-decoration: underline;
margin-left: .5cm }
External Styles: Example
external-styles.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-
transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Importing style sheets</title>
<link type="text/css" rel="stylesheet"
href="styles.css" />
</head>
<body>
<h1>Shopping list for <em>Monday</em>:</h1>
<li>Milk</li>
…
…
<li>Bread
<ul>
<li>White bread</li>
<li>Rye bread</li>
<li>Whole wheat bread</li>
</ul>
</li>
<li>Rice</li>
<li>Potatoes</li>
<li>Pizza <em>with mushrooms</em></li>
</ul>
<a href="http://food.com" title="grocery
store">Go to the Grocery store</a>
</body>
</html>
Output:
…
<li>Bread
<ul>
<li>White bread</li>
<li>Rye bread</li>
<li>Whole wheat bread</li>
</ul>
</li>
<li>Rice</li>
<li>Potatoes</li>
<li>Pizza <em>with mushrooms</em></li>
</ul>
<a href="http://food.com" title="grocery
store">Go to the Grocery store</a>
</body>
</html>
Text-related CSS Properties
• color – specifies the color of the text
• font-size – size of font: xx-small, x-small, small, medium,
large, x-large, xx-large, smaller, larger or numeric value
• font-family – comma separated font names
– Example: verdana, sans-serif, etc.
– The browser loads the first one that is available
– There should always be at least one generic font
• font-weight can be normal, bold, bolder, lighter or a
number in range [100 … 900]
• font-style – styles the font
– Values: normal, italic, oblique
• text-decoration – decorates the text
– Values: none, underline, line-trough, overline, blink
• text-align – defines the alignment of text or other
content
– Values: left, right, center, justify
Shorthand Font Property
font
– Shorthand rule for setting multiple font properties at
the same time
font:italic normal bold 12px/16px verdana
is equal to writing this:
font-style: italic;
font-variant: normal;
font-weight: bold;
font-size: 12px;
line-height: 16px;
font-family: verdana;
Backgrounds
• background-image
– URL of image to be used as background, e.g.:
background-image:url("back.gif");
• background-color
– Using color and image and the same time
• background-repeat
– repeat-x, repeat-y, repeat, no-repeat
• background-attachment
– fixed / scroll
• background-position: specifies vertical and horizontal
position of the background image
– Vertical position: top, center, bottom
– Horizontal position: left, center, right
– Both can be specified in percentage or other
numerical values
Examples:
background-position: top left;