Chapter 3 CSS
Chapter 3 CSS
CSS is a web page layout method that has been added to HTML to
give web developers more control over their design and content layout.
Using CSS allows a designer to create a standard set of commands that
controls the style of all subsequent pages.
These commands are embedded inside the web page or from an
external file/page.
Introduction to CSS…
With CSS you can add style (fonts, colors, spacing, and
size) to web documents.
More advanced techniques control the layout of the page
without the use of tables or other cumbersome HTML.
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
The layout of the document was supposed to be taken care of by the browser, without
When tags like <font>, and color attributes were added to the HTML 3.2 specification, it
started a nightmare for web developers.
Development of large web sites, where fonts and color information were added to every
single page, became a long and expensive process.
To solve this problem, W3C created CSS.
In HTML 4.0, all formatting could be removed from the HTML document, and stored in
a separate CSS file.
All browsers support CSS today.
Introduction to CSS…
CSS is a breakthrough in web design because it allows
developers to control the style and layout of multiple web
pages all at once.
As a web developer you can define a style for each HTML
element and apply it to as many web pages as you want.
To make a global change, simply change the style, and all
elements in the Web are updated automatically.
Benefits of CSS
The benefits of using style sheets are:
p {color:red; text-align:center;}
external style.
embedded.
Example:
<h1 style="color: red">Introduction</h1>
Linking CSS to HTML…
II. Creating Embedded Styles
We use the <style> tag for this
<style> tag has a property called type which should be set to text/css for
CSS styling.
You have to save the CSS file with the .css file extension.
You can link to the file externally by placing one of the following
links in the head section of every HTML file you want to style with
the CSS file.
Values:
background color
image
position
repeat
attachment
Or you can set each property individually
Styling Backgrounds…
Background Color
You can specifically declare a color for the background of an element.
background-color: value;
Values:
color name (e.g. red, green, blue, etc.)
hexadecimal number(e.g. #ff0000, #00ff00, #0000ff, etc)
RGB color code (e.g. rgb(255,0,0), rgb(0, 255, 0), rgb(0,0,255), etc)
Transparent
Example:
<style type=”text/css”>
h1 { background-color: rgb(255, 255, 0); }
p { background-color: #0000FF; }
</style>
Background Image
You can set an image for the background of an element .
background-image: url(path_to_image);
Styling Backgrounds…
Background Attachment
Are you using an image as a background?
You can set whether the background scrolls with the page or is fixed when
background-attachment: value;
Values:
fixed
Scroll
Background Repeat
You can set if an image set as a background of an element is to repeat
Background Position
You can position an image used for the background of an element
Example
p { background-image: url("tulips.jpg");
background-position: top right;
}
2. Styling Text
Color
You can set the color of text with the following:
color: value;
Possible values are:
color name – example: red, black…
hexadecimal number – example: #ff0000, #000000
RGB color code – example: rgb(255, 0, 0), rgb(0, 0, 0)
Example:
p {color: blue;}
2. Styling Text…
Letter Spacing
You can adjust the space between letters in the following manner.
Setting the value to 0 prevents the text from justifying.
You can use negative values.
Negative values make the text overlap each other.
letter-spacing: value;
Length
e.g. h2 { letter-spacing: 6; }
2. Styling Text…
Word Spacing
You can adjust the space between words in the following manner. You can use
negative values.
word-spacing: value;
Possible values are
normal
length
Line height
You can set the distance between lines in the following way:
line-height: value;
Possible values are
normal
number
length
Percentage(%)
2. Styling Text…
Text Align
You can align text with the following:
text-align: value;
Examples:
This text is aligned left.
This text is aligned center.
This text is aligned right.
2. Styling Text…
Text Decoration
You can decorate text with the following:
text-decoration: value;
Examples:
This text is underlined.
This text has a line through it.
This text is overlined.
2. Styling Text…
Text Transform
You can control the case of letters in an (X)HTML element with the
following:
text-transform: value;
e.g.
h3 { text-transform: uppercase; }
2. Styling Text…
Text Indent
You can indent the first line of text in an HTML element with the following:
text-indent: value;
direction: value;
Possible values are
ltr (left to right)
rtl (right to left)
2. Styling Text…
unicode-bidi
Possible value:
normal
embed
bidi-override
Example:
div {
direction: rtl;
unicode-bidi: bidi-override;
}
…
<div> Hebrew and Arabic are written from right to left. </div>
white-space: value;
This property declares how white space(tab, space, etc) and line
break(carriage return, line feed, etc.) inside the element is handled.
2. Styling Text…
Values have the following meanings:
normal: makes user agents to collapse sequences of white space, and
break lines as necessary to fill line boxes.
pre: prevents user agents from collapsing sequences of white space.
Lines are only broken at preserved newline characters.
nowrap: This value collapses white space as for ’normal’, but
suppresses line breaks within text.
pre-wrap: This value prevents user agents from collapsing sequences of
white space. Lines are broken at preserved newline characters, and as
necessary to fill line boxes.
pre-line: This value directs user agents to collapse sequences of white
space. Lines are broken at preserved newline characters, and as
necessary to fill line boxes.
3. Styling Fonts
Font
The font property can set the style, weight, variant, size, line
Example:
font: italic bold normal small/1.4em Verdana, sans-serif;
family property.
There are 2 choices for values:
family-name
generic family
If you set a family name it is best to also add the generic family at
the end as this is a prioritized list.
So if the user does not have the specified font name, it will use the
same generic family.
font-family: Verdana, sans-serif;
3. Styling Fonts…
Font Size
You can set the size of the text used in an element by using the font-size property.
font-size: value;
relative and
absolute.
The absolute units consist of the physical units (in, cm, mm, pt, pc) and the px unit:
in: inches — 1in is equal to 2.54cm.
cm: centimeters
mm: millimeters
pt: points — the points used by CSS are equal to 1/72nd of 1in.
pc: picas — 1pc is equal to 12pt.
px: pixel units — 1px is equal to 0.75pt.
3. Styling Fonts…
Example: all the following are possible
h1 { margin: 0.5in } /* inches */
h2 { line-height: 3cm } /* centimeters */
h3 { word-spacing: 4mm } /* millimeters */
h4 { font-size: 12pt } /* points */
h4 { font-size: 1pc } /* picas */
p { font-size: 12px } /* px */
Example:
body
{
font-size: 12px;
text-indent: 3em; /* i.e. 36px - based on font-size*/
}
3. Styling Fonts…
The ex means the x-height.
The x-height is so called because it is often equal to the
height of the lowercase ‘x’.
However, an ex is defined even for fonts that do not
contain an ‘x’.
3. Styling Fonts…
The x-height of a font can be found in different ways.
Some fonts contain reliable metrics for the x-height.
If reliable font metrics are not available, user agents may determine the x-
height from the height of a lowercase glyph.
In the cases where it is impossible or impractical to determine the x-height,
a value of 0.5em should be used.
Example:
h1 { margin: 0.5em } /* em */
h1 { margin: 1ex } /* ex */
Example:
h1 { line-height: 1.2em }
This means the line height of ‘h1’ elements will be 20% greater than the
font size of the h1 elements.
3. Styling Fonts…
On the other hand:
h1 { font-size: 1.2em }
This means that the font-size of h1 elements will be
20% greater than the font size inherited by h1
elements.
font-style: value;
Font Variant
You can set the variant of text within an element with the font-variant property:
font-variant: value;
font-weight: value;
Example:
body { font-weight: 600;}
p { font-weight: bolder } /* this is based on 600 and it changes to 900*/
4. Styling Links
User agents commonly display unvisited links differently from
previously visited ones.
CSS provides the pseudo-classes ’a:link’ and ’a:visited’ to distinguish
them:
The a:link pseudo-class applies for links that have not yet been visited.
The a:visited pseudo-class applies once the link has been visited by the user.
The a:hover pseudo-class applies while the user designates an element with
some pointing device, but does not activate it. For example, a visual user
agent could apply this pseudo-class when the mouse pointer hovers over a
box generated by the element.
The a:active pseudo-class applies while an element is being activated by the
user. For example, between the times the user presses the mouse button and
releases it.
The a:focus pseudo-class applies while an element has the focus (accepts
keyboard events or other forms of text input).
4. Styling Links…
Below are the various ways you can use CSS to style links.
a:link {
color: #009900;
text-decoration: overline;
}
a:visited {color: #999999;}
a:hover {color: #333333;}
a:focus {color: #333333;}
a:active {color: #009900;}
4. Styling Links…
Remark: You must declare the a:link and a:visited
before you declare a:hover.
Furthermore, you must declare a:hover before you
can declare a:active.
Using the above code will style all links on your web
page, unless you declare a separate set of link styles
for a certain area of your webpage.
Look at pseudo-classes sub-section for more.
5. Styling Lists
List Style
You can control the appearance of ordered and unordered
Values:
image
position
type
position property
list-style-position: value;
Values
inside
outside
5. Styling Lists…
List Style Type
You can control the type of bullet ordered and unordered lists use with the list-style-type property
list-style-type: value;
Values
none
disc
circle
square
decimal
decimal-leading-zero
lower-roman
upper-roman
lower-alpha
upper-alpha
lower-greek
lower-latin
upper-latin
armenian
Georgian
6. Styling Tables
Table Borders
To specify table borders in CSS, use the border property.
The example below specifies a black border for table, th, and
td elements:
table, th, td{
border: 1px solid black;
}
Notice that the table in the example above has double borders.
This is because both the table, th, and td elements have
separate borders.
6. Styling Tables…
Collapse Borders
The border-collapse property sets whether the table borders
The text-align property sets the horizontal alignment, like left, right, or center:
td{
text-align: right;
}
The vertical-align property sets the vertical alignment, like top, bottom, or
middle:
td {
height: 50px;
vertical-align: bottom;
}
6. Styling Tables…
Table Width and Height
Width and height of a table is defined by the width and height
properties.
The example below sets the width of the table to 100%, and
td and th elements:
td{
padding:15px;
}
Table Color
The color of the borders, and the text and background color of th elements can be specified:
I. Generic Classes
Their selector starts with a dot (.), which states that it is a class.
To apply a class to a certain HTML element, use its class attribute where
you state the class name without the dot.
6. CSS Class…
<div class="emphasis">The big match today …</div>
<i class="boooring">This sentence looks boring</i>
Selectors of these classes start with the HTML element name, followed with
These classes can be applied only to a specified HTML element, in this case a
DIV element.
<div class="big">Big bold text.</div>
<span class="big">Normal text - class not applied.</span>
6. CSS Class…
Example: in your paragraph, you put this:
<span class="greenboldtext">sentence</span>
The margin does not have a background color, and it is completely transparent
Values:
width
style
color
Values:
color name
hexadecimal number
RGB color code
transparent
7. CSS Box Model…
Border Style
You can set the style of a border independently with the border-style property.
border-style: value;
Values:
dashed
dotted
double
groove
hidden
inset
none
outset
ridge
solid
7. CSS Box Model…
7. CSS Box Model…
Border Width
You can set the width of a border independently with the border-
width property.
border-width: value;
Values:
Length
Thin
Medium
Thick
Or you can set the elements for each borders side individually
7. CSS Box Model…
Border Bottom
You can set the color, style and width of the bottom border around an element in one declaration
Values:
Width
style
color
Or you can set each value individually
property.
border-bottom-color: value;
You can set the style of the bottom border around an element with the border-bottom-style
property:
border-bottom-style: value;
7. CSS Box Model…
Border Bottom Width
You can set the width of the bottom border around an element with the border-bottom-
width property.
border-bottom-width: value;
Border Left
You can set the color, style and width of the left border around an element with the
border-left property.
border-left: 1px solid #333333;
Values:
style
Width
color
Values:
color
style
width
Or you can set each value individually
7. CSS Box Model: CSS Margin
The margin clears an area around an element outside the
border.
The margin does not have a background color, and is
completely transparent.
Property Description Values
Margin A shorthand property for setting the margin-top
margin properties in one declaration margin-right
margin-bottom
margin-left
margin-bottom Sets the bottom margin of an element auto| length | %
Syntax:
padding: length | percentage | auto | inherit
Examples:
padding: 10px; /* Applied to all sides. */
padding: 10px 6px; /* First is top & bottom, second is left & right. */
7. CSS Box Model: CSS
Padding…
Property Description Values
Padding A shorthand property for setting all the padding-top
padding properties in one declaration padding-right
padding-bottom
padding-left
A value none hides an element, and it will not take up any space.
The element will be hidden, and the page will be displayed as if the
element is not there.
A block element is an element that takes up the full width available, and
has a line break before and after it.
Examples of block elements:
<h1>
<p>
<div>
CSS Display and Visibility…
An inline element only takes up as much width as necessary,
and does not force line breaks.
Examples of inline elements:
<span>
<a>
Example:
li { display: inline; }
<html>
<head>
<title>Display and Visibility</title>
<style type="text/css">
li { display: inline;}
a { display: block; }
</style>
</head>
<body>
There are different types of mini computers:
<ol>
<li>Desktop</li>
<li>Laptop</li>
<li>Palmtop</li>
</ol>
<a href="here.html"> first link </a>
<a href="there.html"> second link </a>
</body>
</html>
CSS Display and Visibility…
CSS Display and Visibility…
The visibility property specifies if an element should be
visible or hidden.
visibility: hidden | visible;
layouts.
The syntax is:
Syntax:
relative: Relative positioning moves the box relative to its original position in
the flow.
The distinctive behavior of relative positioning is that the space the element
would have occupied in the normal flow is preserved.
CSS Display and Visibility…
absolute: Absolutely positioned elements are removed from the document flow
entirely and positioned relative to a containing element.
Unlike relatively positioned elements, the space they would have occupied is
fixed: The distinguishing characteristic of fixed positioning is that the element
stays in one position in the window even when the document scrolls.
Fixed elements are removed from the document flow and positioned relative to
Example:
<html>
<head>
<style type=”text/css”>
span {
position: relative;
top: 30px; left: 60px;
background-color: fuchsia;
}
</style>
</head>
<body>
<p> Along the road he came upon a man who had <span>never worn any trouser </span>, and who
was trying to put on a pair. So he had fastened them to tree … </p>
</body>
</html>
CSS Display and Visibility…
CSS Display and Visibility…
Z-index
When elements are positioned outside the normal flow, they
Syntax:
z-index: value| auto;
An element can have a positive or negative stack order.
Syntax:
cursor: url | auto | crosshair | default | pointer | move | e-
resize | ne-resize | nw-resize | n-resize | se-resize |
sw-resize | s-resize | w-resize | text | wait | help;