Web Design Using CSS
Web Design Using CSS
What is CSS?
CSS stands for Cascading Style Sheets
CSS describes how HTML elements are to
be displayed on screen, paper, or in other
media
CSS saves a lot of work. It can control the
layout of multiple web pages all at once
G-11
Introduction CSS
3
G-11
CSS Syntax
4
ID Selector
The id selector uses the id attribute of an HTML
element to select a specific element.
To select an element with a specific id, write a
hash (#) character, followed by the id of the
element.
Example
The CSS rule below will be applied to the HTML
element with id="para1":
#para1 {
color: red;
text-align: center; G-11
CSS Selectors
9
Class Selector
The class selector selects HTML elements with
a specific class attribute.
To select elements with a specific class, write a
period (.) character, followed by the class
name.
Example
In this example all HTML elements with
class="center" will be red and center-aligned:
.para1 {
color: red;
text-align: center; G-11
CSS Selectors
10
Universal Selector
The universal selector (*) selects all HTML
elements on the page.
Example
The CSS rule below will affect every HTML
element on the page:
* {
color: blue;
text-align: center;
}
G-11
CSS Selectors
11
Grouping Selector
The grouping selector selects all the HTML
elements with the same style definitions.
Look at the following CSS code (the h1, h2, and
p elements have the same style definitions):
Example
In this example we have grouped the selectors from
the code above:
P, h1, h2{
color: red;
text-align: center;
} G-11
CSS Selectors
12
Attribute Selector
It is possible to style HTML elements that have
specific attributes or attribute values.
The [attribute] selector is used to select
elements with a specified attribute.
The following example selects all <a>
elements with a target attribute:
<a href="http://www.wikipedia.org/"
target="_top">wikipedia.org</a>
a[target] {
background-color: yellow;
G-11
}
How To Add CSS
13
sheet:
1. External CSS
2. Internal CSS
3. Inline CSS
G-11
How To Add CSS
14
External CSS
Each HTML page must include a reference
HTML tags.
G-11
How To Add CSS
15
Example
Sample.html mystyle.css
<!DOCTYPE html>
<html> body {
<head> background-color:
<link rel="stylesheet" lightblue;
href="mystyle.css"> }
</head>
<body> h1 {
<h1>This is a color: navy;
heading</h1> margin-left: 20px;
<p>This is a }
paragraph.</p>
</body> G-11
How To Add CSS
16
Internal CSS
An internal style sheet may be used if one
single HTML page has a unique style.
The internal style is defined inside the <style>
element, inside the head section.
Example
<head>
body {
background-color: linen; }
h1 {
color: maroon;
margin-left: 20px; }
G-11
</head>
How To Add CSS
17
Inline CSS
An inline style may be used to apply a unique
style for a single element.
To use inline styles, add the style attribute to
the relevant element
Example
<h1 style="color: blue; text-align: center;">This is a
heading</h1>
<p style="color: red;“>This is a paragraph.</p>
G-11
CSS Comments
18
Comments are used to explain the code, and
may help when you edit the source code at a
later date.
Comments are ignored by browsers.
A CSS comment is placed inside the <style>
element, and starts with /* and ends with */:
c
/* This is a single-line comment */
p{
color: red; /* Set text color to red */
}
G-11
CSS Colors
19
Colors are specified using predefined color
names, or values( RGB, HEX, HSL, RGBA,
HSLA).
CSS Color Names
In CSS, a color can be specified by using a
predefined color name:
Example
<h1 style="color: white; background-color:
lightblue;">This is a heading</h1>
<p style="color: red;“>This is a paragraph.</p>
G-11
CSS Colors
20
RGB Colors
An RGB color value represents RED, GREEN, and BLUE
light sources.
In CSS, a color can be specified as an RGB value, using
this formula:
rgb(red, green, blue)
Each parameter (red, green, and blue) defines the
intensity of the color between 0 and 255.
For example, rgb(255, 0, 0) is displayed as red, because
red is set to its highest value (255) and the others are set
to 0.
To display black, set all color parameters to 0, like this:
rgb(0, 0, 0).
To display white, set all color parameters to 255, like this:
G-11
CSS Colors
21
RGBA Colors
RGBA color values are an extension of RGB color
values with an alpha channel - which specifies the
opacity for a color.
An RGBA color value is specified with:
rgba(red, green, blue, alpha)
The alpha parameter is a number between 0.0
(fully transparent) and 1.0 (not transparent at all):
Example
Experiment by mixing the RGBA values below:
style="background-color:rgba(255, 0, 0, 0.3);“
style="background-color:rgba(255, 0, 0, 0.9);"G-11
CSS Colors
22
HEX Colors
A hexadecimal color is specified with: #RRGGBB,
where the RR (red), GG (green) and BB (blue)
hexadecimal integers specify the components of
the color.
In CSS, a color can be specified using a
hexadecimal value in the form:
#rrggbb
Hexadecimal values between 00 and ff (same as
decimal 0-255).
Example, #ff0000= red, #000000= black,
#ffffff= white
G-11
CSS Colors
23
HSL Colors
HSL stands for hue, saturation, and lightness.
In CSS, a color can be specified using hue,
saturation, and lightness (HSL) in the form:
hsl(hue, saturation, lightness)
Hue is a degree on the color wheel from 0 to 360. 0
is red, 120 is green, and 240 is blue.
Saturation is a percentage value, 0% means a
shade of gray, and 100% is the full color.
Lightness is also a percentage, 0% is black, 50% is
neither light or dark, 100% is white
G-11
CSS Colors
24
Example
style="background-color:hsl(0, 100%, 50%);"
style="background-color:hsl(240, 100%, 50%);“
style="background-color:hsl(147, 50%, 47%);“
style="background-color:hsl(300, 76%, 72%);“
style="background-color:hsl(39, 100%, 50%);“
style="background-color:hsl(248, 53%, 58%);"
G-11
CSS Colors
25
HSLA Value
HSLA color values are an extension of HSL color
values with an alpha channel - which specifies the
opacity for a color.
An HSLA color value is specified with:
G-11
CSS Backgrounds
27
Background-color
The background-color property specifies the
background color of an element.
Example
body {
background-color: lightblue;
}
h1 {
background-color: green;
}
G-11
CSS Backgrounds
28
Opacity / Transparency
The opacity property specifies the
opacity/transparency of an element.
It can take a value from 0.0 - 1.0.
Example
body {
background-color: lightblue;
opacity: 0.3;
}
G-11
CSS Backgrounds
29
Background Image
The background-image property specifies an image
to use as the background of an element.
Example
body {
background-image: url(“sample.png");
}
The background image can also be set for specific
elements, like the <p> element:
Example
p{
background-image: url(“sample.png"); } G-11
CSS Backgrounds
30
Background-repeat
By default, the background-image property repeats
an image both horizontally and vertically.
Some images should be repeated only horizontally
or vertically, or they will look strange, like this:
If the image above is repeated only horizontally
(background-repeat: repeat-x;), the background
will look better.
To repeat an image vertically, set (background-
repeat: repeat-y;).
Showing the background image only once is also
specified by the (background-repeat: no-repeat;)
property: G-11
CSS Backgrounds
31
Example
Horizontally Repeat
body {
background-image: url(“sample.png");
background-repeat: repeat-x; }
Vertically Repeat
body {
background-image: url(“sample.png");
background-repeat: repeat-y; }
Image only once
body {
background-image: url(“sample.png");
background-repeat: no-repeat;
} G-11
CSS Backgrounds
32
Background-position
The background-position property is used to
specify the position of the background image.
Example
body {
background-image: url(“sample.png");
background-repeat: no-repeat;
background-position: right top;
}
G-11
CSS Backgrounds
33
Background-attachment
The background-attachment property specifies
whether the background image should scroll or be
fixed (will not scroll with the rest of the page)
Example
Specify that the background image should be
fixed:
background-attachment: fixed;
Specify that the background image should scroll
Background Shorthand
To shorten the code, it is also possible to specify all
the background properties in one single property.
This is called a shorthand property.
Example
body { Shorthand
body {
background-color: #ffffff;
background-image: Background: #ffffff
url(“sample.png"); url(“sample.png") no-repeat right
background-repeat: no-repeat; top;
background-position: right top;
}
}
G-11
CSS Borders
35
The CSS border properties allow you to specify the
style, width, and color of an element's border.
Border Style
The border-style property specifies what kind of
border to display.
The border-style property can have from one to
four values (for the top border, right border,
bottom border, and the left border).
The following values are allowed:
dotted - Defines a dotted border
dashed - Defines a dashed border
solid - Defines a solid border
double - Defines a double border G-11
CSS Borders
36
dotted - Defines a dotted border
groove - Defines a 3D grooved border. The effect depends
on the border-color value
ridge - Defines a 3D ridged border. The effect depends on
the border-color value
inset - Defines a 3D inset border. The effect depends on
the border-color value
outset - Defines a 3D outset border. The effect depends on
the border-color value
none - Defines no border
hidden - Defines a hidden border
G-11
CSS Borders
37
Border Width
The border-width property specifies the width of
the four borders.
The width can be set as a specific size (in px, pt,
Border Color
The border-color property is used to set the color of
the four borders.
The color can be set by: name - specify a color
}
G-11
CSS Borders
40
}
G-11
CSS Borders
41
Rounded Borders
The border-radius property is used to add rounded
borders to an element:
Example
P{
border: 5px solid blue;
border-radius: 5px:
}
G-11
CSS Margins
44
The CSS margin properties are used to create space around
elements, outside of any defined borders.
With CSS, you have full control over the margins. CSS has
properties for specifying the margin for each side of an
element:
margin-top
margin-right
margin-bottom
margin-left
Example
P{ margin-top: 100px
margin-right: 100px
margin-bottom: 150px
margin-left: 80px
border-style: 5px solid red; G-11
CSS Margins
45
G-11
CSS Box Model
51
G-11
CSS Box Model
52
Example
div {
width: 300px;
border: 15px solid green;
padding: 50px;
margin: 20px;
}
G-11
CSS Links
53
With CSS, links can be styled in many different
ways.
Styling Links
Links can be styled with any CSS property (e.g.
Example
<a href= “Registration.html" > This is a link</a>
G-11
CSS Links
55
a:visited
a:active MUST come after a:hover
G-11
CSS Links
56
Text Decoration
The text-decoration property is mostly used to
Background Color
The background-color property can be used to
Link Buttons
This example demonstrates a more advanced