Professional Documents
Culture Documents
displayed
Styles were added to HTML 5 to solve a
problem
CSS saves a lot of work
External Style Sheets are stored in CSS files
CSS1 Version
◦ Provides margin, border, padding and positioning
of elements.
◦ Provides different text attributes such as spacing
between words, letters and lines of text.
◦ Provides font properties such as emphasis.
◦ Provides alignment of text, image and other
elements.
◦ Provides color to text.
◦ Provides background to elements.
CSS2 Version
◦ Provides absolute, relative and fixed positioning of
elements and z-index.
◦ Provides styles for different media types such as
screen, TV and print.
◦ Supports aural style sheets that are used by visually
impaired persons to access the web.
◦ Provides font properties, such as shadows.
◦ Supports bidirectional text, which represents a text
that can be displayed in both right-to-left (RTL)
and left-to-right(LTR) directions.
CSS3 Version
◦ Supports more colors such as Red, Green, Blue, Alpha
(RGBA) and Hue Saturation Lightness (HSL)
◦ Supports border image through border-image and
border-corner-image properties.
◦ Provides rounded corners for any box using border-
radius and background position.
◦ Provides the box-shadow property to add shadow
effect to the elements.
◦ Allows multiple backgrounds on a web page.
◦ Allows multi-column text without using a table.
◦ Displays shadow with the text.
◦ Provides opacity to set the transparency of box,
images or text.
◦ Provides CSS selectors
◦ Supports RGBA colors
◦ Provides custom fonts
◦ Provides attribute selectors.
HSL takes three values:
Hue is a degree on the color wheel; 0 (or 360)
full colour.
Lightness is also a percentage; 0% is dark
<body>
<p>HEX colors:</p>
<p id="p1">Red</p>
<p id="p2">Green</p>
<p id="p3">Blue</p>
<p id="p4">Yellow</p>
<p id="p5">Cerise</p>
</body>
</html>
An RGB color value is specified with: rgb(red,
green, blue). Each parameter (red, green, and
blue) defines the intensity of the color and
can be an integer between 0 and 255 or a
percentage value (from 0% to 100%).
<!DOCTYPE html>
<html>
<head>
<style>
#p1 {background-color:rgb(255,0,0);}
#p2 {background-color:rgb(0,255,0);}
#p3 {background-color:rgb(0,0,255);}
#p4 {background-color:rgb(192,192,192);}
#p5 {background-color:rgb(255,255,0);}
#p6 {background-color:rgb(255,0,255);}
</style>
</head>
<body>
<p>RGB colors:</p>
<p id="p1">Red</p>
<p id="p2">Green</p>
<p id="p3">Blue</p>
<p id="p4">Grey</p>
<p id="p5">Yellow</p>
<p id="p6">Cerise</p>
</body>
</html>
RGBA color values are an extension of RGB
color values with an alpha channel - which
specifies the opacity of the object.
An RGBA color value is specified with:
<body>
<p>RGB colors with opacity:</p>
<p id="p1">Red</p>
<p id="p2">Green</p>
<p id="p3">Blue</p>
<p id="p4">Grey</p>
<p id="p5">Yellow</p>
<p id="p6">Cerise</p>
</body>
</html>
HSL stands for hue, saturation, and lightness -
and represents a cylindrical-coordinate
representation of colors.
An HSL color value is specified with: hsl(hue,
saturation, lightness).
Hue is a degree on the color wheel (from 0 to
<body>
<p>HSL colors:</p>
<p id="p1">Green</p>
<p id="p2">Light green</p>
<p id="p3">Dark green</p>
<p id="p4">Pastel green</p>
<p id="p5">Violet</p>
<p id="p6">Pastel violet</p>
</body>
</html>
HSLA color values are an extension of HSL
color values with an alpha channel - which
specifies the opacity of the object.
An HSLA color value is specified with:
<body>
<p>HSL colors with opacity:</p>
<p id="p1">Green</p>
<p id="p2">Light green</p>
<p id="p3">Dark green</p>
<p id="p4">Pastel green</p>
<p id="p5">Violet</p>
<p id="p6">Pastel violet</p>
</body>
</html>
External style sheet
Internal style sheet
Inline style
Each page must include a reference to the
external style sheet file inside the <link>
element
myStyle.css
body {
h1 {
color: navy;
margin-left: 20px;
}
<!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>
Internal styles are defined within the <style>
element, inside the <head> section of an
HTML page
<!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>
An inline style may be used to apply a unique
style for a single element.
To use inline styles, add the style attribute to
property.
<!DOCTYPE html>
<html>
<body style="background-
color:yellow;">
<h1 style="color:blue;margin-
left:30px;">This is a
heading.</h1>
<p>This is a paragraph.</p>
</body>
</html>
The CSS background properties are used to
define the background effects for elements.
CSS background properties:
background-color
background-image
background-repeat
background-attachment
background-position
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-color: lightblue;
}
</style>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
background-color: green;
}
div {
background-color: lightblue;
}
p{
background-color: yellow;
}
</style>
</head>
<body>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-image: url("paper.gif");
}
</style>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-image:
url("gradient_bg.png");
background-repeat: repeat-x;
}
</style>
</head>
<body>
<h1>Hello World!</h1>
<p>Here, a backgound image is
repeated only horizontally!</p>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-image: url("img_tree.png");
background-repeat: no-repeat;
}
</style>
</head>
<body>
<h1>Hello World!</h1>
<p>W3Schools background image
example.</p>
<p>The background image is only showing
once, but it is disturbing the reader!</p>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-image: url("img_tree.png");
background-repeat: no-repeat;
background-position: right top;
margin-right: 200px;
}
</style>
</head>
<body>
<h1>Hello World!</h1>
<p>W3Schools background no-repeat, set
position example.</p>
<p>Now the background image is only
shown once, and positioned away from the
text.</p>
<p>In this example we have also added a
margin on the right side, so the
background image will never disturb the
text.</p>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-image: url("img_tree.png");
background-repeat: no-repeat;
background-position: right top;
margin-right: 200px;
background-attachment: fixed;
}
</style>
</head>
<body>
<h1>Hello World!</h1>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>If you do not see any scrollbars, try to resize the browser window.</p>
</body>
</html>
CSS selectors allow you to select an element
to apply CSS style rules and manipulate HTML
elements.
CSS selectors are used to "find" (or select)
<h1>Welcome to My Homepage</h1>
<div class="intro">
<p>My name is Donald.</p>
<p>I live in Duckburg.</p>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
p.hometown {
background: yellow;
}
</style>
</head>
<body>
</body>
</html>
The id selector uses the id attribute of an
HTML element to select a specific element.
An id should be unique within a page, so the
</body>
</html>
h1 {
text-align: center;
color: red;
}
h2 {
text-align: center;
color: red;
}
p{
text-align: center;
color: red;
}
<!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>
A child selector matches when an element is
the child of some element. A child selector is
made up of two or more selectors separated
by ">“
he following rule sets the style of all P
<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>
<!DOCTYPE html>
<html>
<head>
<style>
body > ol > li {color: silver;}
</style>
</head>
<body>
<ol>
<li>The text here is silver.</li>
</ol>
<div>
<ul>
<li>Look, a list (and this text is not silver, by the way):
<ol>
<li>The text here is
<em>not</em> silver.</li>
</ol>
</li>
</ul>
</div>
</body>
</html>
Descendant : Element having child ,
grandchild elements
Descendant Selector : Style is applied to the
H2</h2> </div>
<!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>
The adjacent sibling selector selects all
elements that are the adjacent siblings of a
specified element.
Sibling elements must have the same parent
<div>
<p>Paragraph 1 in the div.</p>
<p>Paragraph 2 in the div.</p>
</div>
</body>
</html>
The general sibling selector selects all
elements that are siblings of a specified
element.
Example selects all <p> elements that are
<div>
<p>Paragraph 1 in the div.</p>
<p>Paragraph 2 in the div.</p>
</div>
</body>
</html>
The [attribute] selector is used to select
elements with a specified attribute.
Example selects all <a> elements with a
target attribute
<!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>
</body>
</html>
The [attribute=value] selector is used to
select elements with a specified attribute and
value.
Example selects all <a> elements with a
target="_blank" attribute
<!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>All images with the title attribute containing the word "flower" get a yellow
border.</p>
</body>
</html>
The [attribute|=value] selector is used to
select elements with the specified attribute
starting with the specified value.
The following example selects all elements
<h1 class="top-header">Welcome</h1>
<p class="top-text">Hello world!</p>
<p class="topcontent">Are you learning CSS?</p>
</body>
</html>
The [attribute^=value] selector is used to
select elements whose attribute value begins
with a specified value.
The following example selects all elements
<h1 class="top-header">Welcome</h1>
<p class="top-text">Hello world!</p>
<p class="topcontent">Are you learning CSS?</p>
</body>
</html>
The [attribute$=value] selector is used to
select elements whose attribute value ends
with a specified value.
The following example selects all elements
</body>
</html>
The [attribute*=value] selector is used to
select elements whose attribute value
contains a specified value.
The following example selects all elements
</body>
</html>
<!DOCTYPE HTML>
<head>
<title> Transition </title>
<style type="text/css">
.item {
color: #444;
margin: 0 20px;
height: 30px;
line-height: 30px;
position: absolute;
text-align: center;
width: 90px;
height: 200px;
opacity: 0.9;
}
.area .item {
-webkit-transition: all 1s linear;
}
.area .item-1 {
bottom: 20px;
-webkit-transition-duration: 0.5s;
}
.area .item-2 {
bottom: 140px;
-webkit-transition-duration: 1.5s;
}
.area .item-3 {
bottom: 260px;
-webkit-transition-duration: 2.5s;
}
.area .item-4 {
bottom: 380px;
-webkit-transition-duration: 3.5s;
}
.item:hover,
.item:focus {
color: white;
width: 400px;
opacity:0.6;
}
</style>
</head>
<body>
<div class="area">
<h1> Working with Transition</h1>
<div class="item item-1">
<img src="car1.jpg">
<div>
0.5 second
</div>
<br/>
</div>
<br/>
</body>
</html>
transform: none|transform-functions|initial|inherit;
Property Values
Value Description
none Defines that there should be no
transformation
matrix(n,n,n,n,n,n) Defines a 2D transformation,
using a matrix of six values
matrix3d Defines a 3D transformation,
(n,n,n,n,n,n,n,n,n,n,n,n,n,n,n,n) using a 4x4 matrix of 16 values
translate(x,y) Defines a 2D translation
translate3d(x,y,z) Defines a 3D translation
initial Sets this property to its default
value
inherit Inherits this property from its
parent element
Value Description
translateX(x) Defines a translation, using only
the value for the X-axis
translateY(y) Defines a translation, using only
the value for the Y-axis
scale(x,y) Defines a 2D scale transformation
scale3d(x,y,z) Defines a 3D scale transformation
scaleX(x) Defines a scale transformation by
giving a value for the X-axis
scaleY(y) Defines a scale transformation by
giving a value for the Y-axis
rotate(angle) Defines a 2D rotation, the angle is
specified in the parameter
Some CSS properties are animatable, meaning
that they can be used in animations and
transitions.
Animatable properties can change gradually
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
#myDIV {
margin: auto;
border: 1px solid black;
width: 200px;
height: 100px;
background-color: coral;
color: white;
-webkit-animation: mymove 5s infinite; /* Chrome, Safari, Opera
*/
animation: mymove 5s infinite;
}
/* Chrome, Safari, Opera */
@-webkit-keyframes mymove {
50% {-webkit-transform: rotate(180deg);}
}
/* Standard syntax */
@keyframes mymove {
50% {transform: rotate(180deg);}
}
</style>
</head>
<body>
</body>
</html>
<!DOCTYPE HTML>
<head>
<title> Rotating Image </title>
<style type="text/css">
img {
width: 300px;
height: 200px;
}
#transform {
position: relative;
margin: 10px auto;
width: 300px;
height: 200px;
-webkit-transform-style: preserve-3d;
-webkit-transition: all 1.0s linear;
}
#transform:hover {
-webkit-transform: rotateZ(60deg);
-webkit-box-shadow: -5px 5px 5px #aaa;
}
</style>
</head>
<body>
</body>
</html>
The attribute selectors can be useful for
styling forms without class or ID
<!DOCTYPE html>
<html>
<head>
<style>
input[type=text] {
width: 150px;
display: block;
margin-bottom: 10px;
background-color: yellow;
}
input[type=button] {
width: 120px;
margin-left: 35px;
display: block;
}
</style>
</head>
<body>
<form name="input" action="" method="get">
Firstname:<input type="text" name="Name" value="Peter" size="20">
Lastname:<input type="text" name="Name" value="Griffin" size="20">
<input type="button" value="Example Button">
</form>
</body>
</html>
External style sheet
Internal style sheet
Inline style
An external style sheet is ideal when the style
is applied to many pages. With an external
style sheet, you can change the look of an
entire Web site by changing just one file.
Each page must include a link to the style
sheet with the <link> tag. The <link> tag
goes inside the head section:
<head>
<link rel="stylesheet" type="text/css"
href="mystyle.css">
</head>
body {
background-color: lightblue;
}
h1 {
color: navy;
margin-left: 20px;
}
An internal style sheet should be used when a
single document has a unique style. You
define internal styles in the head section of
an HTML page, inside the <style> tag.
<!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>
To use inline styles, add the style attribute to
the relevant tag. The style attribute can
contain any CSS property. The example shows
how to change the color and the left margin
of a h1 element:
<h1 style="color:blue;margin-
left:30px;">This is a heading.</h1>
<!DOCTYPE html>
<html>
<body>
<h1 style="color:blue;margin-
left:30px;">This is a heading.</h1>
<p>This is a paragraph.</p>
</body>
</html>
If some properties have been set for the same
selector in different style sheets, the values will
be inherited from the more specific style sheet.
Example assume that an external style sheet
h1 {
color: navy;
margin-left: 20px;
}
internal style sheet
h1 {
color: orange;
}
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css">
<style>
h1 {
color: orange;
}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>The style of this document is a combination of an external stylesheet, and
internal style</p>
</body>
</html>
CSS background properties are used to define the
background effects of an element.
CSS properties used for background effects:
background-color
background-image
background-repeat
background-attachment
background-position
Background Color
The background-color property specifies the
background color of an element.
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-color: #b0c4de;
}
</style>
</head>
<body>
</body>
</html>
With CSS, a color is most often specified by:
a HEX value - like "#ff0000"
an RGB value - like "rgb(255,0,0)"
a color name - like "red"
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
background-color: #6495ed;
}
p{
background-color: #e0ffff;
}
div {
background-color: #b0c4de;
}
</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>
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-image: url("paper.gif");
}
</style>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-image: url("gradient_bg.png");
}
</style>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-image: url("gradient_bg.png");
background-repeat: repeat-x;
}
</style>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>