CSS1
CSS1
CSS Introduction
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
External stylesheets are stored in CSS files
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
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
websites, where fonts and color information were added to every single page,
became a long and expensive process.
To solve this problem, the World Wide Web Consortium (W3C) created CSS.
With an external stylesheet file, you can change the look of an entire website by
changing just one file!
CSS Syntax
A CSS rule-set consists of a selector and a declaration block:
A CSS declaration always ends with a semicolon, and declaration blocks are
surrounded by curly braces.
In the following example all <p> elements will be center-aligned, with a red
text color:
<!DOCTYPE html>
<html>
<head>
<style>
p{
color: red;
text-align: center;
</style>
</head>
<body>
<p>Hello World!</p>
</body>
</html>
Hello World!
You can select all <p> elements on a page like this (in this case, all <p>
elements will be center-aligned, with a red text color):
<!DOCTYPE html>
<html>
<head>
<style>
p{
text-align: center;
color: red;
</style>
</head>
<body>
<p>And me!</p>
</body>
</html>
Me too!
And me!
The 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.
The style rule below will be applied to the HTML element with id="para1":
<!DOCTYPE html>
<html>
<head>
<style>
#para1 {
text-align: center;
color: red;
</style>
</head>
<body>
<p id="para1">Hello World!</p>
</body>
</html>
Hello World!
To select elements with a specific class, write a period (.) character, followed by
the name of the class.
In the example below, all HTML elements with class="center" will be red and
center-aligned:
<!DOCTYPE html>
<html>
<head>
<style>
.center {
text-align: center;
color: red;
</style>
</head>
<body>
<h1 class="center">Red and center-aligned heading</h1>
</body>
</html>
Grouping Selectors
<!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>
Hello World!
Smaller heading!
This is a paragraph.
CSS How To...
External Style Sheet
With an external style sheet, you can change the look of an entire website by
changing just one file!
Each page must include a reference to the external style sheet file inside the
<link> element. The <link> element goes inside the <head> section:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
This is a heading
This is a paragraph.
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>
This is a heading
This is a paragraph.
CSS Colors
Color Names
In HTML, a color can be specified by using a color name:
<!DOCTYPE html>
<html>
<body>
<h1 style="background-color:Tomato;">Tomato</h1>
<h1 style="background-color:Orange;">Orange</h1>
<h1 style="background-color:DodgerBlue;">DodgerBlue</h1>
<h1 style="background-color:MediumSeaGreen;">MediumSeaGreen</h1>
<h1 style="background-color:Gray;">Gray</h1>
<h1 style="background-color:SlateBlue;">SlateBlue</h1>
<h1 style="background-color:Violet;">Violet</h1>
<h1 style="background-color:LightGray;">LightGray</h1>
</body>
</html>
Tomato
Orange
DodgerBlue
MediumSeaGreen
Gray
SlateBlue
Violet
LightGray
Background Color
You can set the background color for HTML elements:
<!DOCTYPE html>
<html>
<body>
<p style="background-color:Tomato;">
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut
laoreet dolore magna aliquam erat volutpat.
Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex
ea commodo consequat.
</p>
</body>
</html>
Hello World
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh
euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad
minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip
ex ea commodo consequat.
Text Color
You can set the color of text:
Hello World
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy
nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.
Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit
lobortis nisl ut aliquip ex ea commodo consequat.
<!DOCTYPE html>
<html>
<body>
<p style="color:DodgerBlue;">Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam
nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.</p>
<p style="color:MediumSeaGreen;">Ut wisi enim ad minim veniam, quis nostrud exerci tation
ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.</p>
</body>
</html>
Hello World
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh
euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.
Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit
lobortis nisl ut aliquip ex ea commodo consequat.
Border Color
You can set the color of borders:
<!DOCTYPE html>
<html>
<body>
</body>
</html>
Hello World
Hello World
Hello World
Color Values
In HTML, colors can also be specified using RGB values, HEX values, HSL
values, RGBA values, and HSLA values:
<!DOCTYPE html>
<html>
<body>
<h1 style="background-color:#ff6347;">#ff6347</h1>
<p>In addition to the predefined color names, colors can be specified using RGB, HEX, HSL, or even
transparent colors using RGBA or HSLA color values.</p>
</body>
</html>
RGB Value
In HTML, 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 the color black, all color parameters must be set to 0, like this: rgb(0, 0, 0).
To display the color white, all color parameters must be set to 255, like this: rgb(255, 255, 255).
<html>
<body>
</body>
</html>
rgb(255, 0, 0)
rgb(0, 0, 255)
rgb(60, 179, 113)
rgb(238, 130, 238)
rgb(255, 165, 0)
rgb(106, 90, 205)
In HTML, you can specify colors using RGB values.
Shades of gray are often defined using equal values for all the 3 light sources:
<!DOCTYPE html>
<html>
<body>
<p>By using equal values for red, green, and blue, you will get different shades of
gray.</p>
</body>
</html>
rgb(0, 0, 0)
rgb(60, 60, 60)
rgb(120, 120, 120)
rgb(180, 180, 180)
rgb(240, 240, 240)
rgb(255, 255, 255)
By using equal values for red, green, and blue, you will get different shades of gray.
HEX Value
In HTML, a color can be specified using a hexadecimal value in the form:
#rrggbb
Where rr (red), gg (green) and bb (blue) are hexadecimal values between 00
and ff (same as decimal 0-255).
For example, #ff0000 is displayed as red, because red is set to its highest value
(ff) and the others are set to the lowest value (00).
<!DOCTYPE html>
<html>
<body>
<h1 style="background-color:#ff0000;">#ff0000</h1>
<h1 style="background-color:#0000ff;">#0000ff</h1>
<h1 style="background-color:#3cb371;">#3cb371</h1>
<h1 style="background-color:#ee82ee;">#ee82ee</h1>
<h1 style="background-color:#ffa500;">#ffa500</h1>
<h1 style="background-color:#6a5acd;">#6a5acd</h1>
<p>In HTML, you can specify colors using Hex values.</p>
</body>
</html>
#ff0000
#0000ff
#3cb371
#ee82ee
#ffa500
#6a5acd
In HTML, you can specify colors using Hex values.
HSL Value
In HTML, 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.
<!DOCTYPE html>
<html>
<body>
</body>
</html>
Saturation
Saturation can be describe as the intensity of a color.
50% is 50% gray, but you can still see the color.
<!DOCTYPE html>
<html>
<body>
<p>With HSL colors, less saturation mean less color. 0% is completely gray.</p>
</body>
</html>
hsl(0, 100%, 50%)
hsl(0, 80%, 50%)
hsl(0, 60%, 50%)
hsl(0, 40%, 50%)
hsl(0, 20%, 50%)
hsl(0, 0%, 50%)
With HSL colors, less saturation mean less color. 0% is completely gray.
Lightness
The lightness off a color can be described as how much light you want to give
the color, where 0% means no light (black), 50% means 50% light (neither
dark nor light) 100% means full lightness (white).
<!DOCTYPE html>
<html>
<body>
</body>
</html>
RGBA Value
RGBA color values are an extension of RGB color values with an alpha channel -
which specifies the opacity for a color.
rgba(red, green, blue, alpha)
The alpha parameter is a number between 0.0 (fully transparent) and 1.0 (not
transparent at all):
<!DOCTYPE html>
<html>
<body>
<h1 style="background-color:rgba(255, 99, 71, 0);">rgba(255, 99, 71, 0)</h1>
<p>You can make transparent colors by using the RGBA color value.</p>
</body>
</html>
CSS Backgrounds
Background Color
The background-color property specifies the background color of an element.
Example
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-color: lightblue;
</style>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
Hello World!
This page has a light blue background color!
With CSS, a color is most often specified by:
In the example below, the <h1>, <p>, and <div> elements have different
background colors:
<!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>
</div>
</body>
</html>
Background Image
The background-image property specifies an image to use as the background of
an element.
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-image: url("paper.gif");
</style>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
Hello World!
This page has an image as the background!
Some images should be repeated only horizontally or vertically, or they will look
strange, like this:
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-image: url("gradient_bg.png");
</style>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
Hello World!
Strange background image...
<!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>
Hello World!
Here, a background image is repeated only horizontally!
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-image: url("img_tree.png");
background-repeat: no-repeat;
</style>
</head>
<body>
<h1>Hello World!</h1>
<p>The background image is only showing once, but it is disturbing the reader!</p>
</body>
</html>
Hello World!
W3Schools background image example.
The background image is only showing once, but it is disturbing the reader!
In the example above, the background image is shown in the same place as the
text. We want to change the position of the image, so that it does not disturb
the text too much.
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-image: url("img_tree.png");
background-repeat: no-repeat;
margin-right: 200px;
}
</style>
</head>
<body>
<h1>Hello World!</h1>
<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>
Hello World!
W3Schools background no-repeat, set position example.
Now the background image is only shown once, and positioned away from the text.
In this example we have also added a margin on the right side, so the background
image will never disturb the text.
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-image: url("img_tree.png");
background-repeat: no-repeat;
margin-right: 200px;
background-attachment: fixed;
</style>
</head>
<body>
<h1>Hello World!</h1>
<p>If you do not see any scrollbars, try to resize the browser window.</p>
</body>
</html>
Hello World!
The background-image is fixed. Try to scroll down the page.
If you do not see any scrollbars, try to resize the browser window.
CSS Borders
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).
<!DOCTYPE html>
<html>
<head>
<style>
</style>
</head>
<body>
<h2>The border-style Property</h2>
</body>
</html>
A dotted border.
A dashed border.
A solid border.
A double border.
A groove border.
A ridge border.
An inset border.
An outset border.
No border.
A hidden border.
A mixed border.
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, cm, em, etc) or by using one
of the three pre-defined values: thin, medium, or thick.
The border-width property can have from one to four values (for the top
border, right border, bottom border, and the left border).
5px border-width
<!DOCTYPE html>
<html>
<head>
<style>
p.one {
border-style: solid;
border-width: 5px;
}
p.two {
border-style: solid;
border-width: medium;
p.three {
border-style: dotted;
border-width: 2px;
p.four {
border-style: dotted;
border-width: thick;
p.five {
border-style: double;
border-width: 15px;
p.six {
border-style: double;
border-width: thick;
}
p.seven {
border-style: solid;
</style>
</head>
<body>
</body>
</html>
The border-width Property
Some text.
Some text.
Some text.
Some text.
Some text.
Some text.
Some text.
Note: The "border-width" property does not work if it is used alone. Always specify
the "border-style" property to set the borders first.
Border Color
The border-color property is used to set the color of the four borders.
The border-color property can have from one to four values (for the top
border, right border, bottom border, and the left border).
<!DOCTYPE html>
<html>
<head>
<style>
p.one {
border-style: solid;
border-color: red;
p.two {
border-style: solid;
border-color: green;
p.three {
border-style: solid;
</style>
</head>
<body>
<p><b>Note:</b> The "border-color" property does not work if it is used alone. Use the "border-style"
property to set the borders first.</p>
</body>
</html>
Note: The "border-color" property does not work if it is used alone. Use the "border-
style" property to set the borders first.
In CSS, there are also properties for specifying each of the borders (top, right,
bottom, and left):
<!DOCTYPE html>
<html>
<head>
<style>
p{
border-top-style: dotted;
border-right-style: solid;
border-bottom-style: dotted;
border-left-style: solid;
</style>
</head>
<body>
</body>
</html>
To shorten the code, it is also possible to specify all the individual border
properties in one property.
border-width
border-style (required)
border-color
<!DOCTYPE html>
<html>
<head>
<style>
p{
</style>
</head>
<body>
</body>
</html>
You can also specify all the individual border properties for just one side:
Left Border
<!DOCTYPE html>
<html>
<head>
<style>
p{
border-left: 6px solid red;
background-color: lightgrey;
</style>
</head>
<body>
</body>
</html>
The border-left Property
Bottom Border
<!DOCTYPE html>
<html>
<head>
<style>
p{
background-color: lightgrey;
</style>
</head>
<body>
</body>
</html>
<html>
<head>
<style>
p.normal {
p.round1 {
border-radius: 5px;
p.round2 {
border-radius: 8px;
p.round3 {
border-radius: 12px;
}
</style>
</head>
<body>
<p><b>Note:</b> The "border-radius" property is not supported in IE8 and earlier versions.</p>
</body>
</html>
Normal border
Round border
Rounder border
Roundest border
<html>
<head>
<style>
div {
margin: 70px;
</style>
</head>
<body>
</body>
</html>
CSS Margins
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. There are properties for
setting the margin for each side of an element (top, right, bottom, and left).
Margin - Individual Sides
CSS has properties for specifying the margin for each side of an element:
margin-top
margin-right
margin-bottom
margin-left
The following example sets different margins for all four sides of a <p>
element:
<!DOCTYPE html>
<html>
<head>
<style>
div {
margin-top: 100px;
margin-bottom: 100px;
margin-right: 150px;
margin-left: 80px;
background-color: lightblue;
}
</style>
</head>
<body>
<div>This div element has a top margin of 100px, a right margin of 150px, a bottom margin of 100px,
and a left margin of 80px.</div>
</body>
</html>
margin-top
margin-right
margin-bottom
margin-left
<!DOCTYPE html>
<html>
<head>
<style>
div {
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>
<hr>
</body>
</html>
The element will then take up the specified width, and the remaining space will
be split equally between the left and right margins:
<!DOCTYPE html>
<html>
<head>
<style>
div {
width:300px;
margin: auto;
</style>
</head>
<body>
<h2>Use of margin:auto</h2>
<p>You can set the margin property to auto to horizontally center the element within its container. The
element will then take up the specified width, and the remaining space will be split equally between the
left and right margins:</p>
<div>
</body>
</html>
Use of margin:auto
You can set the margin property to auto to horizontally center the element within its
container. The element will then take up the specified width, and the remaining space
will be split equally between the left and right margins:
<!DOCTYPE html>
<html>
<head>
<style>
div {
margin-left: 100px;
p.ex1 {
margin-left: inherit;
</style>
</head>
<body>
<div>
<p class="ex1">This paragraph has an inherited left margin (from the div element).</p>
</div>
</body>
</html>
This paragraph has an inherited left margin (from the div element).
CSS Padding
<!DOCTYPE html>
<html>
<head>
<style>
div {
padding: 70px;
}
</style>
</head>
<body>
</body>
</html>
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).
padding-top
padding-right
padding-bottom
padding-left
The following example sets different padding for all four sides of a <div>
element:
<!DOCTYPE html>
<html>
<head>
<style>
div {
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>
Using individual padding properties
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.
padding-top
padding-right
padding-bottom
padding-left
<!DOCTYPE html>
<html>
<head>
<style>
div {
background-color: lightblue;
}
</style>
</head>
<body>
<div>This div element has a top padding of 25px, a right padding of 50px, a bottom padding of 75px, and
a left padding of 100px.</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
div {
background-color: lightblue;
}
</style>
</head>
<body>
<div>This div element has a top padding of 25px, a right and left padding of 50px, and a bottom padding
of 75px.</div>
</body>
</html>
So, if an element has a specified width, the padding added to that element will
be added to the total width of the element. This is often an undesirable result.
<!DOCTYPE html>
<html>
<head>
<style>
div.ex1 {
width: 300px;
background-color: yellow;
div.ex2 {
width: 300px;
padding: 25px;
background-color: lightblue;
</style>
</head>
<body>
<br>
<div class="ex2">The width of this div is 350px, even though it is defined as 300px in the CSS.</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
div {
height: 200px;
width: 50%;
background-color: powderblue;
</style>
</head>
<body>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
div {
height: 100px;
width: 500px;
background-color: powderblue;
</style>
</head>
<body>
<div></div>
</body>
</html>
Setting max-width
The max-width property is used to set the maximum width of an element.
The problem with the <div> above occurs when the browser window is smaller
than the width of the element (500px). The browser then adds a horizontal
scrollbar to the page.
Tip: Drag the browser window to smaller than 500px wide, to see the difference
between the two divs!
<!DOCTYPE html>
<html>
<head>
<style>
div {
max-width: 500px;
height: 100px;
background-color: powderblue;
}
</style>
</head>
<body>
<div></div>
</body>
</html>
CSS Box Model
The 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:
The box model allows us to add a border around elements, and to define space
between elements.
<!DOCTYPE html>
<html>
<head>
<style>
div {
background-color: lightgrey;
width: 300px;
padding: 25px;
margin: 25px;
</style>
</head>
<body>
<p>The CSS box model is essentially a box that wraps around every HTML element. It consists of:
borders, padding, margins, and the actual content.</p>
<div>This text is the actual content of the box. We have added a 25px padding, 25px margin and a 25px
green border. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu
fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt
mollit anim id est laborum.</div>
</body>
</html>
The CSS box model is essentially a box that wraps around every HTML element. It
consists of: borders, padding, margins, and the actual content.
This text is the actual content of the box. We have added a 25px padding, 25px
margin and a 25px green border. Ut enim ad minim veniam, quis nostrud
exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure
dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt
mollit anim id est laborum.
Important: When you set the width and height properties of an element with
CSS, you just set the width and height of the content area. To calculate the
full size of an element, you must also add padding, borders and margins.
<!DOCTYPE html>
<html>
<head>
<style>
div {
width: 320px;
padding: 10px;
margin: 0;
</style>
</head>
<body>
<div>The picture above is 350px wide. The total width of this element is also 350px.</div>
</body>
</html>
CSS Outline
CSS has the following outline properties:
outline-style
outline-color
outline-width
outline-offset
outline
Outline Style
The outline-style property specifies the style of the outline, and can have one
of the following values:
Example
<!DOCTYPE html>
<html>
<head>
<style>
p {outline-color:red;}
</style>
</head>
<body>
<p class="groove">A groove outline. The effect depends on the outline-color value.</p>
<p class="ridge">A ridge outline. The effect depends on the outline-color value.</p>
<p class="inset">An inset outline. The effect depends on the outline-color value.</p>
<p class="outset">An outset outline. The effect depends on the outline-color value.</p>
</body>
</html>
A dotted outline
A dashed outline
A solid outline
A double outline
Outline Color
The outline-color property is used to set the color of the outline.
<!DOCTYPE html>
<html>
<head>
<style>
p.ex1 {
border: 1px solid black;
outline-style: solid;
outline-color: red;
p.ex2 {
outline-style: double;
outline-color: green;
p.ex3 {
outline-style: outset;
outline-color: yellow;
</style>
</head>
<body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
p.ex1 {
outline-style: solid;
outline-color: invert;
</style>
</head>
<body>
<h2>Using outline-color:invert</h2>
</body>
</html>
Using outline-color:invert
Outline Width
The outline-width property specifies the width of the outline, and can have
one of the following values:
<!DOCTYPE html>
<html>
<head>
<style>
p.ex1 {
outline-style: solid;
outline-color: red;
outline-width: thin;
p.ex2 {
outline-style: solid;
outline-color: red;
outline-width: medium;
}
p.ex3 {
outline-style: solid;
outline-color: red;
outline-width: thick;
p.ex4 {
outline-style: solid;
outline-color: red;
outline-width: 4px;
}</style>
</head>
<body>
</html>
A thin outline.
A medium outline.
A thick outline.
Outline Offset
The outline-offset property adds space between an outline and the
edge/border of an element. The space between an element and its outline is
transparent.
<!DOCTYPE html>
<html>
<head>
<style>
p{
margin: 30px;
outline-offset: 15px;
</style>
</head>
<body>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
p{
margin: 30px;
background:yellow;
outline-offset: 15px;
</style>
</head>
<body>
<h2>The outline-offset Property</h2>
</body>
</html>
CSS Text
<!DOCTYPE html>
<html>
<head>
<style>
div {
margin: auto;
padding: 8px;
h1 {
text-align: center;
text-transform: uppercase;
color: #4CAF50;
}
p{
text-indent: 50px;
text-align: justify;
letter-spacing: 3px;
a{
text-decoration: none;
color: #008CBA;
</style>
</head>
<body>
<div>
<h1>text formatting</h1>
<p>This text is styled with some of the text formatting properties. The heading uses the text-align, text-
transform, and color properties.
The paragraph is indented, aligned, and the space between characters is specified. The underline is
removed from this colored
</div>
</body>
</html>
TEXT FORMATTING
This text is styled with some of the text formatting
properties. The heading uses the text-align, text-transform,
and color properties. The paragraph is indented, aligned, and
the space between characters is specified. The underline is
removed from this colored "Try it Yourself" link.
Text Color
The color property is used to set the color of the text. The color is specified by:
The default text color for a page is defined in the body selector.
<!DOCTYPE html>
<html>
<head>
<style>
body {
color: blue;
h1 {
color: green;
</style>
</head>
<body>
</body>
</html>
This is heading 1
This is an ordinary paragraph. Notice that this text is blue. The default text color for a
page is defined in the body selector.
Text Alignment
The text-align property is used to set the horizontal alignment of a text.
The following example shows center aligned, and left and right aligned text (left
alignment is default if text direction is left-to-right, and right alignment is
default if text direction is right-to-left):
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
text-align: center;
h2 {
text-align: left;
}
h3 {
text-align: right;
</style>
</head>
<body>
<h1>Heading 1 (center)</h1>
<h2>Heading 2 (left)</h2>
<h3>Heading 3 (right)</h3>
<p>The three headings above are aligned center, left and right.</p>
</body>
</html>
Heading 1 (center)
Heading 2 (left)
Heading 3 (right)
The three headings above are aligned center, left and right.
Text Decoration
The text-decoration property is used to set or remove decorations from text.
<html>
<head>
<style>
a{
text-decoration: none;
</style>
</head>
<body>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
text-decoration: overline;
}
h2 {
text-decoration: line-through;
h3 {
text-decoration: underline;
</style>
</head>
<body>
</body>
</html>
This is heading 1
This is heading 2
This is heading 3
Text Transformation
The text-transform property is used to specify uppercase and lowercase
letters in a text.
<html>
<head>
<style>
p.uppercase {
text-transform: uppercase;
p.lowercase {
text-transform: lowercase;
p.capitalize {
text-transform: capitalize;
</style>
</head>
<body>
</body>
</html>
THIS IS SOME TEXT.
Text Indentation
The text-indent property is used to specify the indentation of the first line of a
text:
<!DOCTYPE html>
<html>
<head>
<style>
p{
text-indent: 50px;
</style>
</head>
<body>
<p>In my younger and more vulnerable years my father gave me some advice that I've been turning
over in my mind ever since. 'Whenever you feel like criticizing anyone,' he told me, 'just remember that
all the people in this world haven't had the advantages that you've had.'</p>
</body>
</html>
In my younger and more vulnerable years my father gave me some advice that
I've been turning over in my mind ever since. 'Whenever you feel like criticizing
anyone,' he told me, 'just remember that all the people in this world haven't had the
advantages that you've had.'
Letter Spacing
The letter-spacing property is used to specify the space between the
characters in a text.
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
letter-spacing: 3px;
h2 {
letter-spacing: -3px;
</style>
</head>
<body>
</html>
This is heading 1
Thisisheading2
Line Height
The line-height property is used to specify the space between lines:
<!DOCTYPE html>
<html>
<head>
<style>
p.small {
line-height: 0.7;
p.big {
line-height: 1.8;
</style>
</head>
<body>
<p>
</p>
<p class="small">
</p>
<p class="big">
</p>
</body>
</html>
Text Direction
The direction property is used to change the text direction of an element:
<!DOCTYPE html>
<html>
<head>
<style>
p.ex1 {
direction: rtl;
</style>
</head>
<body>
</body>
</html>
Word Spacing
The word-spacing property is used to specify the space between the words in a
text.
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
word-spacing: 10px;
h2 {
word-spacing: -5px;
</style>
</head>
<body>
</body>
</html>
This is heading 1
This is heading 2
Text Shadow
The text-shadow property adds shadow to text.
The following example specifies the position of the horizontal shadow (3px), the
position of the vertical shadow (2px) and the color of the shadow (red):
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
</style>
</head>
<body>
<h1>Text-shadow effect</h1>
<p><b>Note:</b> Internet Explorer 9 and earlier do not support the text-shadow property.</p>
</body>
</html>
Text-shadow effect
Note: Internet Explorer 9 and earlier do not support the text-shadow property.
CSS Fonts
CSS Font Families
In CSS, there are two types of font family names:
generic family - a group of font families with a similar look (like "Serif"
or "Monospace")
font family - a specific font family (like "Times New Roman" or "Arial")
Generic family Font family Description
Serif Times New Roman Serif fonts have small lines at the
characters
Georgia
Lucida Console
Font Family
The font family of a text is set with the font-family property.
Start with the font you want, and end with a generic family, to let the browser
pick a similar font in the generic family, if no other fonts are available.
Note: If the name of a font family is more than one word, it must be in
quotation marks, like: "Times New Roman".
<!DOCTYPE html>
<html>
<head>
<style>
p.serif {
p.sansserif {
</style>
</head>
<body>
<h1>CSS font-family</h1>
</body>
</html>
CSS font-family
This is a paragraph, shown in the Times New Roman font.
Font Style
The font-style property is mostly used to specify italic text.
This property has three values:
<!DOCTYPE html>
<html>
<head>
<style>
p.normal {
font-style: normal;
p.italic {
font-style: italic;
p.oblique {
font-style: oblique;
</style>
</head>
<body>
</html>
Font Size
The font-size property sets the size of the text.
Being able to manage the text size is important in web design. However, you
should not use font size adjustments to make paragraphs look like headings, or
headings look like paragraphs.
Always use the proper HTML tags, like <h1> - <h6> for headings and <p> for
paragraphs.
Absolute size:
Relative size:
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
font-size: 40px;
h2 {
font-size: 30px;
p{
font-size: 14px;
</style>
</head>
<body>
<p>This is a paragraph.</p>
</body>
</html>
This is heading 1
This is heading 2
This is a paragraph.
1em is equal to the current font size. The default text size in browsers is 16px.
So, the default size of 1em is 16px.
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
h2 {
}
p{
</style>
</head>
<body>
<p>This is a paragraph.</p>
<p>Specifying the font-size in em allows all major browsers to resize the text.
Unfortunately, there is still a problem with older versions of IE. When resizing the text, it becomes
larger/smaller than it should.</p>
</body>
</html>
This is heading 1
This is heading 2
This is a paragraph.
Specifying the font-size in em allows all major browsers to resize the text. Unfortunately, there is still a
problem with older versions of IE. When resizing the text, it becomes larger/smaller than it should.
<html>
<head>
<style>
body {
font-size: 100%;
h1 {
font-size: 2.5em;
h2 {
font-size: 1.875em;
p{
font-size: 0.875em;
</style>
</head>
<body>
<p>Specifying the font-size in percent and em displays the same size in all major browsers, and allows all
browsers to resize the text!</p>
</body>
</html>
This is heading 1
This is heading 2
This is a paragraph.
Specifying the font-size in percent and em displays the same size in all major browsers, and allows all
browsers to resize the text!
Font Weight
The font-weight property specifies the weight of a font:
<!DOCTYPE html>
<html>
<head>
<style>
p.normal {
font-weight: normal;
p.light {
font-weight: lighter;
}
p.thick {
font-weight: bold;
p.thicker {
font-weight: 900;
</style>
</head>
<body>
</body>
</html>
This is a paragraph.
This is a paragraph.
This is a paragraph.
This is a paragraph.
Font Variant
The font-variant property specifies whether or not a text should be displayed
in a small-caps font.
<!DOCTYPE html>
<html>
<head>
<style>
p.normal {
font-variant: normal;
p.small {
font-variant: small-caps;
</style>
</head>
<body>
</body>
</html>
My name is Hege Refsnes.
CSS Icons
How To Add Icons
The simplest way to add an icon to your HTML page, is with an icon library, such
as Font Awesome.
Add the name of the specified icon class to any inline HTML element
(like <i> or <span>).
All the icons in the icon libraries below, are scalable vectors that can be
customized with CSS (size, color, shadow, etc.)
<link rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/font-
awesome/4.7.0/css/font-awesome.min.css">
<!DOCTYPE html>
<html>`
<head>
</head>
<body>
</body>
</html>
Bootstrap Icons
To use the Bootstrap glyphicons, add the following line inside
the <head> section of your HTML page:
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.
min.css">
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap Icons</title>
</head>
<body class="container">
<br><br>
</body>
</html>
Styling Links
<!DOCTYPE html>
<html>
<head>
<style>
/* unvisited link */
a:link {
color: red;
/* visited link */
a:visited {
color: green;
a:hover {
color: hotpink;
/* selected link */
a:active {
color: blue;
</style>
</head>
<body>
<p><b>Note:</b> a:hover MUST come after a:link and a:visited in the CSS definition in order to be
effective.</p>
<p><b>Note:</b> a:active MUST come after a:hover in the CSS definition in order to be effective.</p>
</body>
</html>
This is a link
Note: a:hover MUST come after a:link and a:visited in the CSS definition in order to
be effective.
Text Decoration
The text-decoration property is mostly used to remove underlines from links:
<!DOCTYPE html>
<html>
<head>
<style>
a:link {
text-decoration: none;
a:visited {
text-decoration: none;
a:hover {
text-decoration: underline;
}
a:active {
text-decoration: underline;
</style>
</head>
<body>
<p><b>Note:</b> a:hover MUST come after a:link and a:visited in the CSS definition in order to be
effective.</p>
<p><b>Note:</b> a:active MUST come after a:hover in the CSS definition in order to be effective.</p>
</body>
</html>
This is a link
Note: a:hover MUST come after a:link and a:visited in the CSS definition in order to
be effective.
Background Color
The background-color property can be used to specify a background color for
links:
<!DOCTYPE html>
<html>
<head>
<style>
a:link {
background-color: yellow;
a:visited {
background-color: cyan;
a:hover {
background-color: lightgreen;
a:active {
background-color: hotpink;
</style>
</head>
<body>
<p><b>Note:</b> a:hover MUST come after a:link and a:visited in the CSS definition in order to be
effective.</p>
<p><b>Note:</b> a:active MUST come after a:hover in the CSS definition in order to be effective.</p>
</body>
</html>
This is a link
Note: a:hover MUST come after a:link and a:visited in the CSS definition in order to
be effective.
<!DOCTYPE html>
<html>
<head>
<style>
a:link, a:visited {
background-color: #f44336;
color: white;
text-align: center;
text-decoration: none;
display: inline-block;
a:hover, a:active {
background-color: red;
</style>
</head>
<body>
</body>
</html>
This is a link
CSS Lists
Different List Item Markers
The list-style-type property specifies the type of list item marker.
The following example shows some of the available list item markers:
<!DOCTYPE html>
<html>
<head>
<style>
ul.a {
list-style-type: circle;
}
ul.b {
list-style-type: square;
ol.c {
list-style-type: upper-roman;
ol.d {
list-style-type: lower-alpha;
</style>
</head>
<body>
<ul class="a">
<li>Coffee</li>
<li>Tea</li>
<li>Coca Cola</li>
</ul>
<ul class="b">
<li>Coffee</li>
<li>Tea</li>
<li>Coca Cola</li>
</ul>
<ol class="c">
<li>Coffee</li>
<li>Tea</li>
<li>Coca Cola</li>
</ol>
<ol class="d">
<li>Coffee</li>
<li>Tea</li>
<li>Coca Cola</li>
</ol>
</body>
</html>
o Coffee
o Tea
o Coca Cola
Coffee
Tea
Coca Cola
a. Coffee
b. Tea
c. Coca Cola
<!DOCTYPE html>
<html>
<head>
<style>
ul {
list-style-image: url('sqpurple.gif');
</style>
</head>
<body>
<ul>
<li>Coffee</li>
<li>Tea</li>
<li>Coca Cola</li>
</ul>
</body>
</html>
Coffee
Tea
Coca Cola
<!DOCTYPE html>
<html>
<head>
<style>
ul.a {list-style-position:inside;}
ul.b {list-style-position:outside;}
</style>
</head>
<body>
<ul class="a">
<li>Coffee</li>
<li>Tea</li>
<li>Coca Cola</li>
</ul>
<li>Coffee</li>
<li>Tea</li>
<li>Coca Cola</li>
</ul>
</body>
</html>
Coffee
Tea
Coca Cola
Coffee
Tea
Coca Cola
<!DOCTYPE html>
<html>
<head>
<style>
ul.demo {
list-style-type: none;
margin: 0;
padding: 0;
</style>
</head>
<body>
<p>Default list:</p>
<ul>
<li>Coffee</li>
<li>Tea</li>
<li>Coca Cola</li>
</ul>
<ul class="demo">
<li>Coffee</li>
<li>Tea</li>
<li>Coca Cola</li>
</ul>
</body>
</html>
Default list:
Coffee
Tea
Coca Cola
Coffee
Tea
Coca Cola
Anything added to the <ol> or <ul> tag, affects the entire list, while properties
added to the <li> tag will affect the individual list items:
<!DOCTYPE html>
<html>
<head>
<style>
ol {
background: #ff9999;
padding: 20px;
ul {
background: #3399ff;
padding: 20px;
}
ol li {
background: #ffe5e5;
padding: 5px;
margin-left: 35px;
ul li {
background: #cce5ff;
margin: 5px;
</style>
</head>
<body>
<ol>
<li>Coffee</li>
<li>Tea</li>
<li>Coca Cola</li>
</ol>
<ul>
<li>Coffee</li>
<li>Tea</li>
<li>Coca Cola</li>
</ul>
</body>
</html>
Coffee
Tea
Coca Cola
CSS 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:
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
</style>
</head>
<body>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr>
<td>Peter</td>
<td>Griffin</td>
</tr>
<tr>
<td>Lois</td>
<td>Griffin</td>
</tr>
</table>
</body>
</html>
Lois Griffin
<!DOCTYPE html>
<html>
<head>
<style>
table {
border-collapse: collapse;
table, td, th {
</style>
</head>
<body>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr>
<td>Peter</td>
<td>Griffin</td>
</tr>
<tr>
<td>Lois</td>
<td>Griffin</td>
</tr>
</table>
<p><b>Note:</b> If a !DOCTYPE is not specified, the border-collapse property can produce unexpected
results
</body>
</html>
Peter Griffin
Lois Griffin
Note: If a !DOCTYPE is not specified, the border-collapse property can produce
unexpected results in IE8 and earlier versions.
If you only want a border around the table, only specify the border property for
<table>:
Firstnam
Lastname
e
Peter Griffin
Lois Griffin
<!DOCTYPE html>
<html>
<head>
<style>
table {
border-collapse: collapse;
</style>
</head>
<body>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr>
<td>Peter</td>
<td>Griffin</td>
</tr>
<tr>
<td>Lois</td>
<td>Griffin</td>
</tr>
</table>
</body>
</html>
Peter Griffin
Lois Griffin
The example below sets the width of the table to 100%, and the height of the
<th> elements to 50px:
<!DOCTYPE html>
<html>
<head>
<style>
table, td, th {
table {
border-collapse: collapse;
width: 100%;
th {
height: 50px;
}
</style>
</head>
<body>
<p>Set the width of the table, and the height of the table header row:</p>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Savings</th>
</tr>
<tr>
<td>Peter</td>
<td>Griffin</td>
<td>$100</td>
</tr>
<tr>
<td>Lois</td>
<td>Griffin</td>
<td>$150</td>
</tr>
<tr>
<td>Joe</td>
<td>Swanson</td>
<td>$300</td>
</tr>
<tr>
<td>Cleveland</td>
<td>Brown</td>
<td>$250</td>
</tr>
</table>
</body>
</html>
Set the width of the table, and the height of the table header row:
Firstname Lastname Savings
Horizontal Alignment
The text-align property sets the horizontal alignment (like left, right, or
center) of the content in <th> or <td>.
By default, the content of <th> elements are center-aligned and the content of
<td> elements are left-aligned.
<!DOCTYPE html>
<html>
<head>
<style>
table, td, th {
table {
border-collapse: collapse;
width: 100%;
th {
text-align: left;
</style>
</head>
<body>
<p>This property sets the horizontal alignment (like left, right, or center) of the
content in th or td:</p>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Savings</th>
</tr>
<tr>
<td>Peter</td>
<td>Griffin</td>
<td>$100</td>
</tr>
<tr>
<td>Lois</td>
<td>Griffin</td>
<td>$150</td>
</tr>
<tr>
<td>Joe</td>
<td>Swanson</td>
<td>$300</td>
</tr>
<tr>
<td>Cleveland</td>
<td>Brown</td>
<td>$250</td>
</tr>
</table>
</body>
</html>
This property sets the horizontal alignment (like left, right, or center) of the content in
th or td:
Vertical Alignment
The vertical-align property sets the vertical alignment (like top, bottom, or
middle) of the content in <th> or <td>.
By default, the vertical alignment of the content in a table is middle (for both
<th> and <td> elements).
The following example sets the vertical text alignment to bottom for <td>
elements:
<!DOCTYPE html>
<html>
<head>
<style>
table, td, th {
table {
border-collapse: collapse;
width: 100%;
td {
height: 50px;
vertical-align: bottom;
</style>
</head>
<body>
<p>This property sets the vertical alignment (like top, bottom, or middle) of
the content in th or td.</p>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Savings</th>
</tr>
<tr>
<td>Peter</td>
<td>Griffin</td>
<td>$100</td>
</tr>
<tr>
<td>Lois</td>
<td>Griffin</td>
<td>$150</td>
</tr>
<tr>
<td>Joe</td>
<td>Swanson</td>
<td>$300</td>
</tr>
<tr>
<td>Cleveland</td>
<td>Brown</td>
<td>$250</td>
</tr>
</table>
</body>
</html>
This property sets the vertical alignment (like top, bottom, or middle) of the content in
th or td.
<!DOCTYPE html>
<html>
<head>
<style>
table, td, th {
text-align: left;
table {
border-collapse: collapse;
width: 100%;
}
th, td {
padding: 15px;
</style>
</head>
<body>
<p>This property adds space between the border and the content in a
table.</p>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Savings</th>
</tr>
<tr>
<td>Peter</td>
<td>Griffin</td>
<td>$100</td>
</tr>
<tr>
<td>Lois</td>
<td>Griffin</td>
<td>$150</td>
</tr>
<tr>
<td>Joe</td>
<td>Swanson</td>
<td>$300</td>
</tr>
<tr>
<td>Cleveland</td>
<td>Brown</td>
<td>$250</td>
</tr>
</table>
</body>
</html>
This property adds space between the border and the content in a table.
Horizontal Dividers
<!DOCTYPE html>
<html>
<head>
<style>
table {
border-collapse: collapse;
width: 100%;
th, td {
padding: 8px;
text-align: left;
border-bottom: 1px solid #ddd;
</style>
</head>
<body>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Savings</th>
</tr>
<tr>
<td>Peter</td>
<td>Griffin</td>
<td>$100</td>
</tr>
<tr>
<td>Lois</td>
<td>Griffin</td>
<td>$150</td>
</tr>
<tr>
<td>Joe</td>
<td>Swanson</td>
<td>$300</td>
</tr>
<tr>
<td>Cleveland</td>
<td>Brown</td>
<td>$250</td>
</tr>
</table>
</body>
</html>
Table Color
The example below specifies the background color and text color of <th>
elements:
<!DOCTYPE html>
<html>
<head>
<style>
table {
border-collapse: collapse;
width: 100%;
th, td {
text-align: left;
padding: 8px;
tr:nth-child(even){background-color: #f2f2f2}
th {
background-color: #4CAF50;
color: white;
</style>
</head>
<body>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Savings</th>
</tr>
<tr>
<td>Peter</td>
<td>Griffin</td>
<td>$100</td>
</tr>
<tr>
<td>Lois</td>
<td>Griffin</td>
<td>$150</td>
</tr>
<tr>
<td>Joe</td>
<td>Swanson</td>
<td>$300</td>
</tr>
<tr>
<td>Cleveland</td>
<td>Brown</td>
<td>$250</td>
</tr>
</table>
</body>
</html>
Responsive Table
A responsive table will display a horizontal scroll bar if the screen is too small to
display the full content:
<!DOCTYPE html>
<html>
<head>
<style>
table {
border-collapse: collapse;
width: 100%;
th, td {
text-align: left;
padding: 8px;
tr:nth-child(even){background-color: #f2f2f2}
</style>
</head>
<body>
<h2>Responsive Table</h2>
<p>A responsive table will display a horizontal scroll bar if the screen is too
small to display the full content. Resize the browser window to see the
effect:</p>
<p>To create a responsive table, add a container element (like div) with
<strong>overflow-x:auto</strong> around the table element:</p>
<div style="overflow-x:auto;">
<table>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Points</th>
<th>Points</th>
<th>Points</th>
<th>Points</th>
<th>Points</th>
<th>Points</th>
<th>Points</th>
<th>Points</th>
<th>Points</th>
<th>Points</th>
</tr>
<tr>
<td>Jill</td>
<td>Smith</td>
<td>50</td>
<td>50</td>
<td>50</td>
<td>50</td>
<td>50</td>
<td>50</td>
<td>50</td>
<td>50</td>
<td>50</td>
<td>50</td>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
<td>94</td>
<td>94</td>
<td>94</td>
<td>94</td>
<td>94</td>
<td>94</td>
<td>94</td>
<td>94</td>
<td>94</td>
</tr>
<tr>
<td>Adam</td>
<td>Johnson</td>
<td>67</td>
<td>67</td>
<td>67</td>
<td>67</td>
<td>67</td>
<td>67</td>
<td>67</td>
<td>67</td>
<td>67</td>
<td>67</td>
</tr>
</table>
</div>
</body>
</html>
Responsive Table
A responsive table will display a horizontal scroll bar if the screen is too small to
display the full content. Resize the browser window to see the effect:
First Name Last Name Points Points Points Points Points Points Points Points Points Points
Jill Smith 50 50 50 50 50 50 50 50 50 50
Eve Jackson 94 94 94 94 94 94 94 94 94 94
Adam Johnson 67 67 67 67 67 67 67 67 67 67
Changing an inline element to a block element, or vice versa, can be useful for
making the page look a specific way, and still follow the web standards.
<!DOCTYPE html>
<html>
<head>
<style>
li {
display: inline;
</style>
</head>
<body>
<ul>
</ul>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
a{
display: block;
</style>
</head>
<body>
</body>
</html>
HTML
CSS
JavaScript
<html>
<head>
<style>
h1.hidden {
display: none;
</style>
</head>
<body>
<p>Notice that the h1 element with display: none; does not take up any space.</p>
</body>
</html>
However, the element will still take up the same space as before. The element
will be hidden, but still affect the layout:
<!DOCTYPE html>
<html>
<head>
<style>
h1.hidden {
visibility: hidden;
}
</style>
</head>
<body>
</body>
</html>
<html>
<head>
<style>
div.ex1 {
width:500px;
margin: auto;
div.ex2 {
max-width:500px;
margin: auto;
</style>
</head>
<body>
<br>
<p><strong>Tip:</strong> Drag the browser window to smaller than 500px wide, to see the difference
between
</body>
</html>
Tip: Drag the browser window to smaller than 500px wide, to see the difference
between the two divs!
static
relative
fixed
absolute
sticky
Elements are then positioned using the top, bottom, left, and right properties.
However, these properties will not work unless the position property is set
first. They also work differently depending on the position value.
position: static;
HTML elements are positioned static by default.
Static positioned elements are not affected by the top, bottom, left, and right
properties.
<!DOCTYPE html>
<html>
<head>
<style>
div.static {
position: static;
</style>
</head>
<body>
<h2>position: static;</h2>
<p>An element with position: static; is not positioned in any special way; it is
<div class="static">
</div>
</body>
</html>
position: static;
An element with position: static; is not positioned in any special way; it is always
positioned according to the normal flow of the page:
position: relative;
An element with position: relative; is positioned relative to its normal
position.
<!DOCTYPE html>
<html>
<head>
<style>
div.relative {
position: relative;
left: 30px;
</style>
</head>
<body>
<h2>position: relative;</h2>
<p>An element with position: relative; is positioned relative to its normal position:</p>
<div class="relative">
</div>
</body>
</html>
position: relative;
A fixed element does not leave a gap in the page where it would normally have
been located.
Notice the fixed element in the lower-right corner of the page. Here is the CSS
that is used:
<!DOCTYPE html>
<html>
<head>
<style>
div.fixed {
position: fixed;
bottom: 0;
right: 0;
width: 300px;
border: 3px solid #73AD21;
</style>
</head>
<body>
<h2>position: fixed;</h2>
<p>An element with position: fixed; is positioned relative to the viewport, which means it always stays in
the same place even if the page is scrolled:</p>
<div class="fixed">
</div>
</body>
</html>
position: absolute;
An element with position: absolute; is positioned relative to the nearest
positioned ancestor (instead of positioned relative to the viewport, like fixed).
<!DOCTYPE html>
<html>
<head>
<style>
div.relative {
position: relative;
width: 400px;
height: 200px;
div.absolute {
position: absolute;
top: 80px;
right: 0;
width: 200px;
height: 100px;
</style>
</head>
<body>
<h2>position: absolute;</h2>
<p>An element with position: absolute; is positioned relative to the nearest positioned ancestor (instead
of positioned relative to the viewport, like fixed):</p>
<div class="relative">This div element has position: relative;
</div>
</body>
</html>
position: sticky;
An element with position: sticky; is positioned based on the user's scroll
position.
<html>
<head>
<style>
div.sticky {
position: -webkit-sticky;
position: sticky;
top: 0;
padding: 5px;
background-color: #cae8ca;
</style>
</head>
<body>
<p>Try to <b>scroll</b> inside this frame to understand how sticky positioning works.</p>
<div style="padding-bottom:2000px">
<p>In this example, the sticky element sticks to the top of the page (top: 0), when you reach its scroll
position.</p>
<p>Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset
concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis evertitur
eum. Affert laboramus repudiandae nec et. Inciderint efficiantur his ad. Eum no molestiae
voluptatibus.</p>
</div>
</body>
</html>
I am sticky!
In this example, the sticky element sticks to the top of the page (top: 0), when you
reach its scroll position.
Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo,
maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te,
id agam omnis evertitur eum. Affert laboramus repudiandae nec et. Inciderint
efficiantur his ad. Eum no molestiae voluptatibus.
Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo,
maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te,
id agam omnis evertitur eum. Affert laboramus repudiandae nec et. Inciderint
efficiantur his ad. Eum no molestiae voluptatibus.
Overlapping Elements
When elements are positioned, they can overlap other elements.
The z-index property specifies the stack order of an element (which element
should be placed in front of, or behind, the others).
Example
<!DOCTYPE html>
<html>
<head>
<style>
img {
position: absolute;
left: 0px;
top: 0px;
z-index: -1;
</style>
</head>
<body>
<h1>This is a heading</h1>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
.container {
position: relative;
.topleft {
position: absolute;
top: 8px;
left: 16px;
font-size: 18px;
}
img {
width: 100%;
height: auto;
opacity: 0.3;
</style>
</head>
<body>
<h2>Image Text</h2>
<div class="container">
</div>
</body>
</html>
Image Text
overflow: visible
By default, the overflow is visible, meaning that it is not clipped and it renders
outside the element's box:
<!DOCTYPE html>
<html>
<head>
<style>
div {
background-color: #eee;
width: 200px;
height: 50px;
overflow: visible;
</style>
</head>
<body>
<h2>CSS Overflow</h2>
<p>By default, the overflow is visible, meaning that it is not clipped and it renders outside the element's
box:</p>
<div>You can use the overflow property when you want to have better control of the layout. The
overflow property specifies what happens if content overflows an element's box.</div>
</body>
</html>
overflow: hidden
With the hidden value, the overflow is clipped, and the rest of the content is
hidden:
You can use the overflow property when you want to have better control of the
layout. The overflow property specifies what
<!DOCTYPE html>
<html>
<head>
<style>
div {
background-color: #eee;
width: 200px;
height: 50px;
overflow: hidden;
</style>
</head>
<body>
<h2>CSS Overflow</h2>
<p>With the hidden value, the overflow is clipped, and the rest of the content is hidden:</p>
<div>You can use the overflow property when you want to have better control of the layout. The
overflow property specifies what happens if content overflows an element's box.</div>
</body>
</html>
overflow: scroll
Setting the value to scroll, the overflow is clipped and a scrollbar is added to
scroll inside the box. Note that this will add a scrollbar both horizontally and
vertically (even if you do not need it):
<!DOCTYPE html>
<html>
<head>
<style>
div {
background-color: #eee;
width: 200px;
height: 100px;
overflow: scroll;
</style>
</head>
<body>
<h2>CSS Overflow</h2>
<p>Setting the overflow value to scroll, the overflow is clipped and a scrollbar is added to scroll inside
the box. Note that this will add a scrollbar both horizontally and vertically (even if you do not need
it):</p>
<div>You can use the overflow property when you want to have better control of the layout. The
overflow property specifies what happens if content overflows an element's box.</div>
</body>
</html>
overflow: auto
The auto value is similar to scroll, only it add scrollbars when necessary:
<!DOCTYPE html>
<html>
<head>
<style>
div {
background-color: #eee;
width: 200px;
height: 50px;
overflow: auto;
</style>
</head>
<body>
<h2>CSS Overflow</h2>
<p>The auto value is similar to scroll, only it add scrollbars when necessary:</p>
<div>You can use the overflow property when you want to have better control of the layout. The
overflow property specifies what happens if content overflows an element's box.</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
img {
float: right;
</style>
</head>
<body>
<p>In this example, the image will float to the right in the paragraph, and the text in the paragraph will
wrap around the image.</p>
</body>
</html>
Same for float left
img {
float: left;
}
Example - No float
<!DOCTYPE html>
<html>
<head>
<style>
img {
float: none;
</style>
</head>
<body>
<p>In this example, the image will be displayed just where it occurs in the text (float: none;).</p>
<p><img src="pineapple.jpg" alt="Pineapple" style="width:170px;height:170px;">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus imperdiet, nulla et dictum interdum,
nisi lorem egestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas nisl est, ultrices nec
congue eget, auctor vitae massa. Fusce luctus vestibulum augue ut aliquet. Mauris ante ligula, facilisis
sed ornare eu, lobortis in odio. Praesent convallis urna a lacus interdum ut hendrerit risus congue. Nunc
sagittis dictum nisi, sed ullamcorper ipsum dignissim ac. In at libero sed nunc venenatis imperdiet sed
ornare turpis. Donec vitae dui eget tellus gravida venenatis. Integer fringilla congue eros non
fermentum. Sed dapibus pulvinar nibh tempor porta. Cras ac leo purus. Mauris quis diam velit.</p>
</body>
</html>
In this example, the image will be displayed just where it occurs in the text (float:
none;).
The most common way to use the clear property is after you have used
a float property on an element.
When clearing floats, you should match the clear to the float. If an element is
floated to the left, then you should clear to the left. Your floated element will
continue to float, but the cleared element will appear below it on the web page.
The following example clears the float to the left. Means that no floating
elements are allowed on the left side (of the div):
<!DOCTYPE html>
<html>
<head>
<style>
.div1 {
float: left;
width: 100px;
height: 50px;
margin: 10px;
.div2 {
}
.div3 {
float: left;
width: 100px;
height: 50px;
margin: 10px;
.div4 {
clear: left;
</style>
</head>
<body>
<h2>Without clear</h2>
<div class="div1">div1</div>
<div class="div2">div2 - Notice that div2 is after div1 in the HTML code. However, since div1 floats to the
left, the text in div2
<h2>With clear</h2>
<div class="div3">div3</div>
<div class="div4">div4 - Here, clear: left; moves div4 down below the floating div3. The value "left"
clears elements floated to the left. You can also clear "right" and "both".</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
div {
padding: 5px;
}
.img1 {
float: right;
.clearfix {
overflow: auto;
.img2 {
float: right;
</style>
</head>
<body>
<p>In this example, the image is taller than the element containing it, and it is floated, so it overflows
outside of its container:</p>
<div>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus imperdiet, nulla et dictum
interdum...</div>
<p style="clear:right">Add a clearfix class with overflow: auto; to the containing element, to fix this
problem:</p>
<div class="clearfix">
<img class="img2" src="pineapple.jpg" alt="Pineapple" width="170" height="170">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus imperdiet, nulla et dictum
interdum...</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
div {
border: 3px solid #4CAF50;
padding: 5px;
.img1 {
float: right;
.clearfix::after {
content: "";
clear: both;
display: table;
.img2 {
float: right;
</style>
</head>
<body>
<p>In this example, the image is taller than the element containing it, and it is floated, so it overflows
outside of its container:</p>
<div>
<p style="clear:right">Add the clearfix hack to the containing element, to fix this problem:</p>
<div class="clearfix">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus imperdiet, nulla et dictum
interdum...</div>
</body>
</html>
Web Layout Example
It is common to do entire web layouts using the float property:
<!DOCTYPE html>
<html>
<head>
<style>
*{
box-sizing: border-box;
.header, .footer {
background-color: grey;
color: white;
padding: 15px;
.column {
float: left;
padding: 15px;
.clearfix::after {
content: "";
clear: both;
display: table;
.menu {
width: 25%;
.content {
width: 75%;
.menu ul {
list-style-type: none;
margin: 0;
padding: 0;
.menu li {
padding: 8px;
margin-bottom: 8px;
background-color: #33b5e5;
color: #ffffff;
.menu li:hover {
background-color: #0099cc;
</style>
</head>
<body>
<div class="header">
<h1>Chania</h1>
</div>
<div class="clearfix">
<ul>
<li>The Flight</li>
<li>The City</li>
<li>The Island</li>
<li>The Food</li>
</ul>
</div>
<h1>The City</h1>
<p>Chania is the capital of the Chania region on the island of Crete. The city can be divided in two
parts, the old town and the modern city.</p>
<p>You will learn more about web layout and responsive web pages in a later chapter.</p>
</div>
</div>
<div class="footer">
<p>Footer Text</p>
</div>
</body>
</html>
Chania
Footer Text
CSS Layout - inline-block
The inline-block Value
It has been possible for a long time to create a grid of boxes that fills the
browser width and wraps nicely (when the browser is resized), by using
the float property.
inline-block elements are like inline elements but they can have a width and a
height.
Examples
The old way - using float (notice that we also need to specify a clear property
for the element after the floating boxes):
<!DOCTYPE html>
<html>
<head>
<style>
.floating-box {
float: left;
width: 150px;
height: 75px;
margin: 10px;
.after-box {
clear: left;
</style>
</head>
<body>
<h2>The Old Way - using float</h2>
</body>
</html>
CSS Layout - Horizontal & Vertical
Align
Center Align Elements
To horizontally center a block element (like <div>), use margin: auto;
Setting the width of the element will prevent it from stretching out to the edges
of its container.
The element will then take up the specified width, and the remaining space will
be split equally between the two margins:
<!DOCTYPE html>
<html>
<head>
<style>
.center {
margin: auto;
width: 60%;
padding: 10px;
</style>
</head>
<body>
<p>To horizontally center a block element (like div), use margin: auto;</p>
<div class="center">
</div>
</body>
</html>
Center Align Text
To just center the text inside an element, use text-align: center;
<!DOCTYPE html>
<html>
<head>
<style>
.center {
text-align: center;
</style>
</head>
<body>
<h2>Center Text</h2>
<div class="center">
</div>
</body>
</html>
Center an Image
To center an image, use margin: auto; and make it into a block element:
<!DOCTYPE html>
<html>
<head>
<style>
img {
display: block;
margin: 0 auto;
}
</style>
</head>
<body>
<h2>Center an Image</h2>
<p>To center an image, use margin: auto; and make it into a block
element:</p>
</body>
</html>
<html>
<head>
<style>
.right {
position: absolute;
right: 0px;
width: 300px;
padding: 10px;
</style>
</head>
<body>
<h2>Right Align</h2>
<p>An example of how to right align elements with the position property:</p>
<div class="right">
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
.right {
float: right;
width: 300px;
padding: 10px;
</style>
</head>
<body>
<h2>Right Align</h2>
<p>An example of how to right align elements with the float property:</p>
<div class="right">
</div>
</body>
</html>
There is also a problem with IE8 and earlier, when using float. If the !
DOCTYPE declaration is missing, IE8 and earlier versions will add a 17px margin
on the right side. This seems to be space reserved for a scrollbar. So, always
set the !DOCTYPE declaration when using float:
<!DOCTYPE html>
<html>
<head>
<style>
body {
margin: 0;
padding: 0;
.right {
float: right;
width: 300px;
background-color: #b0e0e6;
</style>
</head>
<body>
<h2>Right Align</h2>
<div class="right">
<p><b>Note: </b>When aligning using the float property, always include the
!DOCTYPE declaration! If missing, it can produce strange results in IE
browsers.</p>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
.center {
padding: 70px 0;
}
</style>
</head>
<body>
<h2>Center Vertically</h2>
<p>In this example, we use the padding property to center the div element
vertically:</p>
<div class="center">
</div>
</body>
</html>
Center Vertically - Using line-height
Another trick is to use the line-height property with a value that is equal to
the height property.
<!DOCTYPE html>
<html>
<head>
<style>
.center {
line-height: 200px;
height: 200px;
text-align: center;
.center p {
line-height: 1.5;
display: inline-block;
vertical-align: middle;
</style>
</head>
<body>
<h2>Centering</h2>
<p>In this example, we use the line-height property with a value that is equal
to the height property to center the div element:</p>
<div class="center">
</div>
</body>
</html>
CSS Combinators
Descendant Selector
<!DOCTYPE html>
<html>
<head>
<style>
div p {
background-color: yellow;
</style>
</head>
<body>
<div>
</div>
</body>
</html>
Child Selector
The child selector selects all elements that are the immediate children of a
specified element.
The following example selects all <p> elements that are immediate children of a
<div> element:
<!DOCTYPE html>
<html>
<head>
<style>
div > p {
background-color: yellow;
</style>
</head>
<body>
<div>
<p>Paragraph 1 in the div.</p>
</div>
</body>
</html>
Sibling elements must have the same parent element, and "adjacent" means
"immediately following".
The following example selects all <p> elements that are placed immediately
after <div> elements:
<!DOCTYPE html>
<html>
<head>
<style>
div + p {
background-color: yellow;
</style>
</head>
<body>
<div>
</div>
</body>
</html>
The following example selects all <p> elements that are siblings of <div>
elements:
<!DOCTYPE html>
<html>
<head>
<style>
div ~ p {
background-color: yellow;
</style>
</head>
<body>
<p>Paragraph 1.</p>
<div>
<code>Some code.</code>
<p>Paragraph 2.</p>
</div>
<p>Paragraph 3.</p>
<code>Some code.</code>
<p>Paragraph 4.</p>
</body>
</html>
Paragraph 1.
Some code.
Paragraph 2.
Paragraph 3.
Some code.
Paragraph 4.
CSS Pseudo-classes
What are Pseudo-classes?
A pseudo-class is used to define a special state of an element.
<!DOCTYPE html>
<html>
<head>
<style>
/* unvisited link */
a:link {
color: red;
/* visited link */
a:visited {
color: green;
a:hover {
color: hotpink;
/* selected link */
a:active {
color: blue;
</style>
</head>
<body>
<p><b>Note:</b> a:hover MUST come after a:link and a:visited in the CSS
definition in order to be effective.</p>
</body>
</html>
This is a link
Note: a:hover MUST come after a:link and a:visited in the CSS definition in order to
be effective.
When you hover over the link in the example, it will change color:
<!DOCTYPE html>
<html>
<head>
<style>
a.highlight:hover {
color: #ff0000;
</style>
</head>
<body>
</body>
</html>
CSS Syntax
CSS Tutorial
Hover on <div>
An example of using the :hover pseudo-class on a <div> element:
<!DOCTYPE html>
<html>
<head>
<style>
div {
background-color: green;
color: white;
padding: 25px;
text-align: center;
div:hover {
background-color: blue;
</style>
</head>
<body>
<p>Mouse over the div element below to change its background color:</p>
</body>
</html>
Simple Tooltip Hover
Hover over a <div> element to show a <p> element (like a tooltip):
<!DOCTYPE html>
<html>
<head>
<style>
p{
display: none;
background-color: yellow;
padding: 20px;
div:hover p {
display: block;
</style>
</head>
<body>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
p:first-child {
color: blue;
</style>
</head>
<body>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
p i:first-child {
color: blue;
</style>
</head>
<body>
</body>
</html>
I am a strong person. I am a strong person.
I am a strong person. I am a strong person.
<html>
<head>
<style>
p:first-child i {
color: blue;
</style>
</head>
<body>
</body>
</html>
I am a strong person. I am a strong person.
I am a strong person. I am a strong person.
<!DOCTYPE html>
<html>
<head>
<style>
q:lang(no) {
</style>
</head>
<body>
<p>In this example, :lang defines the quotation marks for q elements with
lang="no":</p>
</body>
</html>
Some text A quote in a paragraph Some text.
In this example, :lang defines the quotation marks for q elements with lang="no":
CSS Pseudo-elements
What are Pseudo-Elements?
A CSS pseudo-element is used to style specified parts of an element.
The following example formats the first line of the text in all <p> elements:
<!DOCTYPE html>
<html>
<head>
<style>
p::first-line {
color: #ff0000;
font-variant: small-caps;
</style>
</head>
<body>
<p>You can use the ::first-line pseudo-element to add a special effect to the
first line of a text. Some more text. And even more, and more, and more, and
more, and more, and more, and more, and more, and more, and more, and
more, and more.</p>
</body>
</html>
You can use the ::first-line pseudo-element to add a special effect to the first line of a
text. Some more text. And even more, and more, and more, and more, and more, and
more, and more, and more, and more, and more, and more, and more.
The following example formats the first letter of the text in all <p> elements:
<!DOCTYPE html>
<html>
<head>
<style>
p::first-letter {
color: #ff0000;
font-size: xx-large;
}
</style>
</head>
<body>
<p>You can use the ::first-letter pseudo-element to add a special effect to the
first character of a text!</p>
</body>
</html>
You can use the ::first-letter pseudo-element to add a special effect to the first
character of a text!
<!DOCTYPE html>
<html>
<head>
<style>
p.intro::first-letter {
color: #ff0000;
font-size:200%;
</style>
</head>
<body>
</body>
</html>
This is an introduction.
This is a paragraph with some text. A bit more text even.
Multiple Pseudo-elements
Several pseudo-elements can also be combined.
In the following example, the first letter of a paragraph will be red, in an xx-
large font size. The rest of the first line will be blue, and in small-caps. The rest
of the paragraph will be the default font size and color:
<!DOCTYPE html>
<html>
<head>
<style>
p::first-letter {
color: #ff0000;
font-size: xx-large;
}
p::first-line {
color: #0000ff;
font-variant: small-caps;
</style>
</head>
<body>
</body>
</html>
You can combine the ::first-letter and ::first-line pseudo-elements to add a special
effect to the first letter and the first line of a text!
The following example inserts an image before the content of each <h1>
element:
<!DOCTYPE html>
<html>
<head>
<style>
h1::before {
content: url(smiley.gif);
</style>
</head>
<body>
<h1>This is a heading</h1>
<h1>This is a heading</h1>
</body>
</html>
CSS - The ::after Pseudo-element
The ::after pseudo-element can be used to insert some content after the
content of an element.
The following example inserts an image after the content of each <h1>
element:
<!DOCTYPE html>
<html>
<head>
<style>
h1::after {
content: url(smiley.gif);
</style>
</head>
<body>
<h1>This is a heading</h1>
<h1>This is a heading</h1>
</html>
The following example makes the selected text red on a yellow background:
<!DOCTYPE html>
<html>
<head>
<style>
color: red;
background: yellow;
}
::selection {
color: red;
background: yellow;
</style>
</head>
<body>
<p>This is a paragraph.</p>
</body>
</html>
CSS Opacity / Transparency
<!DOCTYPE html>
<html>
<head>
<style>
img {
opacity: 0.5;
</style>
</head>
<body>
<h1>Image Transparency</h1>
</body>
</html>
Image Transparency
The opacity property specifies the transparency of an element. The lower the value,
the more transparent:
<!DOCTYPE html>
<html>
<head>
<style>
img {
opacity: 0.5;
img:hover {
opacity: 1.0;
filter: alpha(opacity=100); /* For IE8 and earlier */
</style>
</head>
<body>
<h1>Image Transparency</h1>
<p>The opacity property is often used together with the :hover selector to
change the opacity on mouse-over:</p>
</body>
</html>
Image Transparency
The opacity property is often used together with the :hover selector to change the
opacity on mouse-over:
Note: In IE, a !DOCTYPE must be added for the :hover selector to work on other
elements than the a element.
Transparent Box
When using the opacity property to add transparency to the background of an
element, all of its child elements inherit the same transparency. This can make
the text inside a fully transparent element hard to read:
<!DOCTYPE html>
<html>
<head>
<style>
div {
background-color: #4CAF50;
padding: 10px;
div.first {
opacity: 0.1;
div.second {
opacity: 0.3;
div.third {
opacity: 0.6;
</style>
</head>
<body>
<h1>Transparent Box</h1>
<div><p>opacity 1 (default)</p></div>
</body>
</html>
Transparent Box
When using the opacity property to add transparency to the background of an element,
all of its child elements become transparent as well. This can make the text inside a
<!DOCTYPE html>
<html>
<head>
<style>
div {
padding: 10px;
}
div.first {
div.second {
div.third {
</style>
</head>
<body>
<h1>Transparent Box</h1>
<p>With opacity:</p>
<div><p>default</p></div>
<p>Notice how the text gets transparent as well as the background color when using
the opacity property.</p>
</body>
</html>
Text in Transparent Box
<!DOCTYPE html>
<html>
<head>
<style>
div.background {
div.transbox {
margin: 30px;
background-color: #ffffff;
opacity: 0.6;
div.transbox p {
margin: 5%;
font-weight: bold;
color: #000000;
</style>
</head>
<body>
<div class="background">
<div class="transbox">
</div>
</div>
</body>
</html>
CSS Navigation Bar
Navigation Bars
Having easy-to-use navigation is important for any web site.
With CSS you can transform boring HTML menus into good-looking navigation
bars.
In our examples we will build the navigation bar from a standard HTML list.
A navigation bar is basically a list of links, so using the <ul> and <li> elements
makes perfect sense:
<!DOCTYPE html>
<html>
<body>
<ul>
<li><a href="#home">Home</a></li>
<li><a href="#news">News</a></li>
<li><a href="#contact">Contact</a></li>
<li><a href="#about">About</a></li>
</ul>
<p>Note: We use href="#" for test links. In a real web site this would be URLs.</p>
</body>
</html>
Home
News
Contact
About
Note: We use href="#" for test links. In a real web site this would be URLs.
Now let's remove the bullets and the margins and padding from the list:
<!DOCTYPE html>
<html>
<head>
<style>
ul {
list-style-type: none;
margin: 0;
padding: 0;
</style>
</head>
<body>
<p>In this example, we remove the bullets from the list, and its default padding and
margin.</p>
<ul>
<li><a href="#home">Home</a></li>
<li><a href="#news">News</a></li>
<li><a href="#contact">Contact</a></li>
<li><a href="#about">About</a></li>
</ul>
</body>
</html>
In this example, we remove the bullets from the list, and its default padding and
margin.
Home
News
Contact
About
<!DOCTYPE html>
<html>
<head>
<style>
ul {
list-style-type: none;
margin: 0;
padding: 0;
li a {
display: block;
width: 60px;
background-color: #dddddd;
</style>
</head>
<body>
<ul>
<li><a href="#home">Home</a></li>
<li><a href="#news">News</a></li>
<li><a href="#contact">Contact</a></li>
<li><a href="#about">About</a></li>
</ul>
<p>A background color is added to the links to show the link area.</p>
<p>Notice that the whole link area is clickable, not just the text.</p>
</body>
</html>
Home
News
Contact
About
Notice that the whole link area is clickable, not just the text.
<!DOCTYPE html>
<html>
<head>
<style>
ul {
list-style-type: none;
margin: 0;
padding: 0;
width: 200px;
background-color: #f1f1f1;
li a {
display: block;
color: #000;
text-decoration: none;
li a:hover {
background-color: #555;
color: white;
</style>
</head>
<body>
<li><a href="#home">Home</a></li>
<li><a href="#news">News</a></li>
<li><a href="#contact">Contact</a></li>
<li><a href="#about">About</a></li>
</ul>
</body>
</html>
Home
News
Contact
About
<!DOCTYPE html>
<html>
<head>
<style>
ul {
list-style-type: none;
margin: 0;
padding: 0;
width: 200px;
background-color: #f1f1f1;
li a {
display: block;
color: #000;
text-decoration: none;
li a.active {
background-color: #4CAF50;
color: white;
li a:hover:not(.active) {
background-color: #555;
color: white;
</style>
</head>
<body>
<p>In this example, we create an "active" class with a green background color and a
white text. The class is added to the "Home" link.</p>
<ul>
<li><a href="#news">News</a></li>
<li><a href="#contact">Contact</a></li>
<li><a href="#about">About</a></li>
</ul>
</body>
</html>
In this example, we create an "active" class with a green background color and a white
text. The class is added to the "Home" link.
Home
News
Contact
About
Add the border property to <ul> add a border around the navbar. If you also
want borders inside the navbar, add a border-bottom to all <li> elements,
except for the last one:
<!DOCTYPE html>
<html>
<head>
<style>
ul {
list-style-type: none;
margin: 0;
padding: 0;
width: 200px;
background-color: #f1f1f1;
}
li a {
display: block;
color: #000;
text-decoration: none;
li {
text-align: center;
li:last-child {
border-bottom: none;
li a.active {
background-color: #4CAF50;
color: white;
}
li a:hover:not(.active) {
background-color: #555;
color: white;
</style>
</head>
<body>
<p>In this example, we center the navigation links and add a border to the navigation
bar.</p>
<ul>
<li><a href="#news">News</a></li>
<li><a href="#contact">Contact</a></li>
<li><a href="#about">About</a></li>
</ul>
</body>
</html>
Vertical Navigation Bar
In this example, we center the navigation links and add a border to the navigation bar.
Home
News
Contact
About
<!DOCTYPE html>
<html>
<head>
<style>
body {
margin: 0;
ul {
list-style-type: none;
margin: 0;
padding: 0;
width: 25%;
background-color: #f1f1f1;
position: fixed;
height: 100%;
overflow: auto;
li a {
display: block;
color: #000;
text-decoration: none;
li a.active {
background-color: #4CAF50;
color: white;
li a:hover:not(.active) {
background-color: #555;
color: white;
}
</style>
</head>
<body>
<ul>
<li><a href="#news">News</a></li>
<li><a href="#contact">Contact</a></li>
<li><a href="#about">About</a></li>
</ul>
<h3>Try to scroll this area, and see how the sidenav sticks to the page</h3>
<p>Notice that this div element has a left margin of 25%. This is because the side
navigation is set to 25% width. If you remove the margin, the sidenav will overlay/sit on
top of this div.</p>
<p>Also notice that we have set overflow:auto to sidenav. This will add a scrollbar
when the sidenav is too long (for example if it has over 50 links inside of it).</p>
<p>Some text..</p>
<p>Some text..</p>
<p>Some text..</p>
<p>Some text..</p>
<p>Some text..</p>
<p>Some text..</p>
<p>Some text..</p>
</div>
</body>
</html>
Home
News
Contact
About
Notice that this div element has a left margin of 25%. This is because the side
navigation is set to 25% width. If you remove the margin, the sidenav will overlay/sit
on top of this div.
Also notice that we have set overflow:auto to sidenav. This will add a scrollbar when
the sidenav is too long (for example if it has over 50 links inside of it).
Some text..
Some text..
Some text..
Some text..
Some text..
Some text..
Some text..
<!DOCTYPE html>
<html>
<head>
<style>
ul {
list-style-type: none;
margin: 0;
padding: 0;
li {
display: inline;
</style>
</head>
<body>
<ul>
<li><a href="#home">Home</a></li>
<li><a href="#news">News</a></li>
<li><a href="#contact">Contact</a></li>
<li><a href="#about">About</a></li>
</ul>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
li {
float: left;
li a {
display: block;
padding: 8px;
background-color: #dddddd;
</style>
</head>
<body>
<ul>
<li><a href="#home">Home</a></li>
<li><a href="#news">News</a></li>
<li><a href="#contact">Contact</a></li>
<li><a href="#about">About</a></li>
</ul>
<p>A background color is added to the links to show the link area. The whole link
area is clickable, not just the text.</p>
</body>
</html>
Note: If a !DOCTYPE is not specified, floating items can produce unexpected results.
A background color is added to the links to show the link area. The whole link area is
clickable, not just the text.
<!DOCTYPE html>
<html>
<head>
<style>
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #dddddd;
li {
float: left;
li a {
display: block;
padding: 8px;
</style>
</head>
<body>
<ul>
<li><a href="#home">Home</a></li>
<li><a href="#news">News</a></li>
<li><a href="#contact">Contact</a></li>
<li><a href="#about">About</a></li>
</ul>
<p>A background color is added to the list instead of each link to create a full-width
background color.</p>
</body>
</html>
A background color is added to the list instead of each link to create a full-width
background color.
<!DOCTYPE html>
<html>
<head>
<style>
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
li {
float: left;
li a {
display: block;
color: white;
text-align: center;
text-decoration: none;
}
li a:hover {
background-color: #111;
</style>
</head>
<body>
<ul>
<li><a href="#news">News</a></li>
<li><a href="#contact">Contact</a></li>
<li><a href="#about">About</a></li>
</ul>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
li {
float: left;
li a {
display: block;
color: white;
text-align: center;
text-decoration: none;
}
li a:hover:not(.active) {
background-color: #111;
.active {
background-color: #4CAF50;
</style>
</head>
<body>
<ul>
<li><a href="#news">News</a></li>
<li><a href="#contact">Contact</a></li>
<li><a href="#about">About</a></li>
</ul>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
li {
float: left;
li a {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
li a:hover:not(.active) {
background-color: #111;
.active {
background-color: #4CAF50;
</style>
</head>
<body>
<ul>
<li><a href="#home">Home</a></li>
<li><a href="#news">News</a></li>
<li><a href="#contact">Contact</a></li>
</ul>
</body>
</html>
Border Dividers
Add the border-right property to <li> to create link dividers:
<!DOCTYPE html>
<html>
<head>
<style>
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
li {
float: left;
li:last-child {
border-right: none;
li a {
display: block;
color: white;
text-align: center;
text-decoration: none;
li a:hover:not(.active) {
background-color: #111;
.active {
background-color: #4CAF50;
</style>
</head>
<body>
<ul>
<li><a href="#news">News</a></li>
<li><a href="#contact">Contact</a></li>
</ul>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
body {margin:0;}
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
position: fixed;
top: 0;
width: 100%;
li {
float: left;
li a {
display: block;
color: white;
text-align: center;
text-decoration: none;
}
li a:hover:not(.active) {
background-color: #111;
.active {
background-color: #4CAF50;
</style>
</head>
<body>
<ul>
<li><a href="#news">News</a></li>
<li><a href="#contact">Contact</a></li>
<li><a href="#about">About</a></li>
</ul>
<div style="padding:20px;margin-top:30px;background-
color:#1abc9c;height:1500px;">
<h2>The navigation bar will stay at the top of the page while scrolling</h2>
</div>
</body>
</html>
The navigation bar will stay at the top of the page while scrolling
<!DOCTYPE html>
<html>
<head>
<style>
body {margin:0;}
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
position: fixed;
bottom: 0;
width: 100%;
li {
float: left;
li a {
display: block;
color: white;
text-align: center;
text-decoration: none;
li a:hover:not(.active) {
background-color: #111;
.active {
background-color: #4CAF50;
}
</style>
</head>
<body>
<ul>
<li><a href="#news">News</a></li>
<li><a href="#contact">Contact</a></li>
<li><a href="#about">About</a></li>
</ul>
<div style="padding:20px;background-color:#1abc9c;height:1500px;">
<h2>The navigation bar will stay at the bottom of the page while scrolling</h2>
</div>
</body>
</html>
The navigation bar will stay at the bottom of the page while scrolling
<!DOCTYPE html>
<html>
<head>
<style>
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #f3f3f3;
li {
float: left;
li a {
display: block;
color: #666;
text-align: center;
text-decoration: none;
li a:hover:not(.active) {
background-color: #ddd;
li a.active {
color: white;
background-color: #4CAF50;
</style>
</head>
<body>
<ul>
<li><a href="#news">News</a></li>
<li><a href="#contact">Contact</a></li>
<li><a href="#about">About</a></li>
</ul>
</body>
</html>
CSS Dropdowns
Basic Dropdown
Create a dropdown box that appears when the user moves the mouse over an
element.
<!DOCTYPE html>
<html>
<head>
<style>
.dropdown {
position: relative;
display: inline-block;
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
z-index: 1;
.dropdown:hover .dropdown-content {
display: block;
</style>
</head>
<body>
<h2>Hoverable Dropdown</h2>
<p>Move the mouse over the text below to open the dropdown content.</p>
<div class="dropdown">
<div class="dropdown-content">
<p>Hello World!</p>
</div>
</div>
</body>
</html>
Hoverable Dropdown
Move the mouse over the text below to open the dropdown content.
Mouse over me
Dropdown Menu
Create a dropdown menu that allows the user to choose an option from a list:
<!DOCTYPE html>
<html>
<head>
<style>
.dropbtn {
background-color: #4CAF50;
color: white;
padding: 16px;
font-size: 16px;
border: none;
cursor: pointer;
.dropdown {
position: relative;
display: inline-block;
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
z-index: 1;
.dropdown-content a {
color: black;
text-decoration: none;
display: block;
}
.dropdown:hover .dropdown-content {
display: block;
.dropdown:hover .dropbtn {
background-color: #3e8e41;
</style>
</head>
<body>
<h2>Dropdown Menu</h2>
<p>Move the mouse over the button to open the dropdown menu.</p>
<div class="dropdown">
<button class="dropbtn">Dropdown</button>
<div class="dropdown-content">
</div>
</div>
<p><strong>Note:</strong> We use href="#" for test links. In a real web site this
would be URLs.</p>
</body>
</html>
Dropdown Menu
Move the mouse over the button to open the dropdown menu.
Dropdown
Note: We use href="#" for test links. In a real web site this would be URLs.
<!DOCTYPE html>
<html>
<head>
<style>
.dropbtn {
background-color: #4CAF50;
color: white;
padding: 16px;
font-size: 16px;
border: none;
cursor: pointer;
.dropdown {
position: relative;
display: inline-block;
.dropdown-content {
display: none;
position: absolute;
right: 0;
background-color: #f9f9f9;
min-width: 160px;
z-index: 1;
}
.dropdown-content a {
color: black;
text-decoration: none;
display: block;
.dropdown:hover .dropdown-content {
display: block;
.dropdown:hover .dropbtn {
background-color: #3e8e41;
</style>
</head>
<body>
<button class="dropbtn">Left</button>
</div>
</div>
<button class="dropbtn">Right</button>
<div class="dropdown-content">
</div>
</div>
</body>
</html>
Determine whether the dropdown content should go from left to right or right to left
with the left and right properties.
Left Right
Dropdown Navbar
How to add a dropdown menu inside a navigation bar.
<!DOCTYPE html>
<html>
<head>
<style>
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
li {
float: left;
}
li a, .dropbtn {
display: inline-block;
color: white;
text-align: center;
text-decoration: none;
background-color: red;
li.dropdown {
display: inline-block;
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
.dropdown-content a {
color: black;
text-decoration: none;
display: block;
text-align: left;
.dropdown:hover .dropdown-content {
display: block;
</style>
</head>
<body>
<ul>
<li><a href="#home">Home</a></li>
<li><a href="#news">News</a></li>
<li class="dropdown">
<div class="dropdown-content">
</div>
</li>
</ul>
</body>
</html>
Dropdown Image
How to add an image and other content inside the dropdown box.
<!DOCTYPE html>
<html>
<head>
<style>
.dropdown {
position: relative;
display: inline-block;
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
z-index: 1;
.dropdown:hover .dropdown-content {
display: block;
}
.desc {
padding: 15px;
text-align: center;
</style>
</head>
<body>
<h2>Dropdown Image</h2>
<p>Move the mouse over the image below to open the dropdown content.</p>
<div class="dropdown">
<div class="dropdown-content">
</div>
</div>
</body>
</html>
Dropdown Image
Move the mouse over the image below to open the dropdown content.
CSS Tooltip
Basic Tooltip
Create a tooltip that appears when the user moves the mouse over an element:
<!DOCTYPE html>
<html>
<style>
.tooltip {
position: relative;
display: inline-block;
.tooltip .tooltiptext {
visibility: hidden;
width: 120px;
background-color: black;
color: #fff;
text-align: center;
border-radius: 6px;
padding: 5px 0;
position: absolute;
z-index: 1;
.tooltip:hover .tooltiptext {
visibility: visible;
</style>
<body style="text-align:center;">
</div>
<p>Note that the position of the tooltip text isn't very good. Go back to the tutorial
and continue reading on how to position the tooltip in a desirable way.</p>
</body>
</html>
Hover over me
Note that the position of the tooltip text isn't very good. Go back to the tutorial and
continue reading on how to position the tooltip in a desirable way.
Positioning Tooltips
In this example, the tooltip is placed to the right (left:105%) of the "hoverable"
text (<div>). Also note that top:-5px is used to place it in the middle of its
container element. We use the number 5 because the tooltip text has a top and
bottom padding of 5px. If you increase its padding, also increase the value of
the top property to ensure that it stays in the middle (if this is something you
want). The same applies if you want the tooltip placed to the left
<!DOCTYPE html>
<html>
<style>
.tooltip {
position: relative;
display: inline-block;
}
.tooltip .tooltiptext {
visibility: hidden;
width: 120px;
background-color: black;
color: #fff;
text-align: center;
border-radius: 6px;
padding: 5px 0;
position: absolute;
z-index: 1;
top: -5px;
left: 105%;
.tooltip:hover .tooltiptext {
visibility: visible;
</style>
<body style="text-align:center;">
<h2>Right Tooltip</h2>
</div>
</body>
</html>
Right Tooltip
Hover over me
Left Tooltip
<!DOCTYPE html>
<html>
<style>
.tooltip {
position: relative;
display: inline-block;
}
.tooltip .tooltiptext {
visibility: hidden;
width: 120px;
background-color: black;
color: #fff;
text-align: center;
border-radius: 6px;
padding: 5px 0;
position: absolute;
z-index: 1;
top: -5px;
right: 105%;
.tooltip:hover .tooltiptext {
visibility: visible;
</style>
<body style="text-align:center;">
<h2>Left Tooltip</h2>
</div>
</body>
</html>
Left Tooltip
Hover over me
Top Tooltip
<!DOCTYPE html>
<html>
<style>
.tooltip {
position: relative;
display: inline-block;
.tooltip .tooltiptext {
visibility: hidden;
width: 120px;
background-color: black;
color: #fff;
text-align: center;
border-radius: 6px;
padding: 5px 0;
position: absolute;
z-index: 1;
bottom: 100%;
left: 50%;
margin-left: -60px;
.tooltip:hover .tooltiptext {
visibility: visible;
}
</style>
<body style="text-align:center;">
<h2>Top Tooltip</h2>
</div>
</body>
</html>
Top Tooltip
Hover over me
Bottom Tooltip
<!DOCTYPE html>
<html>
<style>
.tooltip {
position: relative;
display: inline-block;
.tooltip .tooltiptext {
visibility: hidden;
width: 120px;
background-color: black;
color: #fff;
text-align: center;
border-radius: 6px;
padding: 5px 0;
position: absolute;
z-index: 1;
top: 100%;
left: 50%;
margin-left: -60px;
.tooltip:hover .tooltiptext {
visibility: visible;
</style>
<body style="text-align:center;">
<h2>Bottom Tooltip</h2>
</div>
</body>
</html>
Bottom Tooltip
Hover over me
Tooltip Arrows
To create an arrow that should appear from a specific side of the tooltip, add
"empty" content after tooltip, with the pseudo-element class ::after together
with the content property. The arrow itself is created using borders. This will
make the tooltip look like a speech bubble.
This example demonstrates how to add an arrow to the bottom of the tooltip:
<!DOCTYPE html>
<html>
<style>
.tooltip {
position: relative;
display: inline-block;
.tooltip .tooltiptext {
visibility: hidden;
width: 120px;
background-color: black;
color: #fff;
text-align: center;
border-radius: 6px;
padding: 5px 0;
position: absolute;
z-index: 1;
bottom: 150%;
left: 50%;
margin-left: -60px;
}
.tooltip .tooltiptext::after {
content: "";
position: absolute;
top: 100%;
left: 50%;
margin-left: -5px;
border-width: 5px;
border-style: solid;
.tooltip:hover .tooltiptext {
visibility: visible;
</style>
<body style="text-align:center;">
</div>
</body>
</html>
Top Arrow
<!DOCTYPE html>
<html>
<style>
.tooltip {
position: relative;
display: inline-block;
.tooltip .tooltiptext {
visibility: hidden;
width: 120px;
background-color: black;
color: #fff;
text-align: center;
border-radius: 6px;
padding: 5px 0;
position: absolute;
z-index: 1;
top: 150%;
left: 50%;
margin-left: -60px;
.tooltip .tooltiptext::after {
content: "";
position: absolute;
bottom: 100%;
left: 50%;
margin-left: -5px;
border-width: 5px;
border-style: solid;
.tooltip:hover .tooltiptext {
visibility: visible;
</style>
<body style="text-align:center;">
</div>
</body>
</html>
Left Arrow
<!DOCTYPE html>
<html>
<style>
.tooltip {
position: relative;
display: inline-block;
border-bottom: 1px dotted black;
.tooltip .tooltiptext {
visibility: hidden;
width: 120px;
background-color: black;
color: #fff;
text-align: center;
border-radius: 6px;
padding: 5px 0;
position: absolute;
z-index: 1;
top: -5px;
left: 110%;
.tooltip .tooltiptext::after {
content: "";
position: absolute;
top: 50%;
right: 100%;
margin-top: -5px;
border-width: 5px;
border-style: solid;
.tooltip:hover .tooltiptext {
visibility: visible;
</style>
<body style="text-align:center;">
</div>
</body>
</html>
<html>
<style>
.tooltip {
position: relative;
display: inline-block;
.tooltip .tooltiptext {
visibility: hidden;
width: 120px;
background-color: black;
color: #fff;
text-align: center;
border-radius: 6px;
padding: 5px 0;
position: absolute;
z-index: 1;
top: -5px;
right: 110%;
.tooltip .tooltiptext::after {
content: "";
position: absolute;
top: 50%;
left: 100%;
margin-top: -5px;
border-width: 5px;
border-style: solid;
.tooltip:hover .tooltiptext {
visibility: visible;
</style>
<body style="text-align:center;">
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<style>
.tooltip {
position: relative;
display: inline-block;
.tooltip .tooltiptext {
visibility: hidden;
width: 120px;
background-color: black;
color: #fff;
text-align: center;
border-radius: 6px;
padding: 5px 0;
position: absolute;
z-index: 1;
bottom: 100%;
left: 50%;
margin-left: -60px;
opacity: 0;
.tooltip:hover .tooltiptext {
visibility: visible;
opacity: 1;
</style>
<body style="text-align:center;">
<p>When you move the mouse over the text below, the tooltip text will fade in and
take 1 second to go from completely invisible to visible.</p>
</div>
</body>
</html>
When you move the mouse over the text below, the tooltip text will fade in and take 1
second to go from completely invisible to visible.
Hover over me
CSS Image Gallery
<!DOCTYPE html>
<html>
<head>
<style>
div.gallery {
margin: 5px;
float: left;
width: 180px;
div.gallery:hover {
div.gallery img {
width: 100%;
height: auto;
div.desc {
padding: 15px;
text-align: center;
</style>
</head>
<body>
<div class="gallery">
</a>
</div>
<div class="gallery">
</a>
</div>
<div class="gallery">
</a>
</div>
<div class="gallery">
</a>
</div>
</body>
</html>
CSS Image Sprites
Create a Navigation List
<!DOCTYPE html>
<html>
<head>
<style>
#navlist {
position: relative;
#navlist li {
margin: 0;
padding: 0;
list-style: none;
position: absolute;
top: 0;
height: 44px;
display: block;
#home {
left: 0px;
width: 46px;
background: url('img_navsprites.gif') 0 0;
#prev {
left: 63px;
width: 43px;
#next {
left: 129px;
width: 43px;
</style>
</head>
<body>
<ul id="navlist">
</body>
</html>
<html>
<head>
<style>
#navlist {
position: relative;
#navlist li {
margin: 0;
padding: 0;
list-style: none;
position: absolute;
top: 0;
}
#navlist li, #navlist a {
height: 44px;
display: block;
#home {
left: 0px;
width: 46px;
background: url('img_navsprites_hover.gif') 0 0;
#prev {
left: 63px;
width: 43px;
#next {
left: 129px;
width: 43px;
}
#home a:hover {
#prev a:hover {
#next a:hover {
</style>
</head>
<body>
<ul id="navlist">
</ul>
</body>
</html>
CSS Forms
<!DOCTYPE html>
<html>
<style>
input[type=text], select {
width: 100%;
margin: 8px 0;
display: inline-block;
border-radius: 4px;
box-sizing: border-box;
input[type=submit] {
width: 100%;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
input[type=submit]:hover {
background-color: #45a049;
div {
border-radius: 5px;
background-color: #f2f2f2;
padding: 20px;
</style>
<body>
<div>
<form action="/action_page.php">
<label for="fname">First Name</label>
<label for="country">Country</label>
<option value="australia">Australia</option>
<option value="canada">Canada</option>
<option value="usa">USA</option>
</select>
</form>
</div>
</body>
</html>
Country
Submit
Styling Input Fields
<!DOCTYPE html>
<html>
<head>
<style>
input {
width: 100%;
}
</style>
</head>
<body>
<form>
<label for="fname">First Name</label>
<input type="text" id="fname" name="fname">
</form>
</body>
First Name
Padded Inputs
Use the padding property to add space inside the text field.
Tip: When you have many inputs after each other, you might also want to add
some margin, to add more space outside of them:
<!DOCTYPE html>
<html>
<head>
<style>
input[type=text] {
width: 100%;
margin: 8px 0;
box-sizing: border-box;
</style>
</head>
<body>
<form>
</form>
</body>
First Name
Last Name
Bordered Inputs
Use the border property to change the border size and color, and use
the border-radius property to add rounded corners:
<!DOCTYPE html>
<html>
<head>
<style>
input[type=text] {
width: 100%;
margin: 8px 0;
box-sizing: border-box;
border-radius: 4px;
</style>
</head>
<body>
<form>
</form>
</body>
First Name
Last Name
If you only want a bottom border, use the border-bottom property:
<!DOCTYPE html>
<html>
<head>
<style>
input[type=text] {
width: 100%;
margin: 8px 0;
box-sizing: border-box;
border: none;
</style>
</head>
<body>
<form>
</form>
</body>
</html>
First Name
Last Name
Colored Inputs
Use the background-color property to add a background color to the input,
and the color property to change the text color:
<!DOCTYPE html>
<html>
<head>
<style>
input[type=text] {
width: 100%;
margin: 8px 0;
box-sizing: border-box;
border: none;
background-color: #3CBC8D;
color: white;
</style>
</head>
<body>
<form>
</form>
</body>
</html>
First Name
John
Last Name
Doe
Focused Inputs
By default, some browsers will add a blue outline around the input when it gets
focus (clicked on). You can remove this behavior by adding outline: none; to
the input.
Use the :focus selector to do something with the input field when it gets focus:
<!DOCTYPE html>
<html>
<head>
<style>
input[type=text] {
width: 100%;
padding: 12px 20px;
margin: 8px 0;
box-sizing: border-box;
border: 1px solid #555;
outline: none;
}
input[type=text]:focus {
background-color: lightblue;
}
</style>
</head>
<body>
<p>In this example, we use the :focus selector to add a background color to the text
field when it gets focused (clicked on):</p>
<form>
<label for="fname">First Name</label>
<input type="text" id="fname" name="fname" value="John">
<label for="lname">Last Name</label>
<input type="text" id="lname" name="lname" value="Doe">
</form>
</body>
</html>
In this example, we use the :focus selector to add a background color to the text field
when it gets focused (clicked on):
First Name
John
Last Name
Doe
<!DOCTYPE html>
<html>
<head>
<style>
input[type=text] {
width: 100%;
padding: 12px 20px;
margin: 8px 0;
box-sizing: border-box;
border: 3px solid #ccc;
-webkit-transition: 0.5s;
transition: 0.5s;
outline: none;
}
input[type=text]:focus {
border: 3px solid #555;
}
</style>
</head>
<body>
<p>In this example, we use the :focus selector to add a black border color to
the text field when it gets focused (clicked on):</p>
<p>Note that we have added the CSS3 transition property to animate the
border color (takes 0.5 seconds to change the color on focus).</p>
<form>
<label for="fname">First Name</label>
<input type="text" id="fname" name="fname" value="John">
<label for="lname">Last Name</label>
<input type="text" id="lname" name="lname" value="Doe">
</form>
</body>
</html>
In this example, we use the :focus selector to add a black border color to the text field
when it gets focused (clicked on):
Note that we have added the CSS3 transition property to animate the border color
(takes 0.5 seconds to change the color on focus).
First Name
John
Last Name
Doe
<!DOCTYPE html>
<html>
<head>
<style>
input[type=text] {
width: 100%;
box-sizing: border-box;
border-radius: 4px;
font-size: 16px;
background-color: white;
background-image: url('searchicon.png');
background-repeat: no-repeat;
</style>
</head>
<body>
<form>
</form>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
input[type=text] {
width: 130px;
box-sizing: border-box;
border-radius: 4px;
font-size: 16px;
background-color: white;
background-image: url('searchicon.png');
background-repeat: no-repeat;
input[type=text]:focus {
width: 100%;
</style>
</head>
<body>
<form>
</form>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
textarea {
width: 100%;
height: 150px;
box-sizing: border-box;
border-radius: 4px;
background-color: #f8f8f8;
font-size: 16px;
resize: none;
</style>
</head>
<body>
<p><strong>Tip:</strong> Use the resize property to prevent textareas from being
resized (disable the "grabber" in the bottom right corner):</p>
<form>
<textarea>Some text...</textarea>
</form>
</body>
</html>
Tip: Use the resize property to prevent textareas from being resized (disable the
"grabber" in the bottom right corner):
<html>
<head>
<style>
select {
width: 100%;
border: none;
border-radius: 4px;
background-color: #f1f1f1;
</style>
</head>
<body>
<form>
<option value="au">Australia</option>
<option value="ca">Canada</option>
<option value="usa">USA</option>
</select>
</form>
</body>
</html>
Styling Input Buttons
<!DOCTYPE html>
<html>
<head>
<style>
background-color: #4CAF50;
border: none;
color: white;
text-decoration: none;
cursor: pointer;
</style>
</head>
<body>
</body>
</html>
Styled input
buttons.
Reset Submit
CSS Counters
Automatic Numbering With Counters
CSS counters are like "variables". The variable values can be incremented by
CSS rules (which will track how many times they are used).
The following example creates a counter for the page (in the body selector),
then increments the counter value for each <h2> element and adds "Section
<value of the counter>:" to the beginning of each <h2> element:
<!DOCTYPE html>
<html>
<head>
<style>
body {
counter-reset: section;
h2::before {
counter-increment: section;
</style>
</head>
<body>
<h2>HTML Tutorial</h2>
<h2>CSS Tutorial</h2>
<h2>JavaScript Tutorial</h2>
</html>
Nesting Counters
The following example creates one counter for the page (section) and one
counter for each <h1> element (subsection). The "section" counter will be
counted for each <h1> element with "Section <value of the section counter>.",
and the "subsection" counter will be counted for each <h2> element with
"<value of the section counter>.<value of the subsection counter>":
<!DOCTYPE html>
<html>
<head>
<style>
body {
counter-reset: section;
h1 {
counter-reset: subsection;
}
h1::before {
counter-increment: section;
h2::before {
counter-increment: subsection;
</style>
</head>
<body>
<h1>HTML tutorials:</h1>
<h2>HTML Tutorial</h2>
<h2>CSS Tutorial</h2>
<h1>Scripting tutorials:</h1>
<h2>JavaScript</h2>
<h2>VBScript</h2>
<h1>XML tutorials:</h1>
<h2>XML</h2>
<h2>XSL</h2>
</body>
</html>
2.2 VBScript
3.2 XSL
A counter can also be useful to make outlined lists because a new instance of a
counter is automatically created in child elements. Here we use
the counters() function to insert a string between different levels of nested
counters:
<!DOCTYPE html>
<html>
<head>
<style>
ol {
counter-reset: section;
list-style-type: none;
li::before {
counter-increment: section;
</style>
</head>
<body>
<ol>
<li>item</li>
<li>item
<ol>
<li>item</li>
<li>item</li>
<li>item
<ol>
<li>item</li>
<li>item</li>
<li>item</li>
</ol>
</li>
<li>item</li>
</ol>
</li>
<li>item</li>
<li>item</li>
</ol>
<ol>
<li>item</li>
<li>item</li>
</ol>
</body>
</html>
1. item
2. item
1. item
2. item
3. item
1. item
2. item
3. item
4. item
3. item
4. item
1. item
2. item
CSS Website Layout
<!DOCTYPE html>
<html lang="en">
<head>
<title>CSS Website Layout</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
*{
box-sizing: border-box;
}
body {
margin: 0;
}
/* Responsive layout - makes the three columns stack on top of each other instead of
next to each other */
@media (max-width:600px) {
.column {
width: 100%;
}
}
</style>
</head>
<body>
<div class="header">
<h1>Header</h1>
<p>Resize the browser window to see the responsive effect.</p>
</div>
<div class="topnav">
<a href="#">Link</a>
<a href="#">Link</a>
<a href="#">Link</a>
</div>
<div class="row">
<div class="column">
<h2>Column</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas sit amet
pretium urna. Vivamus venenatis velit nec neque ultricies, eget elementum magna
tristique. Quisque vehicula, risus eget aliquam placerat, purus leo tincidunt eros,
eget luctus quam orci in velit. Praesent scelerisque tortor sed accumsan
convallis.</p>
</div>
<div class="column">
<h2>Column</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas sit amet
pretium urna. Vivamus venenatis velit nec neque ultricies, eget elementum magna
tristique. Quisque vehicula, risus eget aliquam placerat, purus leo tincidunt eros,
eget luctus quam orci in velit. Praesent scelerisque tortor sed accumsan
convallis.</p>
</div>
<div class="column">
<h2>Column</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas sit amet
pretium urna. Vivamus venenatis velit nec neque ultricies, eget elementum magna
tristique. Quisque vehicula, risus eget aliquam placerat, purus leo tincidunt eros,
eget luctus quam orci in velit. Praesent scelerisque tortor sed accumsan
convallis.</p>
</div>
</div>
</body>
</html>
Header
Resize the browser window to see the responsive effect.
LinkLinkLink
Footer
The footer is placed at the bottom of your page. It often contains information
like copyright and contact info:
<!DOCTYPE html>
<html lang="en">
<head>
<style>
*{
box-sizing: border-box;
}
body {
margin: 0;
.header {
background-color: #f1f1f1;
padding: 20px;
text-align: center;
.topnav {
overflow: hidden;
background-color: #333;
.topnav a {
float: left;
display: block;
color: #f2f2f2;
text-align: center;
text-decoration: none;
}
.topnav a:hover {
background-color: #ddd;
color: black;
.column {
float: left;
padding: 10px;
.column.side {
width: 25%;
/* Middle column */
.column.middle {
width: 50%;
content: "";
display: table;
clear: both;
/* Responsive layout - makes the three columns stack on top of each other instead of next to each other
*/
.column.side, .column.middle {
width: 100%;
.footer {
background-color: #f1f1f1;
padding: 10px;
text-align: center;
</style>
</head>
<body>
<div class="header">
<h1>Header</h1>
<p>Resize the browser window to see the responsive effect.</p>
</div>
<div class="topnav">
<a href="#">Link</a>
<a href="#">Link</a>
<a href="#">Link</a>
</div>
<div class="row">
<h2>Side</h2>
</div>
<h2>Main Content</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas sit amet pretium urna. Vivamus
venenatis velit nec neque ultricies, eget elementum magna tristique. Quisque vehicula, risus eget
aliquam placerat, purus leo tincidunt eros, eget luctus quam orci in velit. Praesent scelerisque tortor sed
accumsan convallis.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas sit amet pretium urna. Vivamus
venenatis velit nec neque ultricies, eget elementum magna tristique. Quisque vehicula, risus eget
aliquam placerat, purus leo tincidunt eros, eget luctus quam orci in velit. Praesent scelerisque tortor sed
accumsan convallis.</p>
</div>
<h2>Side</h2>
</div>
<div class="footer">
<p>Footer</p>
</div>
</body>
</html>
Header
Resize the browser window to see the responsive effect.
LinkLinkLink
Footer
CSS3 Introduction
CSS3 is the latest standard for CSS.
CSS3 Modules
CSS3 has been split into "modules". It contains the "old CSS specification"
(which has been split into smaller pieces). In addition, new modules are added.
Selectors
Box Model
Backgrounds and Borders
Image Values and Replaced Content
Text Effects
2D/3D Transformations
Animations
Multiple Column Layout
User Interface
CSS3 Rounded Corners
CSS3 border-radius Property
With CSS3, you can give any element "rounded corners", by using the border-
radius property.
<!DOCTYPE html>
<html>
<head>
<style>
#rcorners1 {
border-radius: 25px;
background: #73AD21;
padding: 20px;
width: 200px;
height: 150px;
#rcorners2 {
border-radius: 25px;
padding: 20px;
width: 200px;
height: 150px;
#rcorners3 {
border-radius: 25px;
background: url(paper.gif);
background-repeat: repeat;
padding: 20px;
width: 200px;
height: 150px;
</style>
</head>
<body>
</body>
</html>
CSS3 border-radius - Specify Each Corner
If you specify only one value for the border-radius property, this radius will be
applied to all 4 corners.
However, you can specify each corner separately if you wish. Here are the
rules:
<!DOCTYPE html>
<html>
<head>
<style>
#rcorners4 {
background: #73AD21;
padding: 20px;
width: 200px;
height: 150px;
#rcorners5 {
background: #73AD21;
padding: 20px;
width: 200px;
height: 150px;
#rcorners6 {
background: #73AD21;
padding: 20px;
width: 200px;
height: 150px;
</style>
</head>
<body>
<p id="rcorners4"></p>
<p>Three values - border-radius: 15px 50px 30px:</p>
<p id="rcorners5"></p>
<p id="rcorners6"></p>
</body>
</html>
CSS3 Border Images
Browser Support
The numbers in the table specify the first browser version that fully supports the
property.
Numbers followed by -webkit-, -moz-, or -o- specify the first version that
worked with a prefix.
Property
border-image 16.0 11.0 15.0 6.0
4.0 -webkit- 3.5 -moz- 3.1 -webk
The border-image property takes the image and slices it into nine sections, like
a tic-tac-toe board. It then places the corners at the corners, and the middle
sections are repeated or stretched as you specify.
Here, the middle sections of the image are repeated to create the border:
An image as a border!
<!DOCTYPE html>
<html>
<head>
<style>
#borderimg {
padding: 15px;
</style>
</head>
<body>
</body>
</html>
Here, the middle sections of the image are repeated to create the border.
Here is the original image:
Note: Internet Explorer 10, and earlier versions, do not support the border-image
property.
Here, the middle sections of the image are stretched to create the border:
<!DOCTYPE html>
<html>
<head>
<style>
#borderimg {
padding: 15px;
</style>
</head>
<body>
<p>The border-image property specifies an image to be used as the border
around an element:</p>
</body>
</html>
Here, the middle sections of the image are stretched to create the border.
Note: Internet Explorer 10, and earlier versions, do not support the border-image
property.
<html>
<head>
<style>
#borderimg1 {
padding: 15px;
#borderimg2 {
padding: 15px;
#borderimg3 {
padding: 15px;
</style>
</head>
<body>
</body>
</html>
CSS3 Backgrounds
Browser Support
The numbers in the table specify the first browser version that fully supports the
property.
Numbers followed by -webkit-, -moz-, or -o- specify the first version that
worked with a prefix.
Property
The different background images are separated by commas, and the images are
stacked on top of each other, where the first image is closest to the viewer.
The following example has two background images, the first image is a flower
(aligned to the bottom and right) and the second image is a paper background
(aligned to the top-left corner):
<!DOCTYPE html>
<html>
<head>
<style>
#example1 {
padding: 15px;
</style>
</head>
<body>
<div id="example1">
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam
nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat
volutpat.</p>
<p>Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper
suscipit lobortis nisl ut aliquip ex ea commodo consequat.</p>
</div>
</body>
</html>
CSS3 Background Size
The CSS3 background-size property allows you to specify the size of
background images.
Before CSS3, the size of a background image was the actual size of the image.
CSS3 allows us to re-use background images in different contexts.
The size can be specified in lengths, percentages, or by using one of the two
keywords: contain or cover.
The following example resizes a background image to much smaller than the
original image (using pixels):
<!DOCTYPE html>
<html>
<head>
<style>
#example1 {
background:url(img_flwr.gif);
background-repeat: no-repeat;
padding:15px;
#example2 {
background:url(img_flwr.gif);
background-repeat: no-repeat;
padding:15px;
</style>
</head>
<body>
<p>Original background-image:</p>
<div id="example1">
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam
nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat
volutpat.</p>
<p>Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper
suscipit lobortis nisl ut aliquip ex ea commodo consequat.</p>
</div>
<p>Resized background-image:</p>
<div id="example2">
<h2>Lorem Ipsum Dolor</h2>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam
nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat
volutpat.</p>
<p>Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper
suscipit lobortis nisl ut aliquip ex ea commodo consequat.</p>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
.div1 {
height:150px;
width:180px;
background:url(img_flwr.gif);
background-repeat: no-repeat;
.div2 {
height:150px;
width:180px;
background:url(img_flwr.gif);
background-repeat: no-repeat;
background-size: contain;
.div3 {
height:150px;
width:180px;
background:url(img_flwr.gif);
background-repeat: no-repeat;
background-size: cover;
</style>
</head>
<body>
<p>Original image:</p>
<div class="div1">
</div>
<div class="div2">
</div>
<p>Using the "cover" keyword:</p>
<div class="div3">
</div>
</body>
</html>
Define Sizes of Multiple Background Images
The background-size property also accepts multiple values for background size
(using a comma-separated list), when working with multiple backgrounds.
The following example has three background images specified, with different
background-size value for each image:
<!DOCTYPE html>
<html>
<head>
<style>
#example1 {
padding: 15px;
</style>
</head>
<body>
<div id="example1">
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam
nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat
volutpat.</p>
<p>Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper
suscipit lobortis nisl ut aliquip ex ea commodo consequat.</p>
</div>
</body>
</html>
Full Size Background Image
Now we want to have a background image on a website that covers the entire
browser window at all times.
Fill the entire page with the image (no white space)
Scale image as needed
Center image on page
Do not cause scrollbars
The following example shows how to do it; Use the html element (the html
element is always at least the height of the browser window). Then set a fixed
and centered background on it. Then adjust its size with the background-size
property:
<!DOCTYPE html>
<html>
<head>
<style>
html {
background-size: cover;
}
body {
color: white;
</style>
</head>
<body>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam
nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat
volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper
suscipit lobortis nisl ut aliquip ex ea commodo consequat.</p>
</body>
</html>
CSS3 background-origin Property
The CSS3 background-origin property specifies where the background image
is positioned.
border-box - the background image starts from the upper left corner of
the border
padding-box - (default) the background image starts from the upper left
corner of the padding edge
content-box - the background image starts from the upper left corner of
the content
<!DOCTYPE html>
<html>
<head>
<style>
#example1 {
padding: 35px;
background: url(img_flwr.gif);
background-repeat: no-repeat;
#example2 {
padding: 35px;
background: url(img_flwr.gif);
background-repeat: no-repeat;
background-origin: border-box;
#example3 {
padding: 35px;
background: url(img_flwr.gif);
background-repeat: no-repeat;
background-origin: content-box;
}
</style>
</head>
<body>
<div id="example1">
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam
nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat
volutpat.</p>
<p>Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper
suscipit lobortis nisl ut aliquip ex ea commodo consequat.</p>
</div>
<p>background-origin: border-box:</p>
<div id="example2">
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam
nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat
volutpat.</p>
<p>Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper
suscipit lobortis nisl ut aliquip ex ea commodo consequat.</p>
</div>
<p>background-origin: content-box:</p>
<div id="example3">
<p>Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper
suscipit lobortis nisl ut aliquip ex ea commodo consequat.</p>
</div>
</body>
</html>
CSS3 background-clip Property
The CSS3 background-clip property specifies the painting area of the
background.
<!DOCTYPE html>
<html>
<head>
<style>
#example1 {
padding:35px;
background: yellow;
#example2 {
padding:35px;
background: yellow;
background-clip: padding-box;
}
#example3 {
padding:35px;
background: yellow;
background-clip: content-box;
</style>
</head>
<body>
<div id="example1">
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam
nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat
volutpat.</p>
</div>
<p>background-clip: padding-box:</p>
<div id="example2">
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam
nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat
volutpat.</p>
</div>
<p>background-clip: content-box:</p>
<div id="example3">
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam
nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat
volutpat.</p>
</div>
</body>
</html>
CSS3 Gradients
CSS3 gradients let you display smooth transitions between two or more
specified colors.
Earlier, you had to use images for these effects. However, by using CSS3
gradients you can reduce download time and bandwidth usage. In addition,
elements with gradients look better when zoomed, because the gradient is
generated by the browser.
Browser Support
The numbers in the table specify the first browser version that fully supports the
property.
Numbers followed by -webkit-, -moz-, or -o- specify the first version that
worked with a prefix.
Property
Syntax
background: linear-gradient(direction, color-stop1, color-stop2, ...);
<!DOCTYPE html>
<html>
<head>
<style>
#grad1 {
height: 200px;
background: red; /* For browsers that do not support gradients */
background: -webkit-linear-gradient(red, yellow); /* For Safari 5.1 to 6.0 */
background: -o-linear-gradient(red, yellow); /* For Opera 11.1 to 12.0 */
background: -moz-linear-gradient(red, yellow); /* For Firefox 3.6 to 15 */
background: linear-gradient(red, yellow); /* Standard syntax (must be last) */
}
</style>
</head>
<body>
<div id="grad1"></div>
<p><strong>Note:</strong> Internet Explorer 9 and earlier versions do not support
gradients.</p>
</body>
</html>
The following example shows a linear gradient that starts from the left. It starts
red, transitioning to yellow:
<!DOCTYPE html>
<html>
<head>
<style>
#grad1 {
height: 200px;
</style>
</head>
<body>
<div id="grad1"></div>
</body>
</html>
Linear Gradient - Diagonal
You can make a gradient diagonally by specifying both the horizontal and
vertical starting positions.
The following example shows a linear gradient that starts at top left (and goes
to bottom right). It starts red, transitioning to yellow:
<!DOCTYPE html>
<html>
<head>
<style>
#grad1 {
height: 200px;
</style>
</head>
<body>
<div id="grad1"></div>
</body>
</html>
Using Angles
If you want more control over the direction of the gradient, you can define an
angle, instead of the predefined directions (to bottom, to top, to right, to left, to
bottom right, etc.).
Syntax
background: linear-gradient(angle, color-stop1, color-stop2);
The angle is specified as an angle between a horizontal line and the gradient
line.
<!DOCTYPE html>
<html>
<head>
<style>
#grad1 {
height: 100px;
#grad2 {
height: 100px;
#grad3 {
height: 100px;
#grad4 {
height: 100px;
</style>
</head>
<body>
</body>
</html>
Using Multiple Color Stops
The following example shows a linear gradient (from top to bottom) with
multiple color stops:
<!DOCTYPE html>
<html>
<head>
<style>
#grad1 {
height: 200px;
background: -webkit-linear-gradient(red, yellow, green); /* For Safari
5.1 to 6.0 */
#grad2 {
height: 200px;
#grad3 {
height: 200px;
</style>
</head>
<body>
<div id="grad1"></div>
<div id="grad2"></div>
<div id="grad3"></div>
</body>
</html>
Using Transparency
CSS3 gradients also support transparency, which can be used to create fading
effects.
To add transparency, we use the rgba() function to define the color stops. The
last parameter in the rgba() function can be a value from 0 to 1, and it defines
the transparency of the color: 0 indicates full transparency, 1 indicates full color
(no transparency).
The following example shows a linear gradient that starts from the left. It starts
fully transparent, transitioning to full color red:
<!DOCTYPE html>
<html>
<head>
<style>
#grad1 {
height: 200px;
</style>
</head>
<body>
<p>To add transparency, we use the rgba() function to define the color stops.
The last parameter in the rgba() function can be a value from 0 to 1, and it
defines the transparency of the color: 0 indicates full transparency, 1 indicates
full color (no transparency).</p>
<div id="grad1"></div>
</body>
</html>
Repeating a linear-gradient
The repeating-linear-gradient() function is used to repeat linear gradients:
<!DOCTYPE html>
<html>
<head>
<style>
#grad1 {
height: 200px;
#grad2 {
height: 200px;
background: -webkit-repeating-linear-gradient(45deg,red,yellow
7%,green 10%); /* For Safari 5.1 to 6.0 */
background: -moz-repeating-linear-gradient(45deg,red,yellow
7%,green 10%); /* For Firefox 3.6 to 15 */
#grad3 {
height: 200px;
background: -webkit-repeating-linear-gradient(190deg,red,yellow
7%,green 10%); /* For Safari 5.1 to 6.0 */
background: -moz-repeating-linear-gradient(190deg,red,yellow
7%,green 10%); /* For Firefox 3.6 to 15 */
#grad4 {
height: 200px;
background: -webkit-repeating-linear-gradient(90deg,red,yellow
7%,green 10%); /* For Safari 5.1 to 6.0 */
background: -moz-repeating-linear-gradient(90deg,red,yellow
7%,green 10%); /* For Firefox 3.6 to 15 */
</style>
</head>
<body>
<div id="grad2"></div>
<div id="grad3"></div>
<div id="grad4"></div>
</body>
</html>
CSS3 Radial Gradients
A radial gradient is defined by its center.
To create a radial gradient you must also define at least two color stops.
Syntax
background: radial-gradient(shape size at position, start-color, ..., last-
color);
The following example shows a radial gradient with evenly spaced color stops:
<!DOCTYPE html>
<html>
<head>
<style>
#grad1 {
height: 150px;
width: 200px;
</style>
</head>
<body>
<div id="grad1"></div>
<p><strong>Note:</strong> Internet Explorer 9 and earlier versions do not
support gradients.</p>
</body>
</html>
The following example shows a radial gradient with differently spaced color
stops:
<!DOCTYPE html>
<html>
<head>
<style>
#grad1 {
height: 150px;
width: 200px;
</style>
</head>
<body>
<div id="grad1"></div>
</body>
</html>
Set Shape
The shape parameter defines the shape. It can take the value circle or ellipse.
The default value is ellipse.
The following example shows a radial gradient with the shape of a circle:
<!DOCTYPE html>
<html>
<head>
<style>
#grad1 {
height: 150px;
width: 200px;
#grad2 {
height: 150px;
width: 200px;
</style>
</head>
<body>
<div id="grad1"></div>
<p><strong>Circle:</strong></p>
<div id="grad2"></div>
<p><strong>Note:</strong> Internet Explorer 9 and earlier versions do not
support gradients.</p>
</body>
</html>
Repeating a radial-gradient
The repeating-radial-gradient() function is used to repeat radial gradients:
<!DOCTYPE html>
<html>
<head>
<style>
#grad1 {
height: 150px;
width: 200px;
</style>
</head>
<body>
<div id="grad1"></div>
</body>
</html>
CSS3 Shadow Effects
CSS3 Shadow Effects
With CSS3 you can add shadow to text and to elements.
text-shadow
box-shadow
Browser Support
The numbers in the table specify the first browser version that fully supports the
property.
Numbers followed by -webkit- or -moz- specifies the first version that worked
with a prefix.
Property
text-shadow 4.0 10.0 3.5 4.0
In its simplest use, you only specify the horizontal shadow (2px) and the
vertical shadow (2px):
<html>
<head>
<style>
h1 {
</style>
</head>
<body>
<h1>Text-shadow effect!</h1>
<p><b>Note:</b> Internet Explorer 9 and earlier versions, do not support the
text-shadow property.</p>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
</style>
</head>
<body>
<h1>Text-shadow effect!</h1>
<p><b>Note:</b> Internet Explorer 9 and earlier versions, do not support the
text-shadow property.</p>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
</style>
</head>
<body>
<h1>Text-shadow effect!</h1>
<p><b>Note:</b> Internet Explorer 9 and earlier versions, do not support the
text-shadow property.</p>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
color: white;
</style>
</head>
<body>
<h1>Text-shadow effect!</h1>
<p><b>Note:</b> Internet Explorer 9 and earlier versions, do not support the
text-shadow property.</p>
</body>
</html>
Multiple Shadows
To add more than one shadow to the text, you can add a comma-separated list
of shadows.
The following example shows a red and blue neon glow shadow:
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
</style>
</head>
<body>
<h1>Text-shadow effect!</h1>
</body>
</html>
You can also use the text-shadow property to create a plain border
around some text (without shadows):
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
color: yellow;
}
</style>
</head>
<body>
</body>
</html>
In its simplest use, you only specify the horizontal shadow and the vertical
shadow:
<!DOCTYPE html>
<html>
<head>
<style>
div {
width: 300px;
height: 100px;
padding: 15px;
background-color: yellow;
</style>
</head>
<body>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
div {
width: 300px;
height: 100px;
padding: 15px;
background-color: yellow;
</style>
</head>
<body>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
div {
width: 300px;
height: 100px;
padding: 15px;
background-color: yellow;
</style>
</head>
<body>
</body>
</html>
Cards
An example of using the box-shadow property to create paper-like cards:
<!DOCTYPE html>
<html>
<head>
<style>
div.card {
width: 250px;
text-align: center;
div.header {
background-color: #4CAF50;
color: white;
padding: 10px;
font-size: 40px;
div.container {
padding: 10px;
</style>
</head>
<body>
<h2>Cards</h2>
<div class="card">
<div class="header">
<h1>1</h1>
</div>
<div class="container">
<p>January 1, 2016</p>
</div>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
div.polaroid {
width: 250px;
text-align: center;
div.container {
padding: 10px;
}
</style>
</head>
<body>
<div class="polaroid">
<div class="container">
<p>Hardanger, Norway</p>
</div>
</div>
</body>
</html>
CSS3 Text
CSS3 Text
CSS3 contains several new text features.
In this chapter you will learn about the following text properties:
text-overflow
word-wrap
word-break
Browser Support
The numbers in the table specify the first browser version that fully supports the
property.
Numbers followed by -o- specify the first version that worked with a prefix.
Property
It can be clipped:
This is some long text that will not fit in the box
This is some long text that will not fit in the box
<!DOCTYPE html>
<html>
<head>
<style>
p.test1 {
white-space: nowrap;
width: 200px;
text-overflow: clip;
p.test2 {
white-space: nowrap;
width: 200px;
overflow: hidden;
text-overflow: ellipsis;
</style>
</head>
<body>
<p>The following two paragraphs contains a long text that will not fit in the
box.</p>
<p>text-overflow: clip:</p>
<p class="test1">This is some long text that will not fit in the box</p>
<p>text-overflow: ellipsis:</p>
<p class="test2">This is some long text that will not fit in the box</p>
</body>
</html>
The following example shows how you can display the overflowed content
when hovering over the element:
<!DOCTYPE html>
<html>
<head>
<style>
div.test {
white-space: nowrap;
width: 200px;
overflow: hidden;
div.test:hover {
text-overflow: inherit;
overflow: visible;
</style>
</head>
<body>
<p>Hover over the two divs below, to see the entire text.</p>
<br>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
p.test {
width: 11em;
word-wrap: break-word;
</style>
</head>
<body>
</body>
</html>
CSS3 Word Breaking
The CSS3 word-break property specifies line breaking rules.
<!DOCTYPE html>
<html>
<head>
<style>
p.test1 {
width: 140px;
word-break: keep-all;
p.test2 {
width: 140px;
word-break: break-all;
</style>
</head>
<body>
<p class="test2">This paragraph contains some text. The lines will break
at any character.</p>
</body>
</html>
CSS3 Web Fonts
Using The Font You Want
In the CSS3 @font-face rule you must first define a name for the font (e.g.
myFirstFont), and then point to the font file.
Tip: Always use lowercase letters for the font URL. Uppercase letters can give
unexpected results in IE.
To use the font for an HTML element, refer to the name of the font
(myFirstFont) through the font-family property:
<!DOCTYPE html>
<html>
<head>
<style>
@font-face {
font-family: myFirstFont;
src: url(sansation_light.woff);
div {
font-family: myFirstFont;
</style>
</head>
<body>
<div>
With CSS3, websites can finally use fonts other than the pre-selected "web-
safe" fonts.
</div>
</body>
</html>
With CSS3, websites can finally use fonts other than the pre-selected "web-safe"
fonts.
<!DOCTYPE html>
<html>
<head>
<style>
@font-face {
font-family: myFirstFont;
src: url(sansation_light.woff);
@font-face {
font-family: myFirstFont;
src: url(sansation_bold.woff);
font-weight: bold;
div {
font-family: myFirstFont;
</style>
</head>
<body>
<div>
With CSS3, websites can <b>finally</b> use fonts other than the pre-selected
"web-safe" fonts.
</div>
</body>
</html>
With CSS3, websites can finally use fonts other than the pre-selected "web-safe"
fonts.
CSS3 2D Transforms
CSS3 2D Transforms
In this chapter you will learn about the following 2D transformation methods:
translate()
rotate()
scale()
skewX()
skewY()
matrix()
The following example moves the <div> element 50 pixels to the right, and 100
pixels down from its current position:
<!DOCTYPE html>
<html>
<head>
<style>
div {
width: 300px;
height: 100px;
background-color: yellow;
-ms-transform: translate(50px,100px); /* IE 9 */
}
</style>
</head>
<body>
<div>
The translate() method moves an element from its current position. This div
element is moved 50 pixels to the right, and 100 pixels down from its current
position.
</div>
</body>
</html>
The following example rotates the <div> element clockwise with 20 degrees:
<!DOCTYPE html>
<html>
<head>
<style>
div {
width: 300px;
height: 100px;
background-color: yellow;
div#myDiv {
-ms-transform: rotate(20deg); /* IE 9 */
</style>
</head>
<body>
<div>
</div>
<div id="myDiv">
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
div {
width: 300px;
height: 100px;
background-color: yellow;
div#myDiv {
-ms-transform: rotate(-20deg); /* IE 9 */
</style>
</head>
<body>
<div>
</div>
<div id="myDiv">
</div>
</body>
</html>
The scale() Method
The scale() method increases or decreases the size of an element (according
to the parameters given for the width and height).
The following example increases the <div> element to be two times of its
original width, and three times of its original height:
<!DOCTYPE html>
<html>
<head>
<style>
div {
margin: 150px;
width: 200px;
height: 100px;
background-color: yellow;
</style>
</head>
<body>
<div>
This div element is two times of its original width, and three times of its original
height.
</div>
</body>
</html>
The following example decreases the <div> element to be half of its original
width and height:
<!DOCTYPE html>
<html>
<head>
<style>
div {
margin: 150px;
width: 200px;
height: 100px;
background-color: yellow;
border: 1px solid black;
-ms-transform: scale(0.5,0.5); /* IE 9 */
</style>
</head>
<body>
<div>
This div element is decreased to be half of its original width and height.
</div>
</body>
</html>
The skewX() Method
The skewX() method skews an element along the X-axis by the given angle.
The following example skews the <div> element 20 degrees along the X-axis:
<!DOCTYPE html>
<html>
<head>
<style>
div {
width: 300px;
height: 100px;
background-color: yellow;
div#myDiv {
-ms-transform: skewX(20deg); /* IE 9 */
</style>
</head>
<body>
<p>The skewX() method skews an element along the X-axis by the given
angle.</p>
<div>
</div>
<div id="myDiv">
</div>
</body>
</html>
The skewY() Method
The skewY() method skews an element along the Y-axis by the given angle.
The following example skews the <div> element 20 degrees along the Y-axis:
<!DOCTYPE html>
<html>
<head>
<style>
div {
width: 300px;
height: 100px;
background-color: yellow;
}
div#myDiv {
-ms-transform: skewY(20deg); /* IE 9 */
</style>
</head>
<body>
<p>The skewY() method skews an element along the Y-axis by the given
angle.</p>
<div>
</div>
<div id="myDiv">
</div>
</body>
</html>
The skew() Method
The skew() method skews an element along the X and Y-axis by the given
angles.
The following example skews the <div> element 20 degrees along the X-axis,
and 10 degrees along the Y-axis:
<!DOCTYPE html>
<html>
<head>
<style>
div {
width: 300px;
height: 100px;
background-color: yellow;
border: 1px solid black;
div#myDiv {
-ms-transform: skew(20deg,10deg); /* IE 9 */
</style>
</head>
<body>
<div>
</div>
<div id="myDiv">
This div element is skewed 20 degrees along the X-axis, and 10 degrees along
the Y-axis.
</div>
</body>
</html>
The matrix() Method
The matrix() method combines all the 2D transform methods into one.
<!DOCTYPE html>
<html>
<head>
<style>
div {
width: 300px;
height: 100px;
background-color: yellow;
div#myDiv1 {
div#myDiv2 {
</style>
</head>
<body>
<p>The matrix() method combines all the 2D transform methods into one.</p>
<div>
</div>
<div id="myDiv1">
</div>
<div id="myDiv2">
</div>
</body>
</html>
CSS3 3D Transforms
CSS3 3D Transforms
In this chapter you will learn about the following 3D transformation methods:
rotateX()
rotateY()
rotateZ()
<!DOCTYPE html>
<html>
<head>
<style>
div {
width: 300px;
height: 100px;
background-color: yellow;
div#myDiv {
</style>
</head>
<body>
<div>
</div>
<div id="myDiv">
The rotateX() method rotates an element around its X-axis at a given degree.
This div element is rotated 150 degrees.
</div>
</body>
</html>
The rotateY() Method
The rotateY() method rotates an element around its Y-axis at a given degree:
<!DOCTYPE html>
<html>
<head>
<style>
div {
width: 300px;
height: 100px;
background-color: yellow;
div#myDiv {
-webkit-transform: rotateY(150deg); /* Safari */
</style>
</head>
<body>
<div>
</div>
<div id="myDiv">
The rotateY() method rotates an element around its Y-axis at a given degree.
This div element is rotated 150 degrees.
</div>
</body>
</html>
The rotateZ() Method
The rotateZ() method rotates an element around its Z-axis at a given degree:
<!DOCTYPE html>
<html>
<head>
<style>
div {
width: 300px;
height: 100px;
background-color: yellow;
div#myDiv {
-webkit-transform: rotateZ(90deg); /* Safari */
</style>
</head>
<body>
<div>
</div>
<div id="myDiv">
The rotateZ() method rotates an element around its Z-axis at a given degree.
This div element is rotated 90 degrees.
</div>
</body>
</html>
CSS3 Transitions
CSS3 Transitions
CSS3 transitions allows you to change property values smoothly (from one
value to another), over a given duration.
Note: If the duration part is not specified, the transition will have no effect,
because the default value is 0.
The following example shows a 100px * 100px red <div> element. The <div>
element has also specified a transition effect for the width property, with a
duration of 2 seconds:
<!DOCTYPE html>
<html>
<head>
<style>
div {
width: 100px;
height: 100px;
background: red;
div:hover {
width: 300px;
</style>
</head>
<body>
<div></div>
<p>Hover over the div element above, to see the transition effect.</p>
</body>
</html>
ease - specifies a transition effect with a slow start, then fast, then end
slowly (this is default)
linear - specifies a transition effect with the same speed from start to
end
ease-in - specifies a transition effect with a slow start
ease-out - specifies a transition effect with a slow end
ease-in-out - specifies a transition effect with a slow start and end
cubic-bezier(n,n,n,n) - lets you define your own values in a cubic-
bezier function
The following example shows the some of the different speed curves that can be
used:
<!DOCTYPE html>
<html>
<head>
<style>
div {
width: 100px;
height: 100px;
background: red;
/* Standard syntax */
div:hover {
width: 300px;
</style>
</head>
<body>
<div id="div1">linear</div><br>
<div id="div2">ease</div><br>
<div id="div3">ease-in</div><br>
<div id="div4">ease-out</div><br>
<div id="div5">ease-in-out</div><br>
<p>Hover over the div elements above, to see the different speed curves.</p>
</body>
</html>
Delay the Transition Effect
The transition-delay property specifies a delay (in seconds) for the transition
effect.
<html>
<head>
<style>
div {
width: 100px;
height: 100px;
background: red;
transition-delay: 1s;
div:hover {
width: 300px;
</style>
</head>
<body>
<div></div>
<p>Hover over the div element above, to see the transition effect.</p>
</body>
</html>
Transition + Transformation
The following example also adds a transformation to the transition effect:
<!DOCTYPE html>
<html>
<head>
<style>
div {
width: 100px;
height: 100px;
background: red;
div:hover {
width: 300px;
height: 300px;
transform: rotate(180deg);
</style>
</head>
<body>
<div></div>
<p>Hover over the div element above, to see the transition effect.</p>
</body>
</html>
CSS3 Animations
CSS3 Animations
CSS3 animations allows animation of most HTML elements without using
JavaScript or Flash!
You can change as many CSS properties you want, as many times you want.
To use CSS3 animation, you must first specify some keyframes for the
animation.
Keyframes hold what styles the element will have at certain times.
<!DOCTYPE html>
<html>
<head>
<style>
div {
width: 100px;
height: 100px;
background-color: red;
animation-name: example;
animation-duration: 4s;
@-webkit-keyframes example {
to {background-color: yellow;}
/* Standard syntax */
@keyframes example {
</style>
</head>
<body>
<div></div>
</body>
</html>
Note: If the animation-duration property is not specified, the animation will
have no effect, because the default value is 0.
In the example above we have specified when the style will change by using the
keywords "from" and "to" (which represents 0% (start) and 100% (complete)).
It is also possible to use percent. By using percent, you can add as many style
changes as you like.
The following example will change the background-color of the <div> element
when the animation is 25% complete, 50% complete, and again when the
animation is 100% complete:
<!DOCTYPE html>
<html>
<head>
<style>
div {
width: 100px;
height: 100px;
background-color: red;
animation-name: example;
animation-duration: 4s;
@-webkit-keyframes example {
0% {background-color: red;}
/* Standard syntax */
@keyframes example {
0% {background-color: red;}
</style>
</head>
<body>
<div></div>
</body>
</html>
The following example will change both the background-color and the
position of the <div> element when the animation is 25% complete, 50%
complete, and again when the animation is 100% complete:
<!DOCTYPE html>
<html>
<head>
<style>
div {
width: 100px;
height: 100px;
background-color: red;
position: relative;
animation-name: example;
animation-duration: 4s;
}
@-webkit-keyframes example {
/* Standard syntax */
@keyframes example {
</style>
</head>
<body>
</body>
</html>
Delay an Animation
The animation-delay property specifies a delay for the start of an animation.
The following example has a 2 seconds delay before starting the animation:
<!DOCTYPE html>
<html>
<head>
<style>
div {
width: 100px;
height: 100px;
background-color: red;
position: relative;
animation-name: example;
animation-duration: 4s;
animation-delay: 2s;
@-webkit-keyframes example {
/* Standard syntax */
@keyframes example {
</style>
</head>
<body>
<div></div>
</body>
</html>
The following example will run the animation 3 times before it stops:
<!DOCTYPE html>
<html>
<head>
<style>
div {
width: 100px;
height: 100px;
background-color: red;
position: relative;
animation-name: example;
animation-duration: 4s;
animation-iteration-count: 3;
@-webkit-keyframes example {
}
/* Standard syntax */
@keyframes example {
</style>
</head>
<body>
<div></div>
</body>
</html>
Run Animation in Reverse Direction or
Alternate Cycles
The animation-direction property is used to let an animation run in reverse
direction or alternate cycles.
<!DOCTYPE html>
<html>
<head>
<style>
div {
width: 100px;
height: 100px;
background-color: red;
position: relative;
animation-name: example;
animation-duration: 4s;
animation-iteration-count: 3;
animation-direction: reverse;
@-webkit-keyframes example {
/* Standard syntax */
@keyframes example {
}
</style>
</head>
<body>
<div></div>
</body>
</html>
The following example shows the some of the different speed curves that can be
used:
<!DOCTYPE html>
<html>
<head>
<style>
div {
width: 100px;
height: 50px;
background-color: red;
font-weight: bold;
position: relative;
/* Standard syntax */
@-webkit-keyframes mymove {
to {left: 300px;}
/* Standard syntax */
@keyframes mymove {
to {left: 300px;}
</style>
</head>
<body>
<p><strong>Note:</strong> The animation-timing-funtion property is not
supported in Internet Explorer 9 and earlier versions.</p>
<div id="div1">linear</div>
<div id="div2">ease</div>
<div id="div3">ease-in</div>
<div id="div4">ease-out</div>
<div id="div5">ease-in-out</div>
</body>
</html>
CSS Images
Learn how to style images using CSS.
Rounded Images
Use the border-radius property to create rounded images:
<!DOCTYPE html>
<html>
<head>
<style>
img {
border-radius: 8px;
</style>
</head>
<body>
<h2>Rounded Images</h2>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
img {
border-radius: 50%;
</style>
</head>
<body>
<h2>Rounded Images</h2>
</body>
</html>
Thumbnail Images
Use the border property to create thumbnail images.
<!DOCTYPE html>
<html>
<head>
<style>
img {
border-radius: 4px;
padding: 5px;
width: 150px;
</style>
</head>
<body>
<h2>Thumbnail Images</h2>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
img {
border-radius: 4px;
padding: 5px;
width: 150px;
img:hover {
</style>
</head>
<body>
<h2>Thumbnail Image as Link</h2>
</a>
</body>
</html>
Responsive Images
Responsive images will automatically adjust to fit the size of the screen.
Resize the browser window to see the effect:
<!DOCTYPE html>
<html>
<head>
<style>
img {
max-width: 100%;
height: auto;
</style>
</head>
<body>
<h2>Responsive Images</h2>
</body>
</html>
Center an Image
To center an image within the page, use margin: auto; and make it into
a block element:
<!DOCTYPE html>
<html>
<head>
<style>
img {
display: block;
margin: auto;
</style>
</head>
<body>
</body>
</html>
<html>
<head>
<style>
body {margin:25px;}
div.polaroid {
width: 80%;
background-color: white;
margin-bottom: 25px;
div.container {
text-align: center;
</style>
</head>
<body>
<div class="polaroid">
<div class="container">
</div>
</div>
<div class="polaroid">
<div class="container">
</div>
</div>
</body>
</html>
Transparent Image
The opacity property can take a value from 0.0 - 1.0. The lower value, the
more transparent:
<!DOCTYPE html>
<html>
<head>
<style>
img {
opacity: 0.5;
}
</style>
</head>
<body>
<h1>Image Transparency</h1>
</body>
</html>
Image Text
How to position text in an image:
<!DOCTYPE html>
<html>
<head>
<style>
.container {
position: relative;
.topleft {
position: absolute;
top: 8px;
left: 16px;
font-size: 18px;
img {
width: 100%;
height: auto;
opacity: 0.3;
</style>
</head>
<body>
<h2>Image Text</h2>
<p>Add some text to an image in the top left corner:</p>
<div class="container">
</div>
</body>
</html>
For topright
.topright {
position: absolute;
top: 8px;
right: 16px;
font-size: 18px;
}
position: absolute;
bottom: 8px;
left: 16px;
font-size: 18px;
Bottom right
.bottomright {
position: absolute;
bottom: 8px;
right: 16px;
font-size: 18px;
}
For centered
.bottomright {
position: absolute;
bottom: 8px;
right: 16px;
font-size: 18px;
Image Filters
The CSS filter property adds visual effects (like blur and saturation) to an
element.
<!DOCTYPE html>
<html>
<head>
<style>
img {
width: 33%;
height: auto;
float: left;
max-width: 235px;
}
.blur {-webkit-filter: blur(4px);filter: blur(4px);}
</style>
</head>
<body>
</body>
</html>
Image Hover Overlay
Create an overlay effect on hover:
Example
Fade in text:
<!DOCTYPE html>
<html>
<head>
<style>
.container {
position: relative;
width: 50%;
.image {
display: block;
width: 100%;
height: auto;
.overlay {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
height: 100%;
width: 100%;
opacity: 0;
background-color: #008CBA;
.container:hover .overlay {
opacity: 1;
.text {
color: white;
font-size: 20px;
position: absolute;
top: 50%;
left: 50%;
</style>
</head>
<body>
<h2>Fade in Overlay</h2>
<div class="container">
<div class="overlay">
</div>
</div>
</body>
</html>
Example
Fade in a box:
<!DOCTYPE html>
<html>
<head>
<style>
.container {
position: relative;
width: 50%;
}
.image {
opacity: 1;
display: block;
width: 100%;
height: auto;
backface-visibility: hidden;
.middle {
opacity: 0;
position: absolute;
top: 50%;
left: 50%;
.container:hover .image {
opacity: 0.3;
}
.container:hover .middle {
opacity: 1;
.text {
background-color: #4CAF50;
color: white;
font-size: 16px;
</style>
</head>
<body>
<h2>Fade in a Box</h2>
<div class="container">
<div class="middle">
</div>
</div>
</body>
</html>
Flip an Image
Move your mouse over the image:
<!DOCTYPE html>
<html>
<head>
<style>
img:hover {
-webkit-transform: scaleX(-1);
transform: scaleX(-1);
}
</style>
</head>
<body>
<h2>Flip an Image</h2>
</body>
</html>
Responsive Image Gallery
CSS can be used to create image galleries. This example use media queries to
re-arrange the images on different screen sizes. Resize the browser window to
see the effect:
<!DOCTYPE html>
<html>
<head>
<style>
div.gallery {
div.gallery:hover {
div.gallery img {
width: 100%;
height: auto;
div.desc {
padding: 15px;
text-align: center;
}
*{
box-sizing: border-box;
.responsive {
padding: 0 6px;
float: left;
width: 24.99999%;
.responsive {
width: 49.99999%;
margin: 6px 0;
.responsive {
width: 100%;
.clearfix:after {
content: "";
display: table;
clear: both;
</style>
</head>
<body>
<div class="responsive">
<div class="gallery">
</a>
</div>
</div>
<div class="responsive">
<div class="gallery">
</a>
</div>
</div>
<div class="responsive">
<div class="gallery">
</a>
</div>
</div>
<div class="responsive">
<div class="gallery">
</a>
</div>
</div>
<div class="clearfix"></div>
<div style="padding:6px;">
<p>You will learn more about media queries and responsive web design later
in our CSS Tutorial.</p>
</div>
</body>
</html>
Image Modal (Advanced)
This is an example to demonstrate how CSS and JavaScript can work together.
First, use CSS to create a modal window (dialog box), and hide it by default.
Then, use a JavaScript to show the modal window and to display the image
inside the modal, when a user clicks on the image:
<!DOCTYPE html>
<html>
<head>
<style>
#myImg {
border-radius: 5px;
cursor: pointer;
transition: 0.3s;
.modal {
left: 0;
top: 0;
.modal-content {
margin: auto;
display: block;
width: 80%;
max-width: 700px;
#caption {
margin: auto;
display: block;
width: 80%;
max-width: 700px;
text-align: center;
color: #ccc;
padding: 10px 0;
height: 150px;
/* Add Animation */
.modal-content, #caption {
-webkit-animation-name: zoom;
-webkit-animation-duration: 0.6s;
animation-name: zoom;
animation-duration: 0.6s;
@-webkit-keyframes zoom {
to {-webkit-transform: scale(1)}
@keyframes zoom {
to {transform: scale(1)}
.close {
position: absolute;
top: 15px;
right: 35px;
color: #f1f1f1;
font-size: 40px;
font-weight: bold;
transition: 0.3s;
.close:hover,
.close:focus {
color: #bbb;
text-decoration: none;
cursor: pointer;
.modal-content {
width: 100%;
</style>
</head>
<body>
<h2>Image Modal</h2>
<p>In this example, we use CSS to create a modal (dialog box) that is hidden
by default.</p>
<p>We use JavaScript to trigger the modal and to display the current image
inside the modal when it is clicked on. Also note that we use the value from the
image's "alt" attribute as an image caption text inside the modal.</p>
<p>Don't worry if you do not understand the code right away. When you are
done with CSS, go to our JavaScript Tutorial to learn more.</p>
<span class="close">×</span>
<div id="caption"></div>
</div>
<script>
// Get the image and insert it inside the modal - use its "alt" text as a caption
img.onclick = function(){
modal.style.display = "block";
modalImg.src = this.src;
captionText.innerHTML = this.alt;
span.onclick = function() {
modal.style.display = "none";
</script>
</body>
</html>
Image Modal
In this example, we use CSS to create a modal (dialog box) that is hidden by default.
We use JavaScript to trigger the modal and to display the current image inside the
modal when it is clicked on. Also note that we use the value from the image's "alt"
attribute as an image caption text inside the modal.
Don't worry if you do not understand the code right away. When you are done with
CSS, go to our JavaScript Tutorial to learn more.
CSS The object-fit Property
All Values of The CSS object-fit Property
The object-fit property can have the following values:
fill - This is default. The replaced content is sized to fill the element's
content box. If necessary, the object will be stretched or squished to fit
contain - The replaced content is scaled to maintain its aspect ratio while
fitting within the element's content box
cover - The replaced content is sized to maintain its aspect ratio while
filling the element's entire content box. The object will be clipped to fit
none - The replaced content is not resized
scale-down - The content is sized as if none or contain were specified
(would result in a smaller concrete object size)
<!DOCTYPE html>
<html>
<head>
<style>
</style>
</head>
<body>
<h2>No object-fit:</h2>
<h2>object-fit: contain:</h2>
<h2>object-fit: cover:</h2>
<h2>object-fit: scale-down:</h2>
</body>
</html>
object-fit: contain:
object-fit: cover:
object-fit: scale-down:
object-fit: none:
CSS Buttons
Learn how to style buttons using CSS.
<html>
<head>
<style>
.button {
background-color: #4CAF50;
border: none;
color: white;
text-decoration: none;
display: inline-block;
font-size: 16px;
cursor: pointer;
</style>
</head>
<body>
<h2>CSS Buttons</h2>
<button>Default Button</button>
<button class="button">Button</button>
</body>
</html>
CSS Buttons
Button Colors
<!DOCTYPE html>
<html>
<head>
<style>
.button {
border: none;
color: white;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
cursor: pointer;
}
.button2 {background-color: #008CBA;} /* Blue */
</style>
</head>
<body>
<h2>Button Colors</h2>
<button class="button">Green</button>
</body>
</html>
Button Sizes
<!DOCTYPE html>
<html>
<head>
<style>
.button {
border: none;
color: white;
text-align: center;
text-decoration: none;
display: inline-block;
cursor: pointer;
</style>
</head>
<body>
<h2>Button Sizes</h2>
</body>
</html>
Rounded Buttons
<!DOCTYPE html>
<html>
<head>
<style>
.button {
border: none;
color: white;
padding: 20px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
cursor: pointer;
</style>
</head>
<body>
<h2>Rounded Buttons</h2>
</body>
</html>
<html>
<head>
<style>
.button {
border: none;
color: white;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
cursor: pointer;
.button1 {
background-color: white;
color: black;
.button2 {
background-color: white;
color: black;
.button3 {
background-color: white;
color: black;
.button4 {
background-color: white;
color: black;
.button5 {
background-color: white;
color: black;
</style>
</head>
<body>
</html>
Hoverable Buttons
<!DOCTYPE html>
<html>
<head>
<style>
.button {
border: none;
color: white;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 4px 2px;
transition-duration: 0.4s;
cursor: pointer;
.button1 {
background-color: white;
color: black;
.button1:hover {
background-color: #4CAF50;
color: white;
.button2 {
background-color: white;
color: black;
.button2:hover {
background-color: #008CBA;
color: white;
.button3 {
background-color: white;
color: black;
.button3:hover {
background-color: #f44336;
color: white;
.button4 {
background-color: white;
color: black;
.button5 {
background-color: white;
color: black;
border: 2px solid #555555;
.button5:hover {
background-color: #555555;
color: white;
</style>
</head>
<body>
<h2>Hoverable Buttons</h2>
<p>Use the :hover selector to change the style of the button when you move
the mouse over it.</p>
</body>
</html>
Shadow Buttons
<!DOCTYPE html>
<html>
<head>
<style>
.button {
border: none;
color: white;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
cursor: pointer;
-webkit-transition-duration: 0.4s; /* Safari */
transition-duration: 0.4s;
.button1 {
.button2:hover {
</style>
</head>
<body>
<h2>Shadow Buttons</h2>
</body>
</html>
Disabled Buttons
<!DOCTYPE html>
<html>
<head>
<style>
.button {
border: none;
color: white;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
cursor: pointer;
}
.disabled {
opacity: 0.6;
cursor: not-allowed;
</style>
</head>
<body>
<h2>Disabled Buttons</h2>
<p>Use the opacity property to add some transparency to the button (make it
look disabled):</p>
</body>
</html>
Button Width
<!DOCTYPE html>
<html>
<head>
<style>
.button {
border: none;
color: white;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
cursor: pointer;
</style>
</head>
<body>
<h2>Button Width</h2>
<p><strong>Tip:</strong> Use pixels if you want to set a fixed width and use
percent for responsive buttons (e.g. 50% of its parent element). Resize the
browser window to see the effect.</p>
</body>
</html>
Button Groups
<!DOCTYPE html>
<html>
<head>
<style>
.btn-group .button {
border: none;
color: white;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
cursor: pointer;
float: left;
.btn-group .button:hover {
background-color: #3e8e41;
</style>
</head>
<body>
<h2>Button Groups</h2>
<div class="btn-group">
<button class="button">Button</button>
<button class="button">Button</button>
<button class="button">Button</button>
<button class="button">Button</button>
</div>
</body>
</html>
Bordered Button Groups
<!DOCTYPE html>
<html>
<head>
<style>
.btn-group .button {
color: white;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
cursor: pointer;
float: left;
.btn-group .button:not(:last-child) {
.btn-group .button:hover {
background-color: #3e8e41;
</style>
</head>
<body>
<div class="btn-group">
<button class="button">Button</button>
<button class="button">Button</button>
<button class="button">Button</button>
<button class="button">Button</button>
</div>
</body>
</html>
Vertical Button Groups
<!DOCTYPE html>
<html>
<head>
<style>
.btn-group .button {
color: white;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
cursor: pointer;
width: 150px;
display: block;
.btn-group .button:not(:last-child) {
.btn-group .button:hover {
background-color: #3e8e41;
</style>
</head>
<body>
<div class="btn-group">
<button class="button">Button</button>
<button class="button">Button</button>
<button class="button">Button</button>
<button class="button">Button</button>
</div>
</body>
</html>
Animated Buttons
<!DOCTYPE html>
<html>
<head>
<style>
.button {
display: inline-block;
border-radius: 4px;
background-color: #f4511e;
border: none;
color: #FFFFFF;
text-align: center;
font-size: 28px;
padding: 20px;
width: 200px;
transition: all 0.5s;
cursor: pointer;
margin: 5px;
.button span {
cursor: pointer;
display: inline-block;
position: relative;
transition: 0.5s;
.button span:after {
content: '\00bb';
position: absolute;
opacity: 0;
top: 0;
right: -20px;
transition: 0.5s;
.button:hover span {
padding-right: 25px;
}
.button:hover span:after {
opacity: 1;
right: 0;
</style>
</head>
<body>
<h2>Animated Button</h2>
</body>
</html>
Example
Add a "pressed" effect on click:
<!DOCTYPE html>
<html>
<head>
<style>
.button {
display: inline-block;
font-size: 24px;
cursor: pointer;
text-align: center;
text-decoration: none;
outline: none;
color: #fff;
background-color: #4CAF50;
border: none;
border-radius: 15px;
.button:active {
background-color: #3e8e41;
transform: translateY(4px);
}
</style>
</head>
<body>
</body>
</html>
Example
Add a "ripple" effect on click:
<!DOCTYPE html>
<html>
<head>
<style>
.button {
position: relative;
background-color: #4CAF50;
border: none;
font-size: 28px;
color: #FFFFFF;
padding: 20px;
width: 200px;
text-align: center;
transition-duration: 0.4s;
text-decoration: none;
overflow: hidden;
cursor: pointer;
.button:after {
content: "";
background: #f1f1f1;
display: block;
position: absolute;
padding-top: 300%;
padding-left: 350%;
margin-top: -120%;
opacity: 0;
.button:active:after {
padding: 0;
margin: 0;
opacity: 1;
transition: 0s
</style>
</head>
<body>
</body>
</html>
CSS Pagination Examples
Simple Pagination
If you have a website with lots of pages, you may wish to add some sort of
pagination to each page:
<!DOCTYPE html>
<html>
<head>
<style>
.pagination {
display: inline-block;
.pagination a {
color: black;
float: left;
text-decoration: none;
}
</style>
</head>
<body>
<h2>Simple Pagination</h2>
<div class="pagination">
<a href="#">«</a>
<a href="#">1</a>
<a href="#">2</a>
<a href="#">3</a>
<a href="#">4</a>
<a href="#">5</a>
<a href="#">6</a>
<a href="#">»</a>
</div>
</body>
</html>
Active and Hoverable Pagination
<!DOCTYPE html>
<html>
<head>
<style>
.pagination {
display: inline-block;
.pagination a {
color: black;
float: left;
text-decoration: none;
.pagination a.active {
background-color: #4CAF50;
color: white;
</style>
</head>
<body>
<div class="pagination">
<a href="#">«</a>
<a href="#">1</a>
<a href="#">3</a>
<a href="#">4</a>
<a href="#">5</a>
<a href="#">6</a>
<a href="#">»</a>
</div>
</body>
</html>
Rounded Active and Hoverable Buttons
<!DOCTYPE html>
<html>
<head>
<style>
.pagination {
display: inline-block;
.pagination a {
color: black;
float: left;
text-decoration: none;
.pagination a.active {
background-color: #4CAF50;
color: white;
border-radius: 5px;
.pagination a:hover:not(.active) {
background-color: #ddd;
border-radius: 5px;
</style>
</head>
<body>
<div class="pagination">
<a href="#">«</a>
<a href="#">1</a>
<a href="#">3</a>
<a href="#">4</a>
<a href="#">5</a>
<a href="#">6</a>
<a href="#">»</a>
</div>
</body>
</html>
Hoverable Transition Effect
<!DOCTYPE html>
<html>
<head>
<style>
.pagination {
display: inline-block;
.pagination a {
color: black;
float: left;
text-decoration: none;
.pagination a.active {
background-color: #4CAF50;
color: white;
}
</style>
</head>
<body>
<div class="pagination">
<a href="#">«</a>
<a href="#">1</a>
<a href="#">3</a>
<a href="#">4</a>
<a href="#">5</a>
<a href="#">6</a>
<a href="#">»</a>
</div>
</body>
</html>
Bordered Pagination
<!DOCTYPE html>
<html>
<head>
<style>
.pagination {
display: inline-block;
.pagination a {
color: black;
float: left;
text-decoration: none;
}
.pagination a.active {
background-color: #4CAF50;
color: white;
</style>
</head>
<body>
<div class="pagination">
<a href="#">«</a>
<a href="#">1</a>
<a href="#">3</a>
<a href="#">4</a>
<a href="#">5</a>
<a href="#">6</a>
<a href="#">»</a>
</div>
</body>
</html>
<html>
<head>
<style>
.pagination {
display: inline-block;
.pagination a {
color: black;
float: left;
text-decoration: none;
margin: 0 4px;
}
.pagination a.active {
background-color: #4CAF50;
color: white;
</style>
</head>
<body>
<div class="pagination">
<a href="#">«</a>
<a href="#">1</a>
<a href="#">3</a>
<a href="#">4</a>
<a href="#">5</a>
<a href="#">6</a>
<a href="#">»</a>
</div>
</body>
</html>
Pagination Size
<!DOCTYPE html>
<html>
<head>
<style>
.pagination {
display: inline-block;
.pagination a {
color: black;
float: left;
text-decoration: none;
font-size: 22px;
}
.pagination a.active {
background-color: #4CAF50;
color: white;
</style>
</head>
<body>
<h2>Pagination Size</h2>
<div class="pagination">
<a href="#">«</a>
<a href="#">1</a>
<a href="#">3</a>
<a href="#">4</a>
<a href="#">5</a>
<a href="#">6</a>
<a href="#">»</a>
</div>
</body>
</html>
Centered Pagination
<!DOCTYPE html>
<html>
<head>
<style>
.center {
text-align: center;
.pagination {
display: inline-block;
}
.pagination a {
color: black;
float: left;
text-decoration: none;
margin: 0 4px;
.pagination a.active {
background-color: #4CAF50;
color: white;
</style>
</head>
<body>
<h2>Centered Pagination</h2>
<div class="center">
<div class="pagination">
<a href="#">«</a>
<a href="#">1</a>
<a href="#">3</a>
<a href="#">4</a>
<a href="#">5</a>
<a href="#">6</a>
<a href="#">»</a>
</div>
</div>
</body>
</html>
CSS3 Multiple Columns
CSS3 Create Multiple Columns
The column-count property specifies the number of columns an element should
be divided into.
The following example will divide the text in the <div> element into 3 columns:
<!DOCTYPE html>
<html>
<head>
<style>
.newspaper {
-moz-column-count: 3; /* Firefox */
column-count: 3;
</style>
</head>
<body>
<div class="newspaper">
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam
nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat
volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper
suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum
iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum
dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim
qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla
facilisi. Nam liber tempor cum soluta nobis eleifend option congue nihil
imperdiet doming id quod mazim placerat facer possim assum.
</div>
</body>
</html>
Note: Internet Explorer 9, and earlier versions, does not support the column-count
property.
Lorem ipsum dolor sit consequat. Duis autem facilisi. Nam liber
amet, consectetuer vel eum iriure dolor in tempor cum soluta
adipiscing elit, sed diam hendrerit in vulputate nobis eleifend option
nonummy nibh euismod velit esse molestie congue nihil imperdiet
tincidunt ut laoreet consequat, vel illum doming id quod mazim
dolore magna aliquam dolore eu feugiat nulla placerat facer possim
erat volutpat. Ut wisi facilisis at vero eros et assum.
enim ad minim veniam, accumsan et iusto odio
quis nostrud exerci dignissim qui blandit
tation ullamcorper praesent luptatum zzril
suscipit lobortis nisl ut delenit augue duis
aliquip ex ea commodo dolore te feugait nulla
<!DOCTYPE html>
<html>
<head>
<style>
.newspaper {
-moz-column-count: 3; /* Firefox */
column-count: 3;
column-gap: 40px;
</style>
</head>
<body>
<div class="newspaper">
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam
nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat
volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper
suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum
iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum
dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim
qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla
facilisi. Nam liber tempor cum soluta nobis eleifend option congue nihil
imperdiet doming id quod mazim placerat facer possim assum.
</div>
</body>
Note: Internet Explorer 9, and earlier versions, does not support the column-gap
property.
<!DOCTYPE html>
<html>
<head>
<style>
.newspaper {
-moz-column-count: 3; /* Firefox */
column-count: 3;
column-gap: 40px;
-webkit-column-rule-style: solid; /* Chrome, Safari, Opera */
column-rule-style: solid;
</style>
</head>
<body>
<div class="newspaper">
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam
nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat
volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper
suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum
iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum
dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim
qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla
facilisi. Nam liber tempor cum soluta nobis eleifend option congue nihil
imperdiet doming id quod mazim placerat facer possim assum.
</div>
</body>
</html>
Note: Internet Explorer 9, and earlier versions, does not support the column-rule-style
property.
Lorem ipsum dolor sit adipiscing elit, sed diam tincidunt ut laoreet
amet, consectetuer nonummy nibh euismod dolore magna aliquam
erat volutpat. Ut wisi velit esse molestie facilisi. Nam liber
enim ad minim veniam, consequat, vel illum tempor cum soluta
quis nostrud exerci dolore eu feugiat nulla nobis eleifend option
tation ullamcorper facilisis at vero eros et congue nihil imperdiet
suscipit lobortis nisl ut accumsan et iusto odio doming id quod mazim
aliquip ex ea commodo dignissim qui blandit placerat facer possim
consequat. Duis autem praesent luptatum zzril assum.
vel eum iriure dolor in delenit augue duis
hendrerit in vulputate dolore te feugait nulla
<!DOCTYPE html>
<html>
<head>
<style>
.newspaper {
-moz-column-count: 3; /* Firefox */
column-count: 3;
column-gap: 40px;
column-rule-style: solid;
column-rule-width: 1px;
}
</style>
</head>
<body>
<div class="newspaper">
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam
nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat
volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper
suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum
iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum
dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim
qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla
facilisi. Nam liber tempor cum soluta nobis eleifend option congue nihil
imperdiet doming id quod mazim placerat facer possim assum.
</div>
</body>
</html>
Note: Internet Explorer 9, and earlier versions, does not support the column-rule-
width property.
<!DOCTYPE html>
<html>
<head>
<style>
.newspaper {
-moz-column-count: 3; /* Firefox */
column-count: 3;
column-gap: 40px;
column-rule-style: solid;
column-rule-width: 1px;
column-rule-color: lightblue;
}
</style>
</head>
<body>
<div class="newspaper">
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam
nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat
volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper
suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum
iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum
dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim
qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla
facilisi. Nam liber tempor cum soluta nobis eleifend option congue nihil
imperdiet doming id quod mazim placerat facer possim assum.
</div>
</body>
</html>
Note: Internet Explorer 9, and earlier versions, does not support the column-rule-color
property.
CSS3 Box Sizing
CSS3 Box Sizing
The CSS3 box-sizing property allows us to include the padding and border in
an element's total width and height.
The two <div> elements above end up with different sizes in the result
(because div2 has a padding specified):
<!DOCTYPE html>
<html>
<head>
<style>
.div1 {
width: 300px;
height: 100px;
.div2 {
width: 300px;
height: 100px;
padding: 50px;
</style>
</head>
<body>
<br>
</body>
</html>
With the CSS3 box-sizing Property
The CSS3 box-sizing property allows us to include the padding and border in
an element's total width and height.
<!DOCTYPE html>
<html>
<head>
<style>
.div1 {
width: 300px;
height: 100px;
box-sizing: border-box;
.div2 {
width: 300px;
height: 100px;
padding: 50px;
box-sizing: border-box;
}
</style>
</head>
<body>
<br>
<div class="div2">Hooray!</div>
</body>
</html>
The code below ensures that all elements are sized in this more intuitive way.
Many browsers already use box-sizing: border-box; for many form elements
(but not all - which is why inputs and text areas look different at width:
100%;).
<!DOCTYPE html>
<html>
<head>
<style>
body {
margin: 0;
*{
box-sizing: border-box;
input, textarea {
width: 100%;
</style>
</head>
<body>
<form action="/action_page.php">
First name:<br>
Comments:<br>
</textarea>
<br><br>
</form>
Notice that the width of input, textarea, and submit button will go outside of the
screen.</p>
</body>
</html>
CSS3 Media Queries
CSS3 Introduces Media Queries
Media queries in CSS3 extend the CSS2 media types idea: Instead of looking for
a type of device, they look at the capability of the device.
Using media queries are a popular technique for delivering a tailored style sheet
to tablets, iPhone, and Androids.
Media Queries Simple Examples
One way to use media queries is to have an alternate CSS section right inside
your style sheet.
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-color: pink;
body {
background-color: lightgreen;
</style>
</head>
<body>
<p>The media query will only apply if the media type is screen and the
viewport is 480px wide or wider.</p>
</body>
</html>
The following example shows a menu that will float to the left
of the page if the viewport is 480 pixels wide or wider (if the viewport is less
than 480 pixels, the menu will be on top of the content):
<!DOCTYPE html>
<html>
<head>
<style>
#leftsidebar {
float: none;
width: auto;
#menulist {
margin: 0;
padding: 0;
.menuitem {
background: #CDF0F6;
border-radius: 4px;
list-style-type: none;
margin: 4px;
padding: 2px;
</style>
</head>
<body>
<div class="wrapper">
<div id="leftsidebar">
<ul id="menulist">
</ul>
</div>
<div id="main">
<p>This example shows a menu that will float to the left of the page if the
viewport is 480 pixels wide or wider. If the viewport is less than 480 pixels, the
menu will be on top of the content.</p>
</div>
</div>
</body>
</html>
Width from 520 to 699px - Apply an email
icon to each link
When the browser's width is between 520 and 699px, we will apply an email
icon to each email link:
<!DOCTYPE html>
<html>
<head>
<style>
ul {
list-style-type: none;
}
ul li a {
color: green;
text-decoration: none;
padding: 3px;
display: block;
ul li a {
padding-left: 30px;
</style>
</head>
<body>
<ul>
<li><a data-email="[email protected]"
href="mailto:[email protected]">John Doe</a></li>
<li><a data-email="[email protected]"
href="mailto:[email protected]">Mary Moe</a></li>
<li><a data-email="[email protected]"
href="mailto:[email protected]">Amanda Panda</a></li>
</ul>
</body>
</html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<style>
*{
box-sizing: border-box;
.row::after {
content: "";
clear: both;
display: table;
[class*="col-"] {
float: left;
padding: 15px;
html {
.header {
background-color: #9933cc;
color: #ffffff;
padding: 15px;
.menu ul {
list-style-type: none;
margin: 0;
padding: 0;
.menu li {
padding: 8px;
margin-bottom: 7px;
background-color: #33b5e5;
color: #ffffff;
.menu li:hover {
background-color: #0099cc;
}
</style>
</head>
<body>
<div class="header">
<h1>Chania</h1>
</div>
<div class="row">
<ul>
<li>The Flight</li>
<li>The City</li>
<li>The Island</li>
<li>The Food</li>
</ul>
</div>
<div class="col-9">
<h1>The City</h1>
<p>Chania is the capital of the Chania region on the island of Crete. The
city can be divided in two parts, the old town and the modern city.</p>
<p>Resize the browser window to see how the content respond to the
resizing.</p>
</div>
</div>
</body>
</html>
Example
If the browser window is 500px or smaller, the background color will be
lightblue:
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-color: lightgreen;
body {
background-color: lightblue;
</style>
</head>
<body>
<p>Resize the browser window. When the width of this document is 500pixels
or less, the background-color is "lightblue", otherwise it is "lightgreen".</p>
</body>
</html>
Add a Breakpoint
Earlier in this tutorial we made a web page with rows and columns, and it was
responsive, but it did not look good on a small screen.
Media queries can help with that. We can add a breakpoint where certain parts
of the design will behave differently on each side of the breakpoint.
<!DOCTYPE html>
<html>
<head>
<style>
*{
box-sizing: border-box;
.row::after {
content: "";
clear: both;
display: block;
[class*="col-"] {
float: left;
padding: 15px;
html {
.header {
background-color: #9933cc;
color: #ffffff;
padding: 15px;
}
.menu ul {
list-style-type: none;
margin: 0;
padding: 0;
.menu li {
padding: 8px;
margin-bottom: 7px;
background-color: #33b5e5;
color: #ffffff;
.menu li:hover {
background-color: #0099cc;
.aside {
background-color: #33b5e5;
padding: 15px;
color: #ffffff;
text-align: center;
font-size: 14px;
.footer {
background-color: #0099cc;
color: #ffffff;
text-align: center;
font-size: 12px;
padding: 15px;
/* For desktop: */
[class*="col-"] {
width: 100%;
}
</style>
</head>
<body>
<div class="header">
<h1>Chania</h1>
</div>
<div class="row">
<ul>
<li>The Flight</li>
<li>The City</li>
<li>The Island</li>
<li>The Food</li>
</ul>
</div>
<div class="col-6">
<h1>The City</h1>
<p>Chania is the capital of the Chania region on the island of Crete. The city
can be divided in two parts, the old town and the modern city.</p>
</div>
<div class="col-3 right">
<div class="aside">
<h2>What?</h2>
<h2>Where?</h2>
<h2>How?</h2>
</div>
</div>
</div>
<div class="footer">
<p>Resize the browser window to see how the content respond to the
resizing.</p>
</div>
</body>
</html>
Responsive Web Design - Images
Using The width Property
If the width property is set to 100%, the image will be responsive and scale up
and down:
<!DOCTYPE html>
<html>
<head>
<style>
img {
width: 100%;
height: auto;
</style>
</head>
<body>
<p>Resize the browser window to see how the image will scale.</p>
</body>
</html>
Resize the browser window to see how the image will scale.
Background Images
Background images can also respond to resizing and scaling.
<!DOCTYPE html>
<html>
<head>
<style>
div {
width: 100%;
height: 400px;
background-image: url('img_flowers.jpg');
background-repeat: no-repeat;
background-size: contain;
</style>
</head>
<body>
<p>Resize the browser window to see the effect.</p>
<div></div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<style>
div {
width: 100%;
height: 400px;
background-image: url('img_flowers.jpg');
</style>
</head>
<body>
<div></div>
</body>
</html>
If the background-size property is set to "cover", the background image
will scale to cover the entire content area. Notice that the "cover" value keeps
the aspect ratio, and some part of the background image may be clipped:
<!DOCTYPE html>
<html>
<head>
<style>
div {
width: 100%;
height: 400px;
background-image: url('img_flowers.jpg');
background-size: cover;
</style>
</head>
<body>
<div></div>
</body>
</html>
Different Images for Different Devices
A large image can be perfect on a big computer screen, but useless on a small
device. Why load a large image when you have to scale it down anyway? To
reduce the load, or for any other reasons, you can use media queries to display
different images on different devices.
Here is one large image and one smaller image that will be displayed on
different devices:
<!DOCTYPE html>
<html>
<head>
body {
background-repeat: no-repeat;
background-image: url('img_smallflower.jpg');
body {
background-image: url('img_flowers.jpg');
</style>
</head>
<body>
</body>
</html>
Responsive Web Design - Videos
Using The width Property
If the width property is set to 100%, the video player will be responsive and
scale up and down:
<!DOCTYPE html>
<html>
<head>
<style>
video {
width: 100%;
height: auto;
</style>
</head>
<body>
</video>
<p>Resize the browser window to see how the size of the video player
will scale.</p>
</body>
</html>
Notice that in the example above, the video player can be scaled up to be larger
than its original size. A better solution, in many cases, will be to use the max-
width property instead.
<!DOCTYPE html>
<html>
<head>
<style>
video {
max-width: 100%;
height: auto;
</style>
</head>
<body>
</video>
<p>Resize the browser window to see how the size of the video player
will scale when the width is less than 400px.</p>
</body>
</html>
Add a Video to the Example Web Page
We want to add a video in our example web page. The video will be resized to
always take up all the available space:
<!DOCTYPE html>
<html>
<head>
<style>
*{
box-sizing: border-box;
video {
width: 100%;
height: auto;
.row:after {
content: "";
clear: both;
display: table;
[class*="col-"] {
float: left;
padding: 15px;
width: 100%;
html {
.header {
background-color: #9933cc;
color: #ffffff;
padding: 15px;
.menu ul {
list-style-type: none;
margin: 0;
padding: 0;
.menu li {
padding: 8px;
margin-bottom: 7px;
background-color: #33b5e5;
color: #ffffff;
.menu li:hover {
background-color: #0099cc;
.aside {
background-color: #33b5e5;
padding: 15px;
color: #ffffff;
text-align: center;
font-size: 14px;
.footer {
background-color: #0099cc;
color: #ffffff;
text-align: center;
font-size: 12px;
padding: 15px;
</style>
</head>
<body>
<div class="header">
<h1>Chania</h1>
</div>
<div class="row">
<ul>
<li>The Flight</li>
<li>The City</li>
<li>The Island</li>
<li>The Food</li>
</ul>
</div>
<h1>The City</h1>
<p>Chania is the capital of the Chania region on the island of Crete. The
city can be divided in two parts, the old town and the modern city.</p>
</video>
</div>
<div class="aside">
<h2>What?</h2>
<h2>Where?</h2>
<h2>How?</h2>
</div>
</div>
</div>
<div class="footer">
<p>Resize the browser window to see how the content respond to the
resizing.</p>
</div>
</body>
</html>
Chania
The Flight
The City
The Island
The Food
The City
Chania is the capital of the Chania region on the island of Crete. The city can be
divided in two parts, the old town and the modern city.
What?
Where?
Resize the browser window to see how the content respond to the resizing.
<html>
<title>W3.CSS</title>
<link rel="stylesheet"
href="https://www.w3schools.com/w3css/4/w3.css">
<body>
<h1>W3Schools Demo</h1>
</div>
<div class="w3-row-padding">
<div class="w3-third">
<h2>London</h2>
</div>
<div class="w3-third">
<h2>Paris</h2>
</div>
<div class="w3-third">
<h2>Tokyo</h2>
</div>
</div>
</body>
</html>
W3Schools Demo
Resize this responsive page!
London
London is the capital city of England.
It is the most populous city in the United Kingdom, with a metropolitan area of
over 13 million inhabitants.
Paris
Paris is the capital of France.
The Paris area is one of the largest population centers in Europe, with more
than 12 million inhabitants.
Tokyo
Tokyo is the capital of Japan.
It is the center of the Greater Tokyo Area, and the most populous metropolitan
area in the world.
<html>
<title>W3.CSS Template</title>
<meta charset="UTF-8">
<style>
h1,h2,h3,h4,h5,h6 {
letter-spacing: 5px;
}
</style>
<body>
<div class="w3-top">
</div>
</div>
</div>
</div>
</header>
</div>
</div>
<hr>
<h4>Bread Basket</h4>
<h4>Belgian Waffle</h4>
<h4>Scrambled eggs</h4>
</div>
</div>
</div>
<hr>
<h1>Contact</h1><br>
</form>
</div>
</div>
</footer>
</body>
</html>
CV Template
<!DOCTYPE html>
<html>
<title>W3.CSS Template</title>
<meta charset="UTF-8">
<style>
</style>
<body class="w3-light-grey">
<!-- Page Container -->
<div class="w3-row-padding">
<div class="w3-third">
<div class="w3-display-container">
<h2>Jane Doe</h2>
</div>
</div>
<div class="w3-container">
<hr>
<p class="w3-large"><b><i class="fa fa-asterisk fa-fw w3-margin-right w3-text-
teal"></i>Skills</b></p>
<p>Adobe Photoshop</p>
</div>
<p>Photography</p>
</div>
</div>
<p>Illustrator</p>
</div>
<p>Media</p>
</div>
<br>
<p class="w3-large w3-text-theme"><b><i class="fa fa-globe fa-fw w3-margin-right w3-text-
teal"></i>Languages</b></p>
<p>English</p>
</div>
<p>Spanish</p>
</div>
<p>German</p>
</div>
<br>
</div>
</div><br>
</div>
<div class="w3-twothird">
<div class="w3-container w3-card w3-white w3-margin-bottom">
<div class="w3-container">
<p>Lorem ipsum dolor sit amet. Praesentium magnam consectetur vel in deserunt aspernatur
est reprehenderit sunt hic. Nulla tempora soluta ea et odio, unde doloremque repellendus iure,
iste.</p>
<hr>
</div>
<div class="w3-container">
<hr>
</div>
<div class="w3-container">
</div>
</div>
<div class="w3-container w3-card w3-white">
<div class="w3-container">
<h5 class="w3-opacity"><b>W3Schools.com</b></h5>
<hr>
</div>
<div class="w3-container">
<p>Master Degree</p>
<hr>
</div>
<div class="w3-container">
<p>Bachelor Degree</p><br>
</div>
</div>
<!-- End Right Column -->
</div>
</div>
</div>
</footer>
</body>
</html>