CSS Tutorial: Examples in Each Chapter
CSS Tutorial: Examples in Each Chapter
CSS Tutorial: Examples in Each Chapter
W3Schools Home
Next Chapter
With our online editor, you can edit the CSS, and click on a button to view
the result.
CSS Example
body {
background-color: #d0e4fe;
}
h1 {
color: orange;
text-align: center;
}
p {
font-family: "Times New Roman";
font-size: 20px;
}
Try it yourself
CSS Introduction
Previous
Next Chapter
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!
Previous
Next Chapter
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:
Example
p {
color: red;
text-align: center;
}
Try it yourself
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
p {
color: red;
/* This is a single-line comment */
text-align: center;
}
/* This is
a multi-line
comment */
Try it yourself
CSS Selectors
CSS selectors are used to "find" (or select) HTML elements based on their
element name, id, class, attribute, and more.
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):
Example
p {
text-align: center;
color: red;
}
Try it yourself
The id Selector
The id selector uses the id attribute of an HTML element to select a specific
element.
An id should be unique within a page, so the id selector is used if you want to
select a single, 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
#para1 {
text-align: center;
color: red;
}
Try it yourself
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
.center {
text-align: center;
color: red;
}
Try it yourself
You can also specify that only specific HTML elements should be affected by a
class.
In the example below, all <p> elements with class="center" will be center-
aligned:
Example
p.center {
text-align: center;
color: red;
}
Try it yourself
Grouping Selectors
If you have elements with the same style definitions, like this:
h1 {
text-align: center;
color: red;
}
h2 {
text-align: center;
color: red;
}
p {
text-align: center;
color: red;
}
In the example below we have grouped the selectors from the code above:
Example
h1, h2, p {
text-align: center;
color: red;
}
Try it yourself
When a browser reads a style sheet, it will format the HTML document
according to the information in the 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
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>
Try it yourself
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.
body {
background-color: lightblue;
}
h1 {
color: navy;
margin-left: 20px;
}
Do not add a space between the property value and the unit (such as margin-left:20
is:margin-left:20px;
Internal styles are defined within the <style> element, inside the <head>
section of an HTML page:
Example
<head>
<style>
body {
background-color: linen;
}
h1 {
color: maroon;
margin-left: 40px;
}
</style>
</head>
Try it yourself
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
<h1 style="color:blue;margin-left:30px;">This is a heading.</h1>
Try it yourself
An inline style loses many of the advantages of a style sheet (by mixing content with
method sparingly!
h1 {
color: navy;
}
then, assume that an internal style sheet also has the following style for the
<h1> element:
h1 {
color: orange;
}
If the internal style is defined after the link to the external style sheet, the
<h1> elements will be "orange":
Example
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css">
<style>
h1 {
color: orange;
}
</style>
</head>
Try it yourself
However, if the internal style is defined before the link to the external style
sheet, the <h1> elements will be "navy":
Example
<head>
<style>
h1 {
color: orange;
}
</style>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>
Try it yourself
Cascading Order
What style will be used when there is more than one style specified for an
HTML element?
Generally speaking we can say that all the styles will "cascade" into a new
"virtual" style sheet by the following rules, where number one has the
highest priority:
So, an inline style (inside a specific HTML element) has the highest priority,
which means that it will override a style defined inside the <head> tag, or in
an external style sheet, or a browser default value.
Try it yourself
CSS Backgrounds
Previous
Next Chapter
Background Color
The background-color property specifies the background color of an
element.
Example
body {
background-color: lightblue;
}
Try it yourself
Look at CSS Color Values for a complete list of possible color values.
In the example below, the <h1>, <p>, and <div> elements have different
background colors:
Example
h1 {
background-color: green;
}
div {
background-color: lightblue;
}
p {
background-color: yellow;
}
Try it yourself
Background Image
The background-image property specifies an image to use as the
background of an element.
Example
body {
background-image: url("paper.gif");
}
Try it yourself
Example
body {
background-image: url("bgdesert.jpg");
}
Try it yourself
Note: When using a background image, use an image that does not disturb the text.
Example
body {
background-image: url("gradient_bg.png");
}
Try it yourself
Example
body {
background-image: url("gradient_bg.png");
background-repeat: repeat-x;
}
Try it yourself
Example
body {
background-image: url("img_tree.png");
background-repeat: no-repeat;
}
Try it yourself
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
body {
background-image: url("img_tree.png");
background-repeat: no-repeat;
background-position: right top;
}
Try it yourself
Example
body {
background-image: url("img_tree.png");
background-repeat: no-repeat;
background-position: right top;
background-attachment: fixed;
}
Try it yourself
Example
body {
background: #ffffff url("img_tree.png") no-repeat right top;
}
Try it yourself
When using the shorthand property the order of the property values is:
background-color
background-image
background-repeat
background-attachment
background-position
It does not matter if one of the property values is missing, as long as the
other ones are in this order.
Property Description
CSS Borders
Previous
Next Chapter
This element has a groove border that is 15px wide and green.
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).
Example
p.dotted {border-style: dotted;}
p.dashed {border-style: dashed;}
p.solid {border-style: solid;}
p.double {border-style: double;}
p.groove {border-style: groove;}
p.ridge {border-style: ridge;}
p.inset {border-style: inset;}
p.outset {border-style: outset;}
p.none {border-style: none;}
p.hidden {border-style: hidden;}
p.mix {border-style: dotted dashed solid double;}
Result:
A dotted border.
A dashed border.
A solid border.
A double border.
No border.
A hidden border.
A mixed border.
Try it yourself
Note: None of the OTHER CSS border properties described below will have ANY effec
styleproperty is set!
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
p.one {
border-style: solid;
border-width: 5px;
}
p.two {
border-style: solid;
border-width: medium;
}
p.three {
border-style: solid;
border-width: 2px 10px 4px 20px;
}
Try it yourself
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).
Example
p.one {
border-style: solid;
border-color: red;
}
p.two {
border-style: solid;
border-color: green;
}
p.three {
border-style: solid;
border-color: red green blue yellow;
}
Try it yourself
Border - Individual Sides
From the examples above you have seen that it is possible to specify a
different border for each side.
In CSS, there is also properties for specifying each of the borders (top, right,
bottom, and left):
Example
p {
border-top-style: dotted;
border-right-style: solid;
border-bottom-style: dotted;
border-left-style: solid;
}
Try it yourself
Example
p {
border-style: dotted solid;
}
Try it yourself
border-style: dotted;
o all four borders are dotted
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
Example
p {
border: 5px solid red;
}
Try it yourself
More Examples
All the top border properties in one declaration
This example demonstrates a shorthand property for setting all of the
properties for the top border in one declaration.
Property Description
CSS Margins
Previous
Next Chapter
The margin properties set the size of the white space OUTSIDE the border.
CSS Margins
The CSS margin properties set the size of the white space OUTSIDE the
border.
Note: The margins are completely transparent - and cannot have a background colo
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-top
margin-right
margin-bottom
margin-left
Note: It is also possible to use negative values for margins; to overlap content.
The following example sets different margins for all four sides of a <p>
element:
Example
p {
margin-top: 100px;
margin-bottom: 100px;
margin-right: 150px;
margin-left: 80px;
}
Try it yourself
The following example lets the left margin be inherited from the parent
element:
Example
div.container {
border: 1px solid red;
margin-left: 100px;
}
p.one {
margin-left: inherit;
}
Try it yourself
margin-top
margin-right
margin-bottom
margin-left
Example
p {
margin: 100px 150px 100px 80px;
}
Try it yourself
So, here is how it works:
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
div {
width: 300px;
margin: auto;
border: 1px solid red;
}
Try it yourself
Property Description
margin A shorthand property for setting the margin properties in one decla
CSS Padding
Previous
Next Chapter
The padding properties set the size of the white space between the element
content and the element border.
CSS Padding
The CSS padding properties define the white space between the element
content and the element border.
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
p {
padding-top: 50px;
padding-right: 30px;
padding-bottom: 50px;
padding-left: 80px;
}
Try it yourself
padding-top
padding-right
padding-bottom
padding-left
Example
p {
padding: 50px 30px 50px 80px;
}
Try it yourself
padding: 25px;
o all four paddings are 25px
More Examples
All the padding properties in one declaration
This example demonstrates a shorthand property for setting all of the
padding properties in one declaration, can have from one to four values.
Set the left padding
This example demonstrates how to set the left padding of a <p> element.
Property Description
padding A shorthand property for setting all the padding properties in one d
Next Chapter
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.
This <div> element has a height of 100 pixels and a width of 500 pixels.
Note: The height and width properties do not include padding, borders, or
margins; they set the height/width of the area inside the padding, border,
and margin of the element!
The following example shows a <div> element with a height of 100 pixels and
a width of 500 pixels:
Example
div {
width: 500px;
height: 100px;
border: 3px solid #73AD21;
}
Try it yourself
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 <div> 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
div {
max-width: 500px;
height: 100px;
border: 3px solid #73AD21;
}
Try it yourself
Property Description
CSS Text
Previous
Next Chapter
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 "Try it
yourself" link.
Text Color
The color property is used to set the color of the text.
With CSS, a color is most often specified by:
Look at CSS Color Values for a complete list of possible color values.
The default text color for a page is defined in the body selector.
Example
body {
color: blue;
}
h1 {
color: green;
}
Try it yourself
Note: For W3C compliant CSS: If you define the color property, you must also defin
colorproperty.
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):
Example
h1 {
text-align: center;
}
h2 {
text-align: left;
}
h3 {
text-align: right;
}
Try it yourself
Example
div {
text-align: justify;
}
Try it yourself
Text Decoration
The text-decoration property is used to set or remove decorations from
text.
Example
a {
text-decoration: none;
}
Try it yourself
Example
h1 {
text-decoration: overline;
}
h2 {
text-decoration: line-through;
}
h3 {
text-decoration: underline;
}
Try it yourself
Note: It is not recommended to underline text that is not a link, as this often confus
Text Transformation
The text-transform property is used to specify uppercase and lowercase
letters in a text.
Example
p.uppercase {
text-transform: uppercase;
}
p.lowercase {
text-transform: lowercase;
}
p.capitalize {
text-transform: capitalize;
}
Try it yourself
Text Indentation
The text-indent property is used to specify the indentation of the first line
of a text:
Example
p {
text-indent: 50px;
}
Try it yourself
Letter Spacing
The letter-spacing property is used to specify the space between the
characters in a text.
Example
h1 {
letter-spacing: 3px;
}
h2 {
letter-spacing: -3px;
}
Try it yourself
Line Height
The line-height property is used to specify the space between lines:
Example
p.small {
line-height: 0.8;
}
p.big {
line-height: 1.8;
}
Try it yourself
Text Direction
The direction property is used to change the text direction of an element:
Example
div {
direction: rtl;
}
Try it yourself
Word Spacing
The word-spacing property is used to specify the space between the words
in a text.
Example
h1 {
word-spacing: 10px;
}
h2 {
word-spacing: -5px;
}
Try it yourself
More Examples
Disable text wrapping inside an element
This example demonstrates how to disable text wrapping inside an element.
Property Description
unicode- Used together with the direction property to set or return whether the text sho
bidi
CSS Fonts
Previous
The CSS font properties define the font family, boldness, size, and the style
generic family - a group of font families with a similar look (like "Serif"
font family - a specific font family (like "Times New Roman" or "Arial")
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" syst
Start with the font you want, and end with a generic family, to let the browser
Note: If the name of a font family is more than one word, it must be in quotat
Example
p {
font-family: "Times New Roman", Times, serif;
}
Try it yourself
For commonly used font combinations, look at our Web Safe Font Combination
Font Style
The font-style property is mostly used to specify italic text.
Example
p.normal {
font-style: normal;
}
p.italic {
font-style: italic;
}
p.oblique {
font-style: oblique;
}
Try it yourself
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 s
Always use the proper HTML tags, like <h1> - <h6> for headings and <p> for
Absolute size:
Note: If you do not specify a font size, the default size for normal text,
Example
h1 {
font-size: 40px;
}
h2 {
font-size: 30px;
}
p {
font-size: 14px;
}
Try it yourself
Tip: If you use pixels, you can still use the zoom tool to resize the entire page
1em is equal to the current font size. The default text size in browsers is 16px
The size can be calculated from pixels to em using this formula: pixels/16=em
Example
h1 {
font-size: 2.5em; /* 40px/16=2.5em */
}
h2 {
font-size: 1.875em; /* 30px/16=1.875em */
}
p {
font-size: 0.875em; /* 14px/16=0.875em */
}
Try it yourself
In the example above, the text size in em is the same as the previous example
Unfortunately, there is still a problem with older versions of IE. The text becom
Example
body {
font-size: 100%;
}
h1 {
font-size: 2.5em;
}
h2 {
font-size: 1.875em;
}
p {
font-size: 0.875em;
}
Try it yourself
Our code now works great! It shows the same text size in all browsers, and all
Font Weight
The font-weight property specifies the weight of a font:
Example
p.normal {
font-weight: normal;
}
p.thick {
font-weight: bold;
}
Try it yourself
Font Variant
The font-variant property specifies whether or not a text should be displaye
Example
p.normal {
font-variant: normal;
}
p.small {
font-variant: small-caps;
}
Try it yourself
More Examples
All the font properties in one declaration
This example demonstrates how to use the shorthand property for setting all o
Property Description
Styling Links
Links can be styled with any CSS property (e.g. color, font-family, backgr
Example
a {
color: hotpink;
}
Try it yourself
In addition, links can be styled differently depending on what state they are in
Example
/* unvisited link */
a:link {
color: red;
}
/* visited link */
a:visited {
color: green;
}
/* selected link */
a:active {
color: blue;
}
Try it yourself
When setting the style for several link states, there are some order rules:
Text Decoration
The text-decoration property is mostly used to remove underlines from link
Example
a:link {
text-decoration: none;
}
a:visited {
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
a:active {
text-decoration: underline;
}
Try it yourself
Background Color
The background-color property can be used to specify a background color fo
Example
a:link {
background-color: yellow;
}
a:visited {
background-color: cyan;
}
a:hover {
background-color: lightgreen;
}
a:active {
background-color: hotpink;
}
Try it yourself
More Examples
Add different styles to hyperlinks
This example demonstrates how to add other styles to hyperlinks.
CSS Lists
Previous
1. Coffee
2. Tea
3. Coca Cola
Coffee
Tea
Coca Cola
unordered lists (<ul>) - the list items are marked with bullets
ordered lists (<ol>) - the list items are marked with numbers or letters
The following example shows some of the available list item markers:
Example
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;
}
Try it yourself
Note: Some of the values are for unordered lists, and some for ordered lists.
An Image as The List Item Marker
The list-style-image property specifies an image as the list item marker:
Example
ul {
list-style-image: url('sqpurple.gif');
}
Try it yourself
Example
ul {
list-style-position: inside;
}
Try it yourself
Example
ul {
list-style: square inside url("sqpurple.gif");
}
Try it yourself
When using the shorthand property, the order of the property values are:
If one of the property values above are missing, the default value for the miss
Anything added to the <ol> or <ul> tag, affects the entire list, while propertie
Example
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;
}
Result:
1. Coffee
2. Tea
3. Coca Cola
Coffee
Tea
Coca Cola
Try it yourself
More Examples
Full-width bordered list
This example demonstrates how to create a bordered list without bullets.
Property Description
CSS Tables
Previous
Company Contact
Table Borders
To specify table borders in CSS, use the border property.
The example below specifies a black border for <table>, <th>, and <td> elem
Example
table, th, td {
border: 1px solid black;
}
Try it yourself
Notice that the table in the example above has double borders. This is because
Example
table {
border-collapse: collapse;
}
table, th, td {
border: 1px solid black;
}
Try it yourself
If you only want a border around the table, only specify the border property fo
Example
table {
border: 1px solid black;
}
Try it yourself
The example below sets the width of the table to 100%, and the height of the
Example
table {
width: 100%;
}
th {
height: 50px;
}
Try it yourself
Horizontal Alignment
The text-align property sets the horizontal alignment (like left, right, or cente
By default, the content of <th> elements are center-aligned and the content o
Example
th {
text-align: left;
}
Try it yourself
Vertical Alignment
The vertical-align property sets the vertical alignment (like top, bottom, or m
By default, the vertical alignment of the content in a table is middle (for both <
The following example sets the vertical text alignment to bottom for <td> elem
Example
td {
height: 50px;
vertical-align: bottom;
}
Try it yourself
Table Padding
To control the space between the border and the content in a table, use the pa
Example
th, td {
padding: 15px;
text-align: left;
}
Try it yourself
Horizontal Dividers
First Name Last Name
Peter Griffin
Lois Griffin
Joe Swanson
Add the border-bottom property to <th> and <td> for horizontal dividers:
Example
th, td {
border-bottom: 1px solid #ddd;
}
Try it Yourself
Hoverable Table
Use the :hover selector on <tr> to highlight table rows on mouse over:
Lois Griffin
Joe Swanson
Example
tr:hover {background-color: #f5f5f5}
Try it Yourself
Striped Tables
Peter Griffin
Lois Griffin
Joe Swanson
For zebra-striped tables, use the nth-child() selector and add a background-co
Example
tr:nth-child(even) {background-color: #f2f2f2}
Try it Yourself
Table Color
The example below specifies the background color and text color of <th> elem
Peter Griffin
Lois Griffin
Joe Swanson
Example
th {
background-color: #4CAF50;
color: white;
}
Try it Yourself
Responsive Table
A responsive table will display a horizontal scroll bar if the screen is too small t
First Name Last Name Points Points Points Points Points Points P
Jill Smith 50 50 50 50 50 50 5
Eve Jackson 94 94 94 94 94 94 9
Adam Johnson 67 67 67 67 67 67 6
Add a container element (like <div>) with overflow-x:auto around the <table>
Example
<div style="overflow-x:auto;">
<table>
... table content ...
</table>
</div>
Try it Yourself
More Examples
Make a fancy table
This example demonstrates how to create a fancy table.
Property Description
The CSS box model is essentially a box that wraps around every HTML elemen
Content - The content of the box, where text and images appear
Padding - Clears an area around the content. The padding is transparen
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
Example
div {
width: 300px;
padding: 25px;
border: 25px solid navy;
margin: 25px;
}
Try it yourself
Important: When you set the width and height properties of an elemen
and height of the content area. To calculate the full size of an element
and margins.
Example
div {
width: 320px;
padding: 10px;
border: 5px solid gray;
margin: 0;
}
Try it yourself
Total element height = height + top padding + bottom padding + top border +
Note for old IE: Internet Explorer 8 and earlier versions, include padd
To fix this problem, add a <!DOCTYPE html> to the HTML page.
CSS Outline
Previous
The CSS outline properties specify the style, color, and width of an outline.
This element has a thin black border and a double outline that is 10px wide an
CSS Outline
An outline is a line that is drawn around elements (outside the borders) to mak
However, the outline property is different from the border property - The outlin
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
Example
p {
border: 1px solid black;
outline-color: red;
}
Result:
A dotted outline.
A dashed outline.
A solid outline.
A double outline.
Try it yourself
Note: None of the OTHER CSS outline properties described below will h
styleproperty is set!
Outline Color
The outline-color property is used to set the color of the outline.
Example
p {
border: 1px solid black;
outline-style: double;
outline-color: red;
}
Result:
A colored outline.
Try it yourself
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
Example
p {border: 1px solid black;}
p.one {
outline-style: double;
outline-color: red;
outline-width: thick;
}
p.two {
outline-style: double;
outline-color: green;
outline-width: 3px;
}
Result:
A thick outline.
A thinner outline.
Try it yourself
The outline property is a shorthand property for the following individual outli
outline-width
outline-style (required)
outline-color
Example
p {
border: 1px solid black;
outline: 5px dotted red;
}
Result:
An outline.
Try it yourself
Test Yourself with Exercises!
Exercise 1 Exercise 2 Exercise 3
Property Description
The display property is the most important CSS property for controlling la
Every HTML element has a default display value depending on what type of ele
Block-level Elements
A block-level element always starts on a new line and takes up the full width a
<div>
<h1> - <h6>
<p>
<form>
<header>
<footer>
<section>
Inline Elements
An inline element does not start on a new line and only takes up as much widt
This is an inline <span> element inside a paragraph.
<span>
<a>
<img>
Display: none;
display: none; is commonly used with JavaScript to hide and show elements w
Changing an inline element to a block element, or vice versa, can be useful for
Example
li {
display: inline;
}
Try it yourself
Try it yourself
Example
h1.hidden {
display: none;
}
Try it yourself
However, the element will still take up the same space as before. The element
Example
h1.hidden {
visibility: hidden;
}
Try it yourself
More Examples
display: none; versus visibility: hidden;
This example demonstrates display: none; versus visibility: hidden;
Test Yourself with Exercises!
Exercise 1 Exercise 2 Exercise 3 Exercise 4
Property Description
Setting the width of a block-level element will prevent it from stretching out to
remaining space will be split equally between the two margins:
This <div> element has a width of 500px, and margin set to auto.
Note: The problem with the <div> above occurs when the browser window is s
Using max-width instead, in this situation, will improve the browser's handling o
This <div> element has a max-width of 500px, and margin set to auto.
Tip: Drag the browser window to smaller than 500px wide, to see the differen
Example
div.ex1 {
width: 500px;
margin: auto;
border: 3px solid #73AD21;
}
div.ex2 {
max-width: 500px;
margin: auto;
border: 3px solid #73AD21;
}
CSS Layout - width and max-width
Previous
Setting the width of a block-level element will prevent it from stretching out to
remaining space will be split equally between the two margins:
This <div> element has a width of 500px, and margin set to auto.
Note: The problem with the <div> above occurs when the browser window is s
Using max-width instead, in this situation, will improve the browser's handling o
This <div> element has a max-width of 500px, and margin set to auto.
Tip: Drag the browser window to smaller than 500px wide, to see the differen
Example
div.ex1 {
width: 500px;
margin: auto;
border: 3px solid #73AD21;
}
div.ex2 {
max-width: 500px;
margin: auto;
border: 3px solid #73AD21;
}
The position property specifies the type of positioning method used for an
static
relative
fixed
absolute
Elements are then positioned using the top, bottom, left, and right properties.
position: static;
HTML elements are positioned static by default.
Static positioned elements are not affected by the top, bottom, left, and right
Example
div.static {
position: static;
border: 3px solid #73AD21;
}
Try it yourself
position: relative;
An element with position: relative; is positioned relative to its normal positio
Setting the top, right, bottom, and left properties of a relatively-positioned ele
Example
div.relative {
position: relative;
left: 30px;
border: 3px solid #73AD21;
}
Try it yourself
position: fixed;
An element with position: fixed; is positioned relative to the viewport, which
A fixed element does not leave a gap in the page where it would normally have
Notice the fixed element in the lower-right corner of the page. Here is the CSS
Example
div.fixed {
position: fixed;
bottom: 0;
right: 0;
width: 300px;
border: 3px solid #73AD21;
}
Try it yourself
position: absolute;
An element with position: absolute; is positioned relative to the nearest posit
Example
div.relative {
position: relative;
width: 400px;
height: 200px;
border: 3px solid #73AD21;
}
div.absolute {
position: absolute;
top: 80px;
right: 0;
width: 200px;
height: 100px;
border: 3px solid #73AD21;
}
Try it yourself
Overlapping Elements
When elements are positioned, they can overlap other elements.
The z-index property specifies the stack order of an element (which element sh
Example
img {
position: absolute;
left: 0px;
top: 0px;
z-index: -1;
}
Try it yourself
An element with greater stack order is always in front of an element with a low
Note: If two positioned elements overlap without a z-index specified, th
code will be shown on top.
More Examples
Set the shape of an element
This example demonstrates how to set the shape of an element. The element
Property Description
The following example specifies that an image should float to the right in a tex
Example
img {
float: right;
margin: 0 0 10px 10px;
}
Try it yourself
Elements after a floating element will flow around it. To avoid this, use the cle
Example
div {
clear: left;
}
Try it yourself
Then we can add overflow: auto; to the containing element to fix this problem
Example
.clearfix {
overflow: auto;
}
Try it yourself
Example
div {
border: 3px solid blue;
}
.clearfix {
overflow: auto;
}
nav {
float: left;
width: 200px;
border: 3px solid #73AD21;
}
section {
margin-left: 206px;
border: 3px solid red;
}
Try it yourself
More Examples
An image with border and margins that floats to the right in a paragraph
Let an image float to the right in a paragraph. Add border and margins to the
However, the inline-block value of the display property makes this even easie
inline-block elements are like inline elements but they can have a width and a
Examples
The old way - using float (notice that we also need to specify a clear property
Example
.floating-box {
float: left;
width: 150px;
height: 75px;
margin: 10px;
border: 3px solid #73AD21;
}
.after-box {
clear: left;
}
Try it yourself
The same effect can be achieved by using the inline-block value of the displa
Example
.floating-box {
display: inline-block;
width: 150px;
height: 75px;
margin: 10px;
border: 3px solid #73AD21;
}
CSS Layout - Horizontal Align
Previous
The element will then take up the specified width, and the remaining space wil
Example
.center {
margin: auto;
width: 60%;
border:3px solid #73AD21;
padding: 10px;
}
Try it yourself
Tip: Center aligning has no effect if the width property is not set (or set to 100
Tip: For aligning text, see the CSS Text chapter.
Example
.right {
position: absolute;
right: 0px;
width: 300px;
border:3px solid #73AD21;
padding: 10px;
}
Try it yourself
Note: Absolute positioned elements are removed from the normal flow, and ca
Tip: When aligning elements with position, always define margin and padding f
There is also a problem with IE8 and earlier, when using position. If a contain
This seems to be space reserved for a scrollbar. So, always set the !DOCTYPE
Example
body {
margin: 0;
padding: 0;
}
.container {
position: relative;
width: 100%;
}
.right {
position: absolute;
right: 0px;
width: 300px;
background-color: #b0e0e6;
}
Try it yourself
Example
.right {
float: right;
width: 300px;
border:3px solid #73AD21;
padding: 10px;
}
Try it yourself
Tip: When aligning elements with float, always define margin and padding for t
There is also a problem with IE8 and earlier, when using float. If the !DOCTYP
when using float:
Example
body {
margin: 0;
padding: 0;
}
.right {
float: right;
width: 300px;
background-color: #b0e0e6;
}
Try it yourself
CSS Combinators
Previous
CSS Combinators
A CSS selector can contain more than one simple selector. Between the simple
Descendant Selector
The descendant selector matches all elements that are descendants of a specif
The following example selects all <p> elements inside <div> elements:
Example
div p {
background-color: yellow;
}
Try it yourself
Child Selector
The child selector selects all elements that are the immediate children of a spe
The following example selects all <p> elements that are immediate children of
Example
div > p {
background-color: yellow;
}
Try it yourself
Sibling elements must have the same parent element, and "adjacent" means "
The following example selects all <p> elements that are placed immediately a
Example
div + p {
background-color: yellow;
}
Try it yourself
The following example selects all <p> elements that are siblings of <div> elem
Example
div ~ p {
background-color: yellow;
}
Try it yourself
CSS Pseudo-classes
Previous
selector:pseudo-class {
property:value;
}
Anchor Pseudo-classes
Links can be displayed in different ways:
Example
/* unvisited link */
a:link {
color: #FF0000;
}
/* visited link */
a:visited {
color: #00FF00;
}
/* selected link */
a:active {
color: #0000FF;
}
Try it yourself
Note: a:hover MUST come after a:link and a:visited in the CSS definiti
effective!a:active MUST come after a:hover in the CSS definition in orde
are not case-sensitive.
Pseudo-classes and CSS Classes
Pseudo-classes can be combined with CSS classes:
Example
a.highlight:hover {
color: #ff0000;
}
Try it yourself
When you hover over the link in the example, it will change color.
Example
p:first-child {
color: blue;
}
Try it yourself
Try it yourself
Example
p:first-child i {
color: blue;
}
Try it yourself
In the example below, :lang defines the quotation marks for <q> elements wi
Example
<html>
<head>
<style>
q:lang(no) {
quotes: "~" "~";
}
</style>
</head>
<body>
<p>Some text <q lang="no">A quote in a paragraph</q> Some text.</p>
</body>
</html>
Try it yourself
More Examples
Add different styles to hyperlinks
This example demonstrates how to add other styles to hyperlinks.
Use of :focus
This example demonstrates how to use the :focus pseudo-class.
Home
News
Contact
About
Horizontal
Home
News
Contact
o About
Home
News
Contact
o About
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:
Example
<ul>
<li><a href="default.asp">Home</a></li>
<li><a href="news.asp">News</a></li>
<li><a href="contact.asp">Contact</a></li>
<li><a href="about.asp">About</a></li>
</ul>
Try it yourself
Now let's remove the bullets and the margins and padding from the list:
Example
ul {
list-style-type: none;
margin: 0;
padding: 0;
}
Try it yourself
Example explained:
The code in the example above is the standard code used in both vertical,
and horizontal navigation bars.
Example
li a {
display: block;
width: 60px;
}
Try it yourself
Example explained:
Example
ul {
list-style-type: none;
margin: 0;
padding: 0;
width: 60px;
}
li a {
display: block;
}
Try it yourself
Home
News
Contact
About
Example
ul {
list-style-type: none;
margin: 0;
padding: 0;
width: 200px;
background-color: #f1f1f1;
}
li a {
display: block;
color: #000;
padding: 8px 0 8px 16px;
text-decoration: none;
}
Try it yourself
Home
News
Contact
About
Example
.active {
background-color: #4CAF50;
color: white;
}
Try it yourself
Add the border property to <ul> add a border around the navbar. If you
also want borders inside the navbar, add aborder-bottom to all <li>
elements, except for the last one:
Home
News
Contact
About
Example
ul {
border: 1px solid #555;
}
li {
text-align: center;
border-bottom: 1px solid #555;
}
li:last-child {
border-bottom: none;
}
Try it yourself
Example
ul {
list-style-type: none;
margin: 0;
padding: 0;
width: 25%;
background-color: #f1f1f1;
height: 100%; /* Full height */
position: fixed; /* Make it stick, even on scroll */
overflow: auto; /* Enable scrolling if the sidenav has too much
content */
}
Try it yourself
Example
li {
display: inline;
}
Try it yourself
Example explained:
Example
li {
float: left;
}
a {
display: block;
padding: 8px;
background-color: #dddddd;
}
Try it yourself
Example explained:
float: left; - use float to get block elements to slide next to each
other
display: block; - Displaying the links as block elements makes the
whole link area clickable (not just the text), and it allows us to specify
padding (and height, width, margins, etc. if you want)
padding: 8px; - Since block elements take up the full width
available, they cannot float next to each other. Therefore, specify
some padding to make them look good
background-color: #dddddd; - Add a gray background-color to each
a element
Tip: Add the background-color to <ul> instead of each <a> element if you
want a full-width background color:
Example
ul {
background-color: #dddddd;
}
Try it yourself
Home
News
Contact
About
Example
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;
}
Try it yourself
Home
News
Contact
About
Example
.active {
background-color: #4CAF50;
}
Try it yourself
Right-Align Links
Right-align links by adding a new <ul> inside the <ul> with float:right;:
Home
News
Contact
o About
o Login
Example
<ul>
<li><a class="active" href="#home">Home</a></li>
<li><a href="#news">News</a></li>
<li><a href="#contact">Contact</a></li>
<ul style="float:right;list-style-type:none;">
<li><a href="#about">About</a></li>
<li><a href="#login">Login</a></li>
</ul>
</ul>
Try it yourself
Border Dividers
Add the border-right property to <li> to create link dividers:
Home
News
Contact
o About
o Login
Example
/* Add a gray right border to all list items, except the last item
(last-child) */
li {
border-right: 1px solid #bbb;
}
li:last-child {
border-right: none;
}
Try it yourself
Fixed Top
ul {
position: fixed;
top: 0;
width: 100%;
}
Try it yourself
Fixed Bottom
ul {
position: fixed;
bottom: 0;
width: 100%;
}
Try it yourself
Home
News
Contact
About
Example
ul {
border: 1px solid #e7e7e7;
background-color: #f3f3f3;
}
li a {
color: #666;
}
CSS Dropdowns
Previous
Next Chapter
Dropdown Examples
Move the mouse over the examples below:
Dropdown Text
Dropdown Menu
Other:
Basic Dropdown
Create a dropdown box that appears when the user moves the mouse over
an element.
Example
<style>
.dropdown {
position: relative;
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);
padding: 12px 16px;
z-index: 1;
}
.dropdown:hover .dropdown-content {
display: block;
}
</style>
<div class="dropdown">
<span>Mouse over me</span>
<div class="dropdown-content">
<p>Hello World!</p>
</div>
</div>
Try it Yourself
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.
Dropdown Menu
Create a dropdown menu that allows the user to choose an option from a
list:
Dropdown Menu
This example is similar to the previous one, except that we add links inside
the dropdown box and style them to fit a styled dropdown button:
Example
<style>
/* Style The Dropdown Button */
.dropbtn {
background-color: #4CAF50;
color: white;
padding: 16px;
font-size: 16px;
border: none;
cursor: pointer;
}
/* Change the background color of the dropdown button when the dropdown
content is shown */
.dropdown:hover .dropbtn {
background-color: #3e8e41;
}
</style>
<div class="dropdown">
<button class="dropbtn">Dropdown</button>
<div class="dropdown-content">
<a href="#">Link 1</a>
<a href="#">Link 2</a>
<a href="#">Link 3</a>
</div>
</div>
Try it Yourself
Right
If you want the dropdown menu to go from right to left, instead of left to
right, add right: 0;
Example
.dropdown-content {
right: 0;
}
Try it Yourself
More Examples
Dropdown Image
This example demonstrates how to add an image and other content inside
the dropdown box.
Dropdown Navbar
This example demonstrates how to add a dropdown menu inside a navigation
bar.
Image Gallery
The following image gallery is created with CSS:
Example
<html>
<head>
<style>
div.img {
margin: 5px;
border: 1px solid #ccc;
float: left;
width: 180px;
}
div.img:hover {
border: 1px solid #777;
}
div.img img {
width: 100%;
height: auto;
}
div.desc {
padding: 15px;
text-align: center;
}
</style>
</head>
<body>
<div class="img">
<a target="_blank" href="fjords.jpg">
<img src="fjords.jpg" alt="Fjords" width="300" height="200">
</a>
<div class="desc">Add a description of the image here</div>
</div>
<div class="img">
<a target="_blank" href="img_forest.jpg">
<img src="img_forest.jpg" alt="Forest" width="300" height="200">
</a>
<div class="desc">Add a description of the image here</div>
</div>
<div class="img">
<a target="_blank" href="lights.jpg">
<img src="img_lights.jpg" alt="Northern
Lights" width="300" height="200">
</a>
<div class="desc">Add a description of the image here</div>
</div>
<div class="img">
<a target="_blank" href="img_mountains.jpg">
<img src="img_mountains.jpg" alt="Mountains" width="300" height="200">
</a>
<div class="desc">Add a description of the image here</div>
</div>
</body>
</html>
Try it yourself
Previous
Next Chapter
W3SCHOOLS EXAMS
HTML, CSS, JavaScript, PHP, jQuery, and XML Certifications
COLOR PICKER
CSS Image Opacity / Transparency
Previous
Next Chapter
First we will show you how to create a transparent image with CSS.
Regular image:
Example
img {
opacity: 0.4;
filter: alpha(opacity=40); /* For IE8 and earlier */
}
Try it yourself
The opacity property can take a value from 0.0 - 1.0. The lower value, the
more transparent.
IE8 and earlier use filter:alpha(opacity=x). The x can take a value from 0 -
100. A lower value makes the element more transparent.
Example
img {
opacity: 0.4;
filter: alpha(opacity=40); /* For IE8 and earlier */
}
img:hover {
opacity: 1.0;
filter: alpha(opacity=100); /* For IE8 and earlier */
}
Try it yourself
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.
Example
<html>
<head>
<style>
div.background {
background: url(klematis.jpg) repeat;
border: 2px solid black;
}
div.transbox {
margin: 30px;
background-color: #ffffff;
border: 1px solid black;
opacity: 0.6;
filter: alpha(opacity=60); /* For IE8 and earlier */
}
div.transbox p {
margin: 5%;
font-weight: bold;
color: #000000;
}
</style>
</head>
<body>
<div class="background">
<div class="transbox">
<p>This is some text that is placed in the transparent box.</p>
</div>
</div>
</body>
</html>
Try it yourself
Image Sprites
An image sprite is a collection of images put into a single image.
A web page with many images can take a long time to load and generates
multiple server requests.
Using image sprites will reduce the number of server requests and save
bandwidth.
With CSS, we can show just the part of the image we need.
Example
#home {
width: 46px;
height: 44px;
background: url(img_navsprites.gif) 0 0;
}
Try it yourself
Example explained:
<img id="home" src="img_trans.gif"> - Only defines a small transparent
image because the src attribute cannot be empty. The displayed image
will be the background image we specify in CSS
width: 46px; height: 44px; - Defines the portion of the image we want
to use
background: url(img_navsprites.gif) 0 0; - Defines the background
image and its position (left 0px, top 0px)
This is the easiest way to use image sprites, now we want to expand it by
using links and hover effects.
We will use an HTML list, because it can be a link and also supports a
background image:
Example
#navlist {
position: relative;
}
#navlist li {
margin: 0;
padding: 0;
list-style: none;
position: absolute;
top: 0;
}
#home {
left: 0px;
width: 46px;
background: url('img_navsprites.gif') 0 0;
}
#prev {
left: 63px;
width: 43px;
background: url('img_navsprites.gif') -47px 0;
}
#next {
left: 129px;
width: 43px;
background: url('img_navsprites.gif') -91px 0;
}
Try it yourself
Example explained:
Tip: The :hover selector can be used on all elements, not only on links.
Because this is one single image, and not six separate files, there will be no
loading delay when a user hovers over the image.
Example
#home a:hover {
background: url('img_navsprites_hover.gif') 0 -45px;
}
#prev a:hover {
background: url('img_navsprites_hover.gif') -47px -45px;
}
#next a:hover {
background: url('img_navsprites_hover.gif') -91px -45px;
}
Try it yourself
Example explained:
Next Chapter
The following example selects all <a> elements with a target attribute:
Example
a[target] {
background-color: yellow;
}
Try it yourself
Example
a[target="_blank"] {
background-color: yellow;
}
Try it yourself
The following example selects all elements with a title attribute that contains
a space-separated list of words, one of which is "flower":
Example
[title~="flower"] {
border: 5px solid yellow;
}
Try it yourself
The following example selects all elements with a class attribute value that
begins with "top":
Note: The value has to be a whole word, either alone, like class="top", or
followed by a hyphen( - ), like class="top-text"!
Example
[class|="top"] {
background: yellow;
}
Try it yourself
The following example selects all elements with a class attribute value that
begins with "top":
Example
[class^="top"] {
background: yellow;
}
Try it yourself
The following example selects all elements with a class attribute value that
ends with "test":
Try it yourself
The following example selects all elements with a class attribute value that
contains "te":
Example
[class*="te"] {
background: yellow;
}
Try it yourself
Styling Forms
The attribute selectors can be useful for styling forms without class or ID:
Example
input[type="text"] {
width: 150px;
display: block;
margin-bottom: 10px;
background-color: yellow;
}
input[type="button"] {
width: 120px;
margin-left: 35px;
display: block;
}
Try it yourself
For a complete reference of all the CSS selectors, please go to our CSS
Selectors Reference.
CSS Counters
Previous
Next Chapter
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:
Example
body {
counter-reset: section;
}
h2::before {
counter-increment: section;
content: "Section " counter(section) ": ";
}
Try it yourself
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>":
Example
body {
counter-reset: section;
}
h1 {
counter-reset: subsection;
}
h1::before {
counter-increment: section;
content: "Section " counter(section) ". ";
}
h2::before {
counter-increment: subsection;
content: counter(section) "." counter(subsection) " ";
}
Try it yourself
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:
Example
ol {
counter-reset: section;
list-style-type: none;
}
li::before {
counter-increment: section;
content: counters(section,".") " ";
}
Try it yourself
Property Description
content Used with the ::before and ::after pseudo-elements, to insert generated conte
CSS3 Introduction
Previous
CSS3 Modules
CSS3 has been split into "modules". It contains the "old CSS specification" (wh
In addition, new modules are added.
Some of the most important CSS3 modules are:
Selectors
Box Model
Backgrounds and Borders
Image Values and Replaced Content
Text Effects
2D/3D Transformations
Animations
Multiple Column Layout
User Interface
Browser Support
The numbers in the table specify the first browser version that fully supports t
Numbers followed by -webkit- or -moz- specify the first version that worked w
Property
Rounded corners!
Rounded corners!
Rounded corners!
Example
#rcorners1 {
border-radius: 25px;
background: #73AD21;
padding: 20px;
width: 200px;
height: 150px;
}
#rcorners2 {
border-radius: 25px;
border: 2px solid #73AD21;
padding: 20px;
width: 200px;
height: 150px;
}
#rcorners3 {
border-radius: 25px;
background: url(paper.gif);
background-position: left top;
background-repeat: repeat;
padding: 20px;
width: 200px;
height: 150px;
}
Try it yourself
However, you can specify each corner separately if you wish. Here are the rule
Four values: first value applies to top-left, second value applies to top-
right, and fourth value applies to bottom-left corner
Three values: first value applies to top-left, second value applies to top
applies to bottom-right
Two values: first value applies to top-left and bottom-right corner, and
and bottom-left corner
One value: all four corners are rounded equally
Example
#rcorners4 {
border-radius: 15px 50px 30px 5px;
background: #73AD21;
padding: 20px;
width: 200px;
height: 150px;
}
#rcorners5 {
border-radius: 15px 50px 30px;
background: #73AD21;
padding: 20px;
width: 200px;
height: 150px;
}
#rcorners6 {
border-radius: 15px 50px;
background: #73AD21;
padding: 20px;
width: 200px;
height: 150px;
}
Try it yourself
Example
#rcorners7 {
border-radius: 50px/15px;
background: #73AD21;
padding: 20px;
width: 200px;
height: 150px;
}
#rcorners8 {
border-radius: 15px/50px;
background: #73AD21;
padding: 20px;
width: 200px;
height: 150px;
}
#rcorners9 {
border-radius: 50%;
background: #73AD21;
padding: 20px;
width: 200px;
height: 150px;
}
Try it yourself
Property Description
Browser Support
The numbers in the table specify the first browser version that fully supports t
Numbers followed by -webkit-, -moz-, or -o- specify the first version that work
Property
The border-image property takes the image and slices it into nine sections, li
the corners at the corners, and the middle sections are repeated or stretched a
Note: For border-image to work, the element also needs the border propert
Here, the middle sections of the image are repeated to create the border:
An image as a border!
Example
#borderimg {
border: 10px solid transparent;
padding: 15px;
-webkit-border-image: url(border.png) 30 round; /* Safari 3.1-5 *
-o-border-image: url(border.png) 30 round; /* Opera 11-12.1 */
border-image: url(border.png) 30 round;
}
Try it yourself
Here, the middle sections of the image are stretched to create the border:
An image as a border!
Try it yourself
Example 1:
Example 2:
Example 3:
Example
#borderimg1 {
border: 10px solid transparent;
padding: 15px;
-webkit-border-image: url(border.png) 50 round; /* Safari 3.1-5 *
-o-border-image: url(border.png) 50 round; /* Opera 11-12.1 */
border-image: url(border.png) 50 round;
}
#borderimg2 {
border: 10px solid transparent;
padding: 15px;
-webkit-border-image: url(border.png) 20% round; /* Safari 3.1-5
-o-border-image: url(border.png) 20% round; /* Opera 11-12.1 */
border-image: url(border.png) 20% round;
}
#borderimg3 {
border: 10px solid transparent;
padding: 15px;
-webkit-border-image: url(border.png) 30% round; /* Safari 3.1-5
-o-border-image: url(border.png) 30% round; /* Opera 11-12.1 */
border-image: url(border.png) 30% round;
}
Try it yourself
Property Description
CSS3 Backgrounds
Previous
CSS3 Backgrounds
CSS3 contains a few new background properties, which allow greater control o
In this chapter you will learn how to add multiple background images to one e
You will also learn about the following new CSS3 properties:
background-size
background-origin
background-clip
Browser Support
The numbers in the table specify the first browser version that fully supports t
Numbers followed by -webkit-, -moz-, or -o- specify the first version that work
Property
The different background images are separated by commas, and the images a
the first image is closest to the viewer.
The following example has two background images, the first image is a flower
the second image is a paper background (aligned to the top-left corner):
Example
#example1 {
background-image: url(img_flwr.gif), url(paper.gif);
background-position: right bottom, left top;
background-repeat: no-repeat, repeat;
}
Try it yourself
Multiple background images can be specified using either the individual backgr
thebackground shorthand property.
The following example uses the background shorthand property (same result
Example
#example1 {
background: url(img_flwr.gif) right bottom no-repeat, url(paper.g
}
Try it yourself
Before CSS3, the size of a background image was the actual size of the image
images in different contexts.
The size can be specified in lengths, percentages, or by using one of the two k
The following example resizes a background image to much smaller than the o
Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit
consequat.
Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit
consequat.
Example
#div1 {
background: url(img_flower.jpg);
background-size: 100px 80px;
background-repeat: no-repeat;
}
Try it yourself
The two other possible values for background-size are contain and cover.
The cover keyword scales the background image so that the content area is c
image (both its width and height are equal to or exceed the content area). As
image may not be visible in the background positioning area.
Example
#div1 {
background: url(img_flower.jpg);
background-size: contain;
background-repeat: no-repeat;
}
#div2 {
background: url(img_flower.jpg);
background-size: cover;
background-repeat: no-repeat;
}
Try it yourself
The following example has three background images specified, with different b
Example
#example1 {
background: url(img_flwr.gif) left top no-repeat, url(img_flwr.gi
url(paper.gif) left top repeat;
background-size: 50px, 130px, auto;
}
Try it yourself
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 ele
the browser window). Then set a fixed and centered background on it. Then ad
property:
Example
html {
background: url(img_flower.jpg) no-repeat center center fixed;
background-size: cover;
}
Try it yourself
border-box - the background image starts from the upper left corner of
padding-box - (default) the background image starts from the upper left
content-box - the background image starts from the upper left corner of
Example
#example1 {
border: 10px solid black;
padding:35px;
background:url(img_flwr.gif);
background-repeat: no-repeat;
background-origin: content-box;
}
Try it yourself
Try it yourself
Property Description
CSS3 Colors
CSS supports color names, hexadecimal and RGB colors.
RGBA colors
HSL colors
HSLA colors
opacity
Browser Support
The numbers in the table specify the first browser version that fully supports C
Property
An RGBA color value is specified with: rgba(red, green, blue, alpha). The alpha
(fully transparent) and 1.0 (fully opaque).
rgba(255, 0, 0, 0.2);
rgba(255, 0, 0, 0.4);
rgba(255, 0, 0, 0.6);
rgba(255, 0, 0, 0.8);
Example
#p1 {background-color: rgba(255, 0, 0, 0.3);} /* red with opacity */
#p2 {background-color: rgba(0, 255, 0, 0.3);} /* green with opacity
#p3 {background-color: rgba(0, 0, 255, 0.3);} /* blue with opacity *
Try it yourself
HSL Colors
HSL stands for Hue, Saturation and Lightness.
Example
#p1 {background-color: hsl(120, 100%, 50%);} /* green */
#p2 {background-color: hsl(120, 100%, 75%);} /* light green */
#p3 {background-color: hsl(120, 100%, 25%);} /* dark green */
#p4 {background-color: hsl(120, 60%, 70%);} /* pastel green */
Try it yourself
HSLA Colors
HSLA color values are an extension of HSL color values with an alpha channel
color.
Example
#p1 {background-color: hsla(120, 100%, 50%, 0.3);} /* green with opa
#p2 {background-color: hsla(120, 100%, 75%, 0.3);} /* light green wi
#p3 {background-color: hsla(120, 100%, 25%, 0.3);} /* dark green wit
#p4 {background-color: hsla(120, 60%, 70%, 0.3);} /* pastel green w
Try it yourself
Opacity
The CSS3 opacity property sets the opacity for a specified RGB value.
The opacity property value must be a number between 0.0 (fully transparent)
rgb(255, 0, 0);opacity:0.2;
rgb(255, 0, 0);opacity:0.4;
rgb(255, 0, 0);opacity:0.6;
rgb(255, 0, 0);opacity:0.8;
Example
#p1 {background-color:rgb(255,0,0);opacity:0.6;} /* red with opacity
#p2 {background-color:rgb(0,255,0);opacity:0.6;} /* green with opaci
#p3 {background-color:rgb(0,0,255);opacity:0.6;} /* blue with opacit
CSS3 Gradients
Previous
Gradient Backgroun
CSS3 gradients let you display smooth transitions between two or more specif
Earlier, you had to use images for these effects. However, by using CSS3 grad
and bandwidth usage. In addition, elements with gradients look better when z
generated by the browser.
Browser Support
The numbers in the table specify the first browser version that fully supports t
Numbers followed by -webkit-, -moz-, or -o- specify the first version that work
Property
Syntax
background: linear-gradient(direction, color-stop1, color-stop2, ...)
The following example shows a linear gradient that starts at the top. It starts r
Example
#grad {
background: red; /* For browsers that do not support gradients */
background: -webkit-linear-gradient(red, yellow); /* For Safari 5
background: -o-linear-gradient(red, yellow); /* For Opera 11.1 to
background: -moz-linear-gradient(red, yellow); /* For Firefox 3.6
background: linear-gradient(red, yellow); /* Standard syntax */
}
Try it yourself
The following example shows a linear gradient that starts from the left. It star
Example
#grad {
background: red; /* For browsers that do not support gradients */
background: -webkit-linear-gradient(left, red , yellow); /* For Saf
background: -o-linear-gradient(right, red, yellow); /* For Opera 11
background: -moz-linear-gradient(right, red, yellow); /* For Firefo
background: linear-gradient(to right, red , yellow); /* Standard sy
}
Try it yourself
You can make a gradient diagonally by specifying both the horizontal and verti
The following example shows a linear gradient that starts at top left (and goes
transitioning to yellow:
Example
#grad {
background: red; /* For browsers that do not support gradients */
background: -webkit-linear-gradient(left top, red, yellow); /* For
background: -o-linear-gradient(bottom right, red, yellow); /* For O
background: -moz-linear-gradient(bottom right, red, yellow); /* For
background: linear-gradient(to bottom right, red, yellow); /* Stand
}
Try it yourself
Using Angles
If you want more control over the direction of the gradient, you can define an
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 li
Example
#grad {
background: red; /* For browsers that do not support gradients */
background: -webkit-linear-gradient(-90deg, red, yellow); /* For Sa
background: -o-linear-gradient(-90deg, red, yellow); /* For Opera 1
background: -moz-linear-gradient(-90deg, red, yellow); /* For Firef
background: linear-gradient(-90deg, red, yellow); /* Standard synta
}
Try it yourself
Example
#grad {
background: red; /* For browsers that do not support gradients */
background: -webkit-linear-gradient(red, yellow, green); /* For Saf
background: -o-linear-gradient(red, yellow, green); /* For Opera 11
background: -moz-linear-gradient(red, yellow, green); /* For Firefo
background: linear-gradient(red, yellow, green); /* Standard syntax
}
Try it yourself
The following example shows how to create a linear gradient (from left to right
some text:
Gradient Backgro
Example
#grad {
background: red; /* For browsers that do not support gradients */
/* For Safari 5.1 to 6.0 */
background: -webkit-linear-gradient(left,red,orange,yellow,green,bl
/* For Opera 11.1 to 12.0 */
background: -o-linear-gradient(left,red,orange,yellow,green,blue,in
/* For Fx 3.6 to 15 */
background: -moz-linear-gradient(left,red,orange,yellow,green,blue,
/* Standard syntax */
background: linear-gradient(to right, red,orange,yellow,green,blue,
}
Try it yourself
Using Transparency
CSS3 gradients also support transparency, which can be used to create fading
To add transparency, we use the rgba() function to define the color stops. The
can be a value from 0 to 1, and it defines the transparency of the color: 0 indi
color (no transparency).
The following example shows a linear gradient that starts from the left. It start
color red:
Example
#grad {
background: red; /* For browsers that do not support gradients */
background: -webkit-linear-gradient(left,rgba(255,0,0,0),rgba(255,0
background: -o-linear-gradient(right,rgba(255,0,0,0),rgba(255,0,0,1
background: -moz-linear-gradient(right,rgba(255,0,0,0),rgba(255,0,0
background: linear-gradient(to right, rgba(255,0,0,0), rgba(255,0,0
}
Try it yourself
Repeating a linear-gradient
The repeating-linear-gradient() function is used to repeat linear gradients:
Example
A repeating linear gradient:
#grad {
background: red; /* For browsers that do not support gradients */
/* Safari 5.1 to 6.0 */
background: -webkit-repeating-linear-gradient(red, yellow 10%, gree
/* Opera 11.1 to 12.0 */
background: -o-repeating-linear-gradient(red, yellow 10%, green 20%
/* Firefox 3.6 to 15 */
background: -moz-repeating-linear-gradient(red, yellow 10%, green 2
/* Standard syntax */
background: repeating-linear-gradient(red, yellow 10%, green 20%);
}
Try it yourself
To create a radial gradient you must also define at least two color stops.
Syntax
background: radial-gradient(shape size at position, start-color, ...,
The following example shows a radial gradient with evenly spaced color stops:
Example
#grad {
background: red; /* For browsers that do not support gradients */
background: -webkit-radial-gradient(red, yellow, green); /* Safari
background: -o-radial-gradient(red, yellow, green); /* For Opera 11
background: -moz-radial-gradient(red, yellow, green); /* For Firefo
background: radial-gradient(red, yellow, green); /* Standard syntax
}
Try it yourself
The following example shows a radial gradient with differently spaced color sto
Example
#grad {
background: red; /* For browsers that do not support gradients */
background: -webkit-radial-gradient(red 5%, yellow 15%, green 60%);
background: -o-radial-gradient(red 5%, yellow 15%, green 60%); /* F
background: -moz-radial-gradient(red 5%, yellow 15%, green 60%); /*
background: radial-gradient(red 5%, yellow 15%, green 60%); /* Stan
}
Try it yourself
Set Shape
The shape parameter defines the shape. It can take the value circle or ellipse.
The following example shows a radial gradient with the shape of a circle:
Example
#grad {
background: red; /* For browsers that do not support gradients */
background: -webkit-radial-gradient(circle, red, yellow, green); /*
background: -o-radial-gradient(circle, red, yellow, green); /* Oper
background: -moz-radial-gradient(circle, red, yellow, green); /* Fi
background: radial-gradient(circle, red, yellow, green); /* Standar
}
Try it yourself
closest-side
farthest-side
closest-corner
farthest-corner
Example
A radial gradient with different size keywords:
#grad1 {
background: red; /* For browsers that do not support gradients */
/* Safari 5.1 to 6.0 */
background: -webkit-radial-gradient(60% 55%, closest-side, red, yel
/* For Opera 11.6 to 12.0 */
background: -o-radial-gradient(60% 55%, closest-side, red, yellow,
/* For Firefox 3.6 to 15 */
background: -moz-radial-gradient(60% 55%, closest-side, red, yellow
/* Standard syntax */
background: radial-gradient(closest-side at 60% 55%, red, yellow, b
}
#grad2 {
/* Safari 5.1 to 6.0 */
background: -webkit-radial-gradient(60% 55%, farthest-side, red, ye
/* Opera 11.6 to 12.0 */
background: -o-radial-gradient(60% 55%, farthest-side, red, yellow,
/* For Firefox 3.6 to 15 */
background: -moz-radial-gradient(60% 55%, farthest-side, red, yello
/* Standard syntax */
background: radial-gradient(farthest-side at 60% 55%, red, yellow,
}
Try it yourself
Repeating a radial-gradient
The repeating-radial-gradient() function is used to repeat radial gradients:
Example
A repeating radial gradient:
#grad {
background: red; /* For browsers that do not support gradients */
/* For Safari 5.1 to 6.0 */
background: -webkit-repeating-radial-gradient(red, yellow 10%, gree
/* For Opera 11.6 to 12.0 */
background: -o-repeating-radial-gradient(red, yellow 10%, green 15%
/* For Firefox 3.6 to 15 */
background: -moz-repeating-radial-gradient(red, yellow 10%, green 1
/* Standard syntax */
background: repeating-radial-gradient(red, yellow 10%, green 15%);
}
text-shadow
box-shadow
Browser Support
The numbers in the table specify the first browser version that fully supports t
Numbers followed by -webkit- or -moz- specifies the first version that worked
Property
In its simplest use, you only specify the horizontal shadow (2px) and the verti
Try it yourself
Try it yourself
Try it yourself
Try it yourself
Try it yourself
Multiple Shadows
To add more than one shadow to the text, you can add a comma-separated lis
The following example shows a red and blue neon glow shadow:
Try it yourself
The following example shows a white text with black, blue, and darkblue shado
Try it yourself
CSS3 box-shadow Property
The CSS3 box-shadow property applies shadow to elements.
In its simplest use, you only specify the horizontal shadow and the vertical sha
Example
div {
box-shadow: 10px 10px;
}
Try it yourself
Example
div {
box-shadow: 10px 10px grey;
}
Try it yourself
Example
div {
box-shadow: 10px 10px 5px grey;
}
Try it yourself
Property Description
CSS3 Text
Previous
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
p.test2 {
white-space: nowrap;
width: 200px;
border: 1px solid #000000;
overflow: hidden;
text-overflow: ellipsis;
}
Try it yourself
The following example shows how you can display the overflowed content when hovering ove
Example
div.test:hover {
text-overflow: inherit;
overflow: visible;
}
Try it yourself
The word-wrap property allows you to force the text to wrap - even if it means splitting it in th
p {
word-wrap: break-word;
}
Try it yourself
This paragraph contains some text. The lines will break at any character.
Example
p.test1 {
word-break: keep-all;
}
p.test2 {
word-break: break-all;
}
Try it yourself
Property Description
Previous
W3SCHOOLS EXAMS
HTML, CSS, JavaScript, PHP, jQuery, and XML Certif
COLOR PICKER
LEARN MORE:
Google Maps
Animated Buttons
Modal Boxes
JS Animations
Progress Bars
Dropdowns
HTML Includes
Color Palettes