This document provides an overview of Cascading Style Sheets (CSS) in 3 sections:
1. It introduces CSS and its uses for controlling layout and styling across multiple web pages. External CSS files allow changing the look of an entire website by editing just one file.
2. Various CSS selectors are described for selecting HTML elements based on name, id, class and other attributes. Common selectors include the element, id, class, and grouping selectors.
3. Different CSS properties are outlined for styling text, colors, backgrounds, borders, padding, margins and other element features. Examples are given for setting colors, images, sizes and other styles using CSS.
2. Introduction
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
External stylesheets are stored in CSS files
3. Introduction (Contd.)
Why Use CSS?
Define styles for your web pages
Variations in display for different devices and
screen sizes.
CSS Saves a Lot ofWork!
The style definitions are normally saved in
external .css files.
With an external stylesheet file, you can
change the look of an entire website by
changing just one file!
4. CSS Syntax
A CSS rule-set consists of a selector and a
declaration block:
selector points to the HTML element you want to
style.
declaration block contains one or more
declarations separated by semicolons
Each declaration includes a CSS property name
and a value, separated by a semi-colon
5. CSS Selectors
CSS selectors are used to "find" (or
select) HTML elements based on their
element name, id, class, attribute, and
more.
◦ The element Selector
◦ The id Selector
◦ The class Selector
◦ Grouping Selectors
6. 1.The element Selector
The element selector selects elements based
on the element name.
You can select all <p> elements on a page
like this:
p {
text-align: center;
color: red;
}
7. Example: Element Selector
<!DOCTYPE html>
<html>
<head>
<style>
p {
color: red;
text-align: center;
}
</style>
</head>
<body>
<p>HelloWorld!</p>
<p>These paragraphs are styled with CSS.</p>
</body>
</html>
8. 2. Id Selector
The id selector uses the id attribute of an HTML element to
select a specific element.
id of an element should be unique within a page, so
the id selector is used to select one unique element!
To select an element with a specific id, write a hash (#)
character, followed by the id of the element.
The style rule below will be applied to the HTML element
with id="para1":
#para1 {
text-align: center;
color: red;
}
9. Example: Id Selector
<!DOCTYPE html>
<html>
<head>
<style>
#para1 {
text-align: center;
color: red;
}
</style></head>
<body>
<p id="para1“>HelloWorld!</p>
<p>This paragraph is not affected by the style.</p>
</body> </html>
10. 3. Class Selector
The class selector selects elements with a
specific class attribute.
To select elements with a specific class, write a
period (.) character, followed by the name of
the class.
Example:
.center {
text-align: center;
color: red;
}
12. 4. Grouping Selectors
If you have elements with the same style
definitions, like this:
h1 {
text-align: center;
color: red;
}
h2 {
text-align: center;
color: red;
}
p {
text-align: center;
color: red;
}
13. Grouping Selectors (Contd.)
It will be better to group the selectors, to
minimize the code.
To group selectors, separate each selector
with a comma.
In the example below we have grouped the
selectors from the code above:
h1, h2, p {
text-align: center;
color: red;
}
14. Example: Group Selector
<!DOCTYPE html>
<html>
<head>
<style>
h1, h2, p {
text-align: center;
color: red;
}
</style>
</head>
<body>
<h1>Hello World!</h1>
<h2>Smaller heading!</h2>
<p>This is a paragraph.</p>
</body> </html>
15. How to Apply CSS
When a browser reads a style sheet, it
will format the HTML document
according to the information in the style
sheet.
ThreeWays to Insert CSS
◦ External style sheet
◦ Internal style sheet
◦ Inline style
16. 1. External Style Sheet
The <link> element include a reference to the external style sheet
goes inside the <head> section:
<! DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body> </html>
18. 2. Internal Style Sheet
An internal style sheet may be used if
one single page has a unique style
Internal styles are defined within the
<style> element, inside the <head>
section of an HTML page
19. 2. Internal Style Sheet (Example)
<!DOCTYPE html>
<html><head>
<style>
body {
background-color: linen;
}
h1 {
color: maroon;
margin-left: 40px;
}
</style> </head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body> </html>
20. 3. Inline Styles
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.
The style attribute can contain any CSS
property.
<!DOCTYPE html>
<html>
<body>
<h1 style="color:blue;margin-left:30px;">This is a
heading</h1>
<p>This is a paragraph.</p>
</body> </html>
22. 1. Background Color
The background-color property specifies the background color
of an element.
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-color: lightblue;
}
</style>
</head>
<body>
<h1>Hello World!</h1>
<p>This page has a light blue background color!</p>
</body> </html>
23. Example 2
<!DOCTYPE html>
<html> <head>
<style>
h1 { background-color: green; }
div { background-color: lightblue; }
p { background-color:yellow; }
</style>
</head>
<body>
<h1>CSS background-color example!</h1>
<div>
This is a text inside a div element.
<p>This paragraph has its own background color.</p>
We are still in the div element.
</div>
</body> </html>
24. 2. Background Image
The background-image property specifies an image to use as the
background of an element.
By default, the image is repeated so it covers the entire element.
<!DOCTYPE html>
<html>
<head>
<style>
body { background-image: url("paper.gif"); }
</style> </head>
<body>
<h1>HelloWorld!</h1>
<p>This page has an image as the background!</p>
</body> </html>
26. Background Image - Set position and
no-repeat
<!DOCTYPE html>
<html> <head> <style>
body {
background-image: url("img_tree.png");
background-position: right top;
}
</style> </head>
<body>
<h1>HelloWorld!</h1>
<p>W3Schools background image example.</p>
<p>The background image is only showing once, but it is disturbing the
reader!</p>
</body>
</html>
27. CSS Border
Specifies what kind of border to display.
dotted - Defines a dotted border
dashed - Defines a dashed border
solid - Defines a solid border
double - Defines a double 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
none - Defines no border
hidden - Defines a hidden border
The border-style property can have from one to four
values (for the top border, right border, bottom border, and
the left border)
30. CSS Margins
The margin property is a shorthand
property for the following individual
margin properties:
margin-top
margin-right
margin-bottom
margin-left
31. Example: (CSS Margins)
<!DOCTYPE html>
<html>
<head> <style>
div {
border: 1px solid black;
margin: 25px 50px 75px 100px;
background-color: lightblue;
}
</style> </head>
<body>
<div>
This div element has a top margin of 25px, a right margin of 50px, a bottom
margin of 75px, and a left margin of 100px.
</div>
</body> </html>
32. CSS padding
The CSS padding properties are used to
generate space around an element's
content, inside of any defined borders.
With CSS, you have full control over the
padding.
There are properties for setting the padding for
each side of an element (top, right, bottom, and
left).
33. Example: (CSS padding)
<!DOCTYPE html>
<html>
<head>
<style>
div {
border: 1px solid black;
background-color: lightblue;
padding-top: 50px;
padding-right: 30px;
padding-bottom: 50px;
padding-left: 80px;
}
</style>
</head>
<body>
<div>This div element has a top padding of 50px, a right padding of 30px, a bottom padding
of 50px, and a left padding of 80px.</div>
</body> </html>
34. CSS height and width
The height and width properties are used
to set the height and width of an element.
The height and width can be set to auto
(this is default.)
Means that the browser calculates the
height and width), or be specified in length
values, like px, cm, etc., or in percent (%)
of the containing block.
35. Example: (CSS height and width)
<!DOCTYPE html>
<html>
<head> <style>
p {
height: 200px;
width: 50%;
background-color: powderblue;
}
</style> </head>
<body>
<p>This div element has a height of 200px and a width of
50%:</p>
</body> </html>
36. CSS Box Model
All HTML elements can be considered as boxes. In CSS, the term
"box model" is used when talking about design and layout.
The CSS box model is essentially a box that wraps around every
HTML element. It consists of: margins, borders, padding, and the
actual content.The image below illustrates the box model:
37. CSS Text
Text Color
Set the color of the text.
The color is specified by:
◦ a color name - like "red"
◦ a HEX value - like "#ff0000"
◦ an RGB value - like "rgb(255,0,0)”
Text Alignment
Set the horizontal alignment of a text.
A text can be left or right aligned, centered, or justified.
38. Text Decoration
The text-decoration property is used to set or
remove decorations from text.
Values specified: overline, line-through,
underline, none
TextTransformation
The text-transform property is used to specify
uppercase and lowercase letters in a text.
Values specified: uppercase, lowercase,
capitalize
CSS Text
40. <body>
<h1>This is heading 1</h1>
<h2>sample text decoration tag</h2>
<p>This is an ordinary paragraph. Notice that
this text is blue.The default text color for a
page is defined in the body selector.</p>
</body>
</html>
Example: (CSS Text)
41. CSS Fonts
Font Family
The font family of a text is set with the font-
family property
Values specified:Times New Roman, Georgia,Arial,
Verdana etc
Font Style
Specify italic text.
This property has three values:
◦ normal - The text is shown normally
◦ italic - The text is shown in italics
◦ oblique - The text is "leaning" (oblique is very similar to
italic, but less supported)
42. Font Size
Sets the size of the text.
The font-size value can be an absolute, or relative size.
Absolute size:
◦ Sets the text to a specified size
◦ Does not allow a user to change the text size in all browsers (bad for
accessibility reasons)
◦ Absolute size is useful when the physical size of the output is known
Relative size:
◦ Sets the size relative to surrounding elements
◦ Allows a user to change the text size in browsers
CSS Fonts
43. Font Weight
Specifies the weight of a font
Values specified: normal, bold, lighter
CSS Fonts
46. <html>
<body>
<h1>CSS font-family</h1>
<h2> font size in “em” units</h2>
<p class="serif">This is a paragraph, shown in the Times New Roman font.</p>
<p class="italic">This is a paragraph in italic style.</p>
<p class="normal">This is a paragraph.</p>
</body>
</html>
Example:
47. CSS Links
<!DOCTYPE html>
<html> <head> <style>
a {
color: hotpink;
}
</style> </head>
<body>
<p><b><a href="http://www.google.com" target="_blank">This is a
link</a></b></p>
</body>
</html>
48. CSS Links
In addition, links can be styled differently
depending on what state they are in.
The four links states are:
◦ a:link - a normal, unvisited link
◦ a:visited - a link the user has visited
◦ a:hover - a link when the user moves over it
◦ a:active - a link the moment it is clicked
49. Example: (CSS Links)
<!DOCTYPE html>
<html> <head> <style>
/* unvisited link */
a:link {
color: red;
}
/* visited link */
a:visited {
color: green;
}
/* mouse over link */
a:hover {
color: hotpink;
}
50. Example: (CSS Links)
/* selected link */
a:active {
color: blue;
}
</style>
</head>
<body>
<p><b><a href=“http://www.google.com"
target="_blank">This is a link</a></b></p>
</body> </html>
51. CSS Lists
The CSS list properties allow you to:
◦ Set different list item markers for ordered lists
◦ Set different list item markers for unordered lists
◦ Set an image as the list item marker
◦ Add background colors to lists and list items
62. CSS Website Layout
Header
A header is usually located at the top of
the website (or right below a top
navigation menu). It often contains a logo
or the website name
Navigation Bar
A navigation bar contains a list of links to
help visitors navigating through your
website:
63. Content
The layout in this section, often depends on the target users.
The most common layout is one (or combining them) of the
following:
1-column (often used for mobile browsers)
2-column (often used for tablets and laptops)
3-column layout (only used for desktops)
64. CSS Website Layout
Footer
The footer is placed at the bottom of
your page. It often contains information
like copyright and contact info
66. CSS3 Borders
The CSS3 provides two new properties for
styling the borders of an element in a more
elegant way —
◦ border-image property for adding the images
to borders
◦ border-radius property for making the
rounded corners without using any images.
69. CSS3 Colors
CSS3 adds some new functional
notations for setting color values for the
elements which are :
◦ rgba()
◦ hsl()
◦ hsla()
70. RGBA ColorValues
Colors defined in the RGBA model (red-green-blue-alpha)
using the rgba() functional notation.
RGBA color model are an extension of RGB color model
with an alpha channel — which specifies the opacity of a
color.
The alpha parameter accepts a value from 0.0 (fully
transparent) to 1.0 (fully opaque).
Example
h1 {
color: rgba(0,0,255,0.5);
}
p {
background-color: rgba(0%,0%,100%,0.3);
}
72. HSL ColorValues
Colors also can be defined the HSL model (hue-saturation-lightness)
using the hsl() functional notation.
Hue is represented as an angle (from 0 to 360) of the color wheel or
circle
Saturation and lightness are represented as percentages.
100% saturation means full color, and 0% is a shade of gray.
100% lightness is white, 0% lightness is black, and 50% lightness is normal.
Example
h1 {
color: hsl(360,70%,60%);
}
p {
background-color: hsl(480,50%,80%);
}
74. HSLA ColorValues
Colors can be defined in the HSLA model (hue-saturation-
lightness-alpha) using the hsla()functional notation.
HSLA color model are an extension of HSL color model
with an alpha channel — which specifies the opacity of a
color.
The alpha parameter accepts a value from 0.0 (fully
transparent) to 1.0 (fully opaque).
Example
h1 {
color: hsla(360,80%,50%,0.5);
}
p {
background-color: hsla(480,60%,30%,0.3);
}
76. CSS3 Backgrounds
The CSS3 provides several new
properties to manipulate the background
of an element like
background clipping
multiple backgrounds
option to adjust the background size.
77. CSS3 background-size Property
background-size property can be used to
specify the size of the background images.
Prior to CSS3, the size of the background
images was determined by the actual size of
the images.
The background image size can be specified
using the pixels or percentage values as well
as the keywords contain, and cover.
Negative values are not allowed.
79. CSS3 background-clip Property
The background-clip property can be
used to specify whether an element's
background extends into the border or
not.
The background-clip property can take
the three values: border-box, padding-
box, content-box.
81. CSS3 Gradients
The CSS3 gradient feature provides a flexible
solution to generate smooth transitions
between two or more colors.
The elements with gradients can be scaled up
or down to any extent without losing the
quality, also the output will render much faster
because it is generated by the browser.
Gradients are available in two
styles: linear and radial.
82. Linear Gradient –Top to Bottom
/* Standard syntax */
background: linear-gradient(red, yellow);
}
83. Linear Gradient - Left to Right
/* Standard syntax */
background: linear-gradient(to right, red, yellow);
}
84. CSS3 Drop Shadows
Add drop shadow effects to the elements like you
do in Photoshop without using any images.
Syntax: box-shadow: offset-x offset-y blur-
radius color;
◦ offset-x — Sets the horizontal offset of the shadow.
◦ offset-y — Sets the vertical offset of the shadow.
◦ blur-radius — Sets the blur radius.The larger the
value, the bigger the blur and more the shadow's edge
will be blurred. Negative values are not allowed.
◦ color — Sets the color of the shadow. If the color
value is omitted or not specified, it takes the value of
the color property.
87. CSS3 2DTransforms
With CSS3 2D transform feature you can perform basic
transform manipulations such as move, rotate, scale
and skew on elements in a two-dimensional space.
A transformed element doesn't affect the surrounding
elements, but can overlap them, just like the absolutely
positioned elements.
CSS3 transform property uses the transform
functions to manipulate the coordinate system used by
an element in order to apply the transformation effect.
88. CSS3 2DTransforms:
The translate() Function
Moves the element from its current position to a new position along
the X andY axes.
Syntax: translate(tx, ty).
<!DOCTYPE html>
<head> <meta charset="UTF-8">
<title>Example of CSS3 translate() Method</title>
<style type="text/css">
img {
transform: translate(200px, 50px); /* Standard syntax */
}
.box{
margin: 50px;
width:153px;
height:103px;
background: url("/examples/images/tortoise-transparent.png") no-repeat;
}</style> </head>
90. CSS3 2DTransforms:
The rotate() Function
Rotates the element around its origin by the specified angle.
Syntax: rotate(a).
<!DOCTYPE html>
<head>
<title>Example of CSS3 rotate() Method</title>
<style type="text/css">
img {
transform: rotate(30deg); /* Modern Browsers */
}
.box{
margin: 50px;
width:120px;
height:110px;
background: url("/examples/images/star-fish-transparent.png") no-repeat;
}
</style>
</head>
93. CSS3 3DTransforms
With CSS3 3D transform feature you can perform basic
transform manipulations such as move, rotate, scale
and skew on elements in a three-dimensional space.
The CSS3 transform property uses the transform
functions to manipulate the coordinate system used by
an element in order to apply the transformation effect.
94. CSS3 3DTransforms:
The translate3d() Function
Moves the element from its current position to a new position
along the X,Y and Z-axis.
Syntax: translate(tx, ty, tz).
<!DOCTYPE html>
<head>
<title>Example of CSS3 translate3d() Method</title>
<style type="text/css">
.container{
width: 125px;
height: 125px;
perspective: 500px;
border: 4px solid #e5a043;
background: #fff2dd;
margin: 30px;
}
96. CSS3 3DTransforms:
The rotate3d() Function
The rotate3d() function rotates the element in 3D
space by the specified angle around the [x,y,z] direction
vector.
Syntax: rotate(vx, vy, vz, angle).
Example:
#myDiv {
transform: rotateX(150deg);
}
97. Example: 3D rotate method
<!DOCTYPE html>
<html>
<head>
<style>
div {
width: 300px;
height: 100px;
background-color: yellow;
border: 1px solid black;
}
#myDiv {
transform: rotateX(150deg); /* Standard
syntax */
}
</style> </head>
<body>
<h1>The rotateX() Method</h1>
<div>
This a normal div element.
</div>
<div id="myDiv">
This div element is rotated 150 degrees.
</div>
</body>
</html>
99. CSS3 Transitions
CSS transitions allows you to change property values
smoothly, over a given duration.
101. CSS Animations
CSS allows animation of HTML elements without using
JavaScript or Flash!
102. Explore
CSSTooltips(extra information about something when
the user moves the mouse pointer over an element)
CSS Object Fit(<img> or <video> should be resized
to fit its container)
CSS Buttons (styling of buttons)
CSS Pagination (move from one page to another
<1>…<2>…