Css
Css
Each declaration includes a CSS property name and a value, separated by a colon.
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:
Example
<!DOCTYPE html>
<html>
<head>
<style>
p{
color: red;
text-align: center;
</style>
</head>
<body>
<p>Hello World!</p>
</body>
</html>
CSS Selectors
CSS selectors are used to "find" (or select) HTML elements based on their element name, id,
class, attribute, and more.
Example
<!DOCTYPE html>
<html>
<head>
<style>
p{
text-align: center;
color: red;
}
</style>
</head>
<body>
</body>
</html>
The id Selector
The id selector uses the id attribute of an HTML element to select a specific element.
The id of an element should be unique within a page, so the id selector is used to select one
unique element!
To select an element with a specific id, write a hash (#) character, followed by the id of the
element.
The style rule below will be applied to the HTML element with id="para1":
Example
<!DOCTYPE html>
<html>
<head>
<style>
#para1 {
text-align: center;
color: red;
}
</style>
</head>
<body>
</body>
</html>
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:
Example
<!DOCTYPE html>
<html>
<head>
<style>
.center {
text-align: center;
color: red;
}
</style>
</head>
<body>
</body>
</html>
You can also specify that only specific HTML elements should be affected by a class.
In the example below, only <p> elements with class="center" will be center-aligned:
Example
<!DOCTYPE html>
<html>
<head>
<style>
p.center {
text-align: center;
color: red;
}
</style>
</head>
<body>
</body>
</html>
Grouping Selectors
If you have elements with the same style definitions, like this:
Example
<!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>
CSS Comments
Comments are used to explain the code, and may help when you edit the source code at a later
date.
Comments are ignored by browsers.
A CSS comment starts with /* and ends with */. Comments can also span multiple lines:
Example
<!DOCTYPE html>
<html>
<head>
<style>
p{
color: red;
/* This is a single-line comment */
text-align: center;
}
/* This is
a multi-line
comment */
</style>
</head>
<body>
<p>Hello World!</p>
<p>This paragraph is styled with CSS.</p>
<p>CSS comments are not shown in the output.</p>
</body>
</html>
Three Ways to Insert CSS
There are three ways of inserting a style sheet:
Each page must include a reference to the external style sheet file inside the <link> element. The
<link> element goes inside the <head> section:
Example
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
An external style sheet can be written in any text editor. The file should not contain any html
tags. The style sheet file must be saved with a .css extension.
Example
body {
background-color: lightblue;
}
h1 {
color: navy;
margin-left: 20px;
}
Internal styles are defined within the <style> element, inside the <head> section of an HTML
page:
Example
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-color: linen;
}
h1 {
color: maroon;
margin-left: 40px;
}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
Inline Styles
An inline style may be used to apply a unique style for a single element.
To use inline styles, add the style attribute to the relevant element. The style attribute can contain
any CSS property.
The example below shows how to change the color and the left margin of a <h1> element:
Example
<!DOCTYPE html>
<html>
<body>
</body>
</html>
CSS Colors
Color Names
Colors set by using color names:
Example
<!DOCTYPE html>
<html>
<body>
<h2 style="background-color:green">
Green background-color
</h2>
<h2 style="background-color:blue;color:white">
Blue background-color and white text color
</h2>
<h2 style="background-color:orange">
Orange background-color
</h2>
<h2 style="background-color:yellow">
Yellow background-color
</h2>
<h2 style="background-color:cyan">
Cyan background-color
</h2>
<h2 style="background-color:black;color:white">
Black background-color and white text color
</h2>
</body>
</html>
Each parameter (red, green, 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)
Example
<!DOCTYPE html>
<html>
<body>
Hexadecimal Colors
RGB values can also be specified using hexadecimal color values 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). Note: HEX values are case-insensitive: "#ff0000" is the
same as "FF0000".
Example
<!DOCTYPE html>
<html>
<body>
<h2 style="background-color:#FF0000">
Background-color set by using #FF0000
</h2>
<h2 style="background-color:#00FF00">
Background-color set by using #00FF00
</h2>
<h2 style="background-color:#0000FF">
Background-color set by using #0000FF
</h2>
<h2 style="background-color:#FFA500">
Background-color set by using #FFA500
</h2>
<h2 style="background-color:#FFFF00">
Background-color set by using #FFFF00
</h2>
<h2 style="background-color:#00FFFF">
Background-color set by using #00FFFF
</h2>
</body>
</html>
CSS Backgrounds
The CSS background properties are used to define the background effects for elements.
background-color
background-image
background-repeat
background-attachment
background-position
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>
<p>This page has a light blue background color!</p>
</body>
</html>
Background Image
The background-image property specifies an image to use as the background of an element.
Example
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-image: url("paper.gif");
}
</style>
</head>
<body>
<h1>Hello World!</h1>
Some images should be repeated only horizontally or vertically, or they will look strange, like
this:
Example
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-image: url("gradient_bg.png");
}
</style>
</head>
<body>
<h1>Hello World!</h1>
<p>Strange background image...</p>
</body>
</html>
If the image above is repeated only horizontally (background-repeat: repeat-x;), the background
will look better:
Example
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-image: url("gradient_bg.png");
background-repeat: repeat-x;
}
</style>
</head>
<body>
<h1>Hello World!</h1>
<p>Here, a backgound image is repeated only horizontally!</p>
</body>
</html>
Background Image - Set position and no-repeat
Showing the background image only once is also specified by the background-repeat property:
Example
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-image: url("img_tree.png");
background-repeat: no-repeat;
}
</style>
</head>
<body>
<h1>Hello World!</h1>
<p>W3Schools background image example.</p>
<p>The background image is only showing once, but it is disturbing the reader!</p>
</body>
</html>
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.
Example
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-image: url("img_tree.png");
background-repeat: no-repeat;
background-position: right top;
margin-right: 200px;
}
</style>
</head>
<body>
<h1>Hello World!</h1>
<p>W3Schools background no-repeat, set position example.</p>
<p>Now the background image is only shown once, and positioned away from the text.</p>
<p>In this example we have also added a margin on the right side, so the background image will
never disturb the text.</p>
</body>
</html>
Example
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-image: url("img_tree.png");
background-repeat: no-repeat;
background-position: right top;
margin-right: 200px;
background-attachment: fixed;
}
</style>
</head>
<body>
<h1>Hello World!</h1>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>If you do not see any scrollbars, try to resize the browser window.</p>
</body>
</html>
Example
<!DOCTYPE html>
<html>
<head>
<style>
body {
background: #ffffff url("img_tree.png") no-repeat right top;
margin-right: 200px;
}
</style>
</head>
<body>
</body>
</html>
CSS Borders
The CSS border properties allow you to specify the style, width, and color of an element's border.
Border Style
The border-style property specifies what kind of border to display.
Example
<!DOCTYPE html>
<html>
<head>
<style>
</style>
</head>
<body>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
div {
border: 1px solid black;
padding: 10px;
width: 200px;
height: 200px;
text-align: justify;
}
</style>
</head>
<body>
<p>The text-align: justify; value stretches the lines so that each line has equal width (like in
newspapers and magazines).</p>
<div>
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.'
</div>
</body>
</html>
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).
Example
<!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>
In CSS, there is also properties for specifying each of the borders (top, right, bottom, and left):
Example
<!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>
border-style: dotted;
o all four borders are dotted
The border-style property is used in the example above. However, it also works with border-
width and border-color.
To shorten the code, it is also possible to specify all the individual border properties in one
property.
The border property is a shorthand property for the following individual border properties:
border-width
border-style (required)
border-color
Example
<!DOCTYPE html>
<html>
<head>
<style>
p{
</style>
</head>
<body>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
p{
background-color: lightgrey;
</style>
</head>
<body>
</body>
</html>
Rounded Borders
The border-radius property is used to add rounded borders to an element:
Example
<!DOCTYPE html>
<html>
<head>
<style>
p.normal {
p.round1 {
border-radius: 5px;
p.round2 {
border-radius: 8px;
p.round3 {
border: 2px solid red;
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>
CSS Margins
The CSS margin properties are used to generate space around elements.
The margin properties set the size of the white space outside the border.
With CSS, you have full control over the margins. There are CSS 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
Example
<!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>
The margin property is a shorthand property for the following individual margin properties:
margin-top
margin-right
margin-bottom
margin-left
Example
<!DOCTYPE html>
<html>
<head>
<style>
div {
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: 25px;
o all four margins are 25px
The element will then take up the specified width, and the remaining space will be split equally
between the left and right margins:
Example
<!DOCTYPE html>
<html>
<head>
<style>
div {
width:300px;
margin: auto;
</style>
</head>
<body>
<h2>Use of the auto Value</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>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
div.container {
margin-left: 100px;
p.one {
margin-left: inherit;
</style>
</head>
<body>
<div class="container">
<p class="one">This is a paragraph with an inherited left margin (from the div element).</p>
</div>
</body>
</html>
Margin Collapse
Top and bottom margins of elements are sometimes collapsed into a single margin that is equal
to the largest of the two margins.
This does not happen on left and right margins! Only top and bottom margins!
Example
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
margin: 0 0 50px 0;
h2 {
margin: 20px 0 0 0;
</style>
</head>
<body>
<p>In this example the h1 element has a bottom margin of 50px and the h2 element has a top
margin of 20px. Then, the vertical margin between h1 and h2 should have been 70px (50px +
20px). However, due to margin collapse, the actual margin ends up being 50px.</p>
<h1>Heading 1</h1>
<h2>Heading 2</h2>
</body>
</html>
In the example above, the <h1> element has a bottom margin of 50px. The <h2> element has a
top margin set to 20px.
Common sense would seem to suggest that the vertical margin between the <h1> and the <h2>
would be a total of 70px (50px + 20px). But due to margin collapse, the actual margin ends up
being 50px.
CSS Padding
The CSS padding properties are used to generate space around content.
The padding clears an area around the content (inside the border) of an element.
With CSS, you have full control over the padding. There are CSS 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 <p> element:
Example
<!DOCTYPE html>
<html>
<head>
<style>
div {
background-color: lightblue;
padding-top: 50px;
padding-right: 30px;
padding-bottom: 50px;
padding-left: 80px;
</style>
</head>
<body>
</body>
</html>
Padding - Shorthand Property
To shorten the code, it is possible to specify all the padding properties in one property.
The padding property is a shorthand property for the following individual padding properties:
padding-top
padding-right
padding-bottom
padding-left
Example
<html>
<head>
<style>
div {
background-color: lightblue;
</style>
</head>
<body>
<h2>Using the padding shorthand property</h2>
<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>
padding: 25px;
o all four paddings are 25px
Example
<!DOCTYPE html>
<html>
<head>
<style>
div {
background-color: lightblue;
div.ex1 {
div.ex2 {
div.ex3 {
}
div.ex4 {
padding: 25px;
</style>
</head>
<body>
<div class="ex1">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><br>
<div class="ex2">This div element has a top padding of 25px, a left and right padding of 50px,
and a bottom padding of 75px.</div><br>
<div class="ex3">This div element has a top and bottom padding of 25px, and a left and right
padding of 50px.</div><br>
<div class="ex4">This div element has a top, right, bottom and left paddding of 25px.</div>
</body>
</html>
Setting height and width
The height and width properties are used to set the height and width of an element.
The height and width can be set to auto (this is default. Means that the browser calculates the
height and width), or be specified in length values, like px, cm, etc., or in percent (%) of the
containing block.
<!DOCTYPE html>
<html>
<head>
<style>
div {
height: 200px;
width: 50%;
background-color: powderblue;
</style>
</head>
<body>
<div></div>
</body>
</html>
This element has a height of 100 pixels and a width of 500 pixels.
Example
<!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 max-width can be specified in length values, like px, cm, etc., or in percent (%) of the
containing block, or set to none (this is default. Means that there is no maximum width).
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.
Using max-width instead, in this situation, will improve the browser's handling of small
windows.
Tip: Drag the browser window to smaller than 500px wide, to see the difference between the
two divs!
This element has a height of 100 pixels and a max-width of 500 pixels.
The following example shows a <div> element with a height of 100 pixels and a max-width of
500 pixels:
Example
<!DOCTYPE html>
<html>
<head>
<style>
div {
max-width: 500px;
height: 100px;
background-color: powderblue;
</style>
</head>
<body>
<div></div>
</body>
</html>
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:
Content - The content of the box, where text and images appear
Padding - Clears an area around the content. The padding is transparent
Border - A border that goes around the padding and content
Margin - Clears an area outside the border. The margin is transparent
The box model allows us to add a border around elements, and to define space between
elements.
Example
<!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>
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.
Example
<!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
The CSS outline properties specify the style, color, and width of an outline.
An outline is a line that is drawn around elements (outside the borders) to make the element
"stand out".
However, the outline property is different from the border property - The outline is NOT a part
of an element's dimensions; the element's total width and height is not affected by the width of
the outline.
Outline Style
The outline-style property specifies the style of the outline.
The following example first sets a thin black border around each <p> element, then it shows the
different outline-style valu
Example
<!DOCTYPE html>
<html>
<head>
<style>
p{
outline-color:red;
</head>
<body>
</body>
</html>
Outline Color
The outline-color property is used to set the color of the outline.
Example
<!DOCTYPE html>
<html>
<head>
<style>
p{
outline-style: double;
outline-color: red;
</style>
</head>
<body>
</body>
</html>
Outline Width
The outline-width property specifies the width of the outline.
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.
Example
<!DOCTYPE html>
<html>
<head>
<style>
p.one {
outline-style: double;
outline-color: red;
outline-width: thick;
p.two {
outline-style: double;
outline-color: green;
outline-width: 3px;
}
</style>
</head>
<body>
</body>
</html>
The outline property is a shorthand property for the following individual outline properties:
outline-width
outline-style (required)
outline-color
Example
<!DOCTYPE html>
<html>
<head>
<style>
p{
</style>
</head>
<body>
</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 remo ved from this
colored "Try it Yourself" link.
<!DOCTYPE html>
<html>
<head>
<style>
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>
<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
</body>
</html>
Text Color
The color property is used to set the color of the text.
<!DOCTYPE html>
<html>
<head>
<style>
body {
color: blue;
}
h1 {
color: green;
</style>
</head>
<body>
<p>This is an ordinary paragraph. Notice that this text is blue. The default text color for a page is
defined in the body selector.</p>
</body>
</html>
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>
When the text-align property is set to "justify", each line is stretched so that every line has
equal width, and the left and right margins are straight (like in magazines and newspapers):
<!DOCTYPE html>
<html>
<head>
<style>
div {
padding: 10px;
width: 200px;
height: 200px;
text-align: justify;
</style>
</head>
<body>
<p>The text-align: justify; value stretches the lines so that each line has equal width (like in
newspapers and magazines).</p>
<div>
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.'
</div>
</body>
</html>
Text Decoration
The text-decoration property is used to set or remove decorations from text.
The value text-decoration: none; is often used to remove underlines from links:
<!DOCTYPE html>
<html>
<head>
<style>
a{
text-decoration: none;
</style>
</head>
<body>
</body>
</html>
The other text-decoration values are used to decorate text:
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
text-decoration: overline;
h2 {
text-decoration: line-through;
h3 {
text-decoration: underline;
</style>
</head>
<body>
</body>
</html>
Text Transformation
The text-transform property is used to specify uppercase and lowercase letters in a text.
It can be used to turn everything into uppercase or lowercase letters, or capitalize the first letter of
each word:
<!DOCTYPE html>
<html>
<head>
<style>
p.uppercase {
text-transform: uppercase;
p.lowercase {
text-transform: lowercase;
p.capitalize {
text-transform: capitalize;
</style>
</head>
<body>
</body>
</html>
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>
Letter Spacing
The letter-spacing property is used to specify the space between the characters in a text.
The following example demonstrates how to increase or decrease the space between characters:
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
letter-spacing: 3px;
h2 {
letter-spacing: -3px;
</style>
</head>
<body>
</body>
</html>
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">
This is a paragraph with a smaller line-height.<br>
</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>
div.ex1 {
direction: rtl;
</style>
</head>
<body>
<div>This is default text direction.</div>
</body>
</html>
Word Spacing
The word-spacing property is used to specify the space between the words in a text.
The following example demonstrates how to increase or decrease the space between words:
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
word-spacing: 10px;
h2 {
word-spacing: -5px;
</style>
</head>
<body>
<h1>This is heading 1</h1>
</body>
</html>
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>
</html>
Font Family
The font family of a text is set with the font-family property.
The font-family property should hold several font names as a "fallback" system. If the browser does
not support the first font, it tries the next font, and so on.
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>
Font Style
The font-style property is mostly used to specify italic text.
<!DOCTYPE html>
<html>
<head>
<style>
p.normal {
font-style: normal;
}
p.italic {
font-style: italic;
p.oblique {
font-style: oblique;
</style>
</head>
<body>
</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.
The font-size value can be an absolute, or relative size.
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>
1em is equal to the current font size. The default text size in browsers is 16px. So, the default size of
1em is 16px.
The size can be calculated from pixels to em using this formula: pixels/16=em
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
h2 {
font-size: 1.875em; /* 30px/16=1.875em */
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>
In the example above, the text size in em is the same as the previous example in pixels. However,
with the em size, it is possible to adjust the text size in all browsers.
Unfortunately, there is still a problem with older versions of IE. The text becomes larger than it
should when made larger, and smaller than it should when made smaller.
<!DOCTYPE html>
<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>This is a paragraph.</p>
<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>
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>
Font Variant
The font-variant property specifies whether or not a text should be displayed in a small-caps font.
In a small-caps font, all lowercase letters are converted to uppercase letters. However, the converted
uppercase letters appears in a smaller font size than the original uppercase letters in the text.
<!DOCTYPE html>
<html>
<head>
<style>
p.normal {
font-variant: normal;
}
p.small {
font-variant: small-caps;
</style>
</head>
<body>
</body>
</html>
CSS Icons
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.)
<!DOCTYPE html>
<html>
<head>
<title>Font Awesome Icons</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-
awesome.min.css">
</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>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body class="container">
</body>
</html>
Google Icons
To use the Google icons, add the following line inside the <head> section of your HTML page:
<!DOCTYPE html>
<html>
<head>
<title>Google Icons</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
</head>
<body>
</body>
</html>
CSS Links
Styling Links
Links can be styled with any CSS property (e.g. color, font-family, background, etc.).
<!DOCTYPE html>
<html>
<head>
<style>
a{
color: hotpink;
</style>
</head>
<body>
</body>
</html>
In addition, links can be styled differently depending on what state they are in.
<!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>
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>
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>
<!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
CSS Lists
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>
<!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>
<!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>
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>
CSS Tables
able 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>
<!DOCTYPE html>
<html>
<head>
<style>
table {
border-collapse: collapse;
table, td, th {
</style>
</head>
<body>
<h2>Let the borders collapse:</h2>
<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>
f you only want a border around the table, only specify the border property for <table>:
<!DOCTYPE html>
<html>
<head>
<style>
table {
border-collapse: collapse;
</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>
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>
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>
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>
Table Padding
To control the space between the border and the content in a table, use the padding property on <td>
and <th> elements:
<!DOCTYPE html>
<html>
<head>
<style>
table, td, th {
border: 1px solid #ddd;
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>
Horizontal Dividers
Add the border-bottom property to <th> and <td> for horizontal dividers:
<!DOCTYPE html>
<html>
<head>
<style>
table {
border-collapse: collapse;
width: 100%;
th, td {
padding: 8px;
text-align: left;
</style>
</head>
<body>
<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>
Hoverable Table
Use the :hover selector on <tr> to highlight table rows on mouse over:
<!DOCTYPE html>
<html>
<head>
<style>
table {
border-collapse: collapse;
width: 100%;
th, td {
padding: 8px;
text-align: left;
}
tr:hover{background-color:#f5f5f5}
</style>
</head>
<body>
<h2>Hoverable Table</h2>
<p>Move the mouse over the table rows to see the effect.</p>
<table>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Points</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>
Striped Tables
For zebra-striped tables, use the nth-child() selector and add a background-color to all even (or
odd) table rows:
<!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>Striped Table</h2>
<p>For zebra-striped tables, use the nth-child() selector and add a background-color to all even (or
odd) table rows:</p>
<table>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Points</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>
Setting the width of a block-level element will prevent it from stretching out to the edges of its
container. Then, you can set the margins to auto, to horizontally center the element within its
container. The element will take up the specified width, and the remaining space will be split
equally between the two margins:
<!DOCTYPE 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>
<!DOCTYPE html>
<html>
<head>
<style>
img {
opacity: 0.5;
filter: alpha(opacity=50); /* For IE8 and earlier */
}
</style>
</head>
<body>
<h1>Image Transparency</h1>
<p>The opacity property specifies the transparency of an element. The lower the value, the more
transparent:</p>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
img {
opacity: 0.5;
filter: alpha(opacity=50); /* For IE8 and earlier */
}
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>
<img src="img_forest.jpg" alt="Forest" width="170" height="100">
<img src="img_mountains.jpg" alt="Mountains" width="170" height="100">
<img src="img_fjords.jpg" alt="Fjords" width="170" height="100">
<p><b>Note:</b> In IE, a !DOCTYPE must be added for the :hover selector to work on other
elements than the a element.</p>
</body>
</html>
xample explained
The first CSS block is similar to the code in Example 1. In addition, we have added what should
happen when a user hovers over one of the images. In this case we want the image to NOT be
transparent when the user hovers over it. The CSS for this is opacity:1;.
When the mouse pointer moves away from the image, the image will be transparent again.
<!DOCTYPE html>
<html>
<head>
<style>
img:hover {
opacity: 0.5;
filter: alpha(opacity=50); /* 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>
<img src="img_forest.jpg" alt="Forest" width="170" height="100">
<img src="img_mountains.jpg" alt="Mountains" width="170" height="100">
<img src="img_fjords.jpg" alt="Fjords" width="170" height="100">
<p><b>Note:</b> In IE, a !DOCTYPE must be added for the :hover selector to work on other elements than the a
element.</p>
</body>
</html>
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>
<p>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 fully transparent
element hard to read:</p>
<div><p>opacity 1 (default)</p></div>
</body>
</html>
<!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>opacity 1</p></div>
<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>
<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>
First, we create a <div> element (class="background") with a background image, and a border. Then
we create another <div> (class="transbox") inside the first <div>. The <div class="transbox">
have a background color, and a border - the div is transparent. Inside the transparent <div>, we
add some text inside a <p> element.
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>
<!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;
padding: 8px 16px;
text-decoration: none;
li a:hover {
background-color: #555;
color: white;
</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>
Active/Current Navigation Link
Add an "active" class to the current link to let the user know which page he/she is on:
<!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>
Full-height Fixed Vertical Navbar
Create a full-height, "sticky" side navigation:
<!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>
<div style="margin-left:25%;padding:1px 16px;height:1000px;">
<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>
<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;
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>
<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>
Right-Align Links
Right-align links by floating the list items to the right (float:right;):
<!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="#home">Home</a></li>
<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;
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;">
</div>
</body>
</html>
<!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>
<!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>
CSS Dropdowns
Create a hoverable dropdown with CSS
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>
Example Explained
HTML) Use any element to open the dropdown content, e.g. a <span>, or a <button> element.
Use a container element (like <div>) to create the dropdown content and add whatever you want
inside of it.
Wrap a <div> element around the elements to position the dropdown content correctly with CSS.
CSS) The .dropdown class use position:relative, which is needed when we want the dropdown
content to be placed right below the dropdown button (using position:absolute).
The .dropdown-content class holds the actual dropdown content. It is hidden by default, and will be
displayed on hover (see below). Note the min-width is set to 160px. Feel free to change
this. Tip: If you want the width of the dropdown content to be as wide as the dropdown button, set
the width to 100% (and overflow:auto to enable scroll on small screens).
Instead of using a border, we have used the CSS3 box-shadow property to make the dropdown
menu look like a "card".
The :hover selector is used to show the dropdown menu when the user moves the mouse over the
dropdown button.
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;
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>
CSS Tooltip
Demo: Tooltip Examples
A tooltip is often used to specify extra information about something when the user moves the mouse
pointer over an element:
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 an desirable way.</p>
</body>
</html>
Example Explained
HTML) Use a container element (like <div>) and add the "tooltip" class to it. When the user mouse
over this <div>, it will show the tooltip text.
The tooltip text is placed inside an inline element (like <span>) with class="tooltiptext".
CSS) The tooltip class use position:relative, which is needed to position the tooltip text
(position:absolute).Note: See examples below on how to position the tooltip.
The tooltiptext class holds the actual tooltip text. It is hidden by default, and will be visible on hover
(see below). We have also added some basic styles to it: 120px width, black background color,
white text color, centered text, and 5px top and bottom padding.
The CSS3 border-radius property is used to add rounded corners to the tooltip text.
The :hover selector is used to show the tooltip text when the user moves the mouse over the <div>
with class="tooltip".
Positioning Tooltips
In this example, the tooltip is placed to the right (left:105%) of the "hoverable" text (<div>). Also
note that top:-5pxis 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>
Image Gallery
The following image gallery is created with CSS:
<!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>
<!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 target="_blank" href="img_fjords.jpg">
</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>This example use media queries to re-arrange the images on different screen sizes: for screens
larger than 700px wide, it will show four images side by side, for screens smaller than 700px, it
will show two images side by side. For screens smaller than 500px, the images will stack
vertically (100%).</p>
<p>You will learn more about media queries and responsive web design later in our CSS
Tutorial.</p>
</div>
</body>
</html>
CSS Forms
The look of an HTML form can be greatly improved with CSS:
<!DOCTYPE html>
<html>
<head>
<style>
input {
width: 100%;
</style>
</head>
<body>
<form>
</form>
</body>
</html>
The example above applies to all <input> elements. If you only want to style a specific input type,
you can use attribute selectors:
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>
</body>
</html>
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>
</html>
<!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>
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>
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%;
margin: 8px 0;
box-sizing: border-box;
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>
</form>
</body>
</html>
Example 2
<!DOCTYPE html>
<html>
<head>
<style>
input[type=text] {
width: 100%;
margin: 8px 0;
box-sizing: border-box;
border: 3px solid #ccc;
-webkit-transition: 0.5s;
transition: 0.5s;
outline: none;
input[type=text]:focus {
</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>
</form>
</body>
</html>
<!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>
Styling Textareas
Tip: Use the resize property to prevent textareas from being resized (disable the "grabber" in the
bottom right corner):
<!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>
<html>
<head>
<style>
select {
width: 100%;
border: none;
border-radius: 4px;
background-color: #f1f1f1;
</style>
</head>
<body>
<p>A styled select menu.</p>
<form>
<option value="au">Australia</option>
<option value="ca">Canada</option>
<option value="usa">USA</option>
</select>
</form>
</body>
</html>
<html>
<head>
<style>
background-color: #4CAF50;
border: none;
color: white;
cursor: pointer;
</style>
</head>
<body>
</body>
</html>