CSS Tutorial: Examples in Each Chapter
CSS Tutorial: Examples in Each Chapter
CSS Tutorial: Examples in Each Chapter
W3Schools Home
Next Chapter
CSS
CSS Example
body {
background-color: #d0e4fe;
}
h1 {
color: orange;
text-align: center;
}
p{
CSS Examples
Learn from over 300 examples! With our editor, you can edit the CSS, and click
on a button to view the result.
Try-It-Yourself!
CSS References
At W3Schools you will find complete CSS references of all properties and
selectors with syntax, examples, browser support, and more.
CSS Properties Reference
CSS Selectors Reference
CSS Aural Reference
CSS Units
W3Schools Home
Next Chapter
W3SCHOOLS EXAMS
HTML, CSS, JavaScript, PHP, jQuery, Bootstrap and XML Certifications
COLOR PICKER
LEARN MORE:
Color Converter
Google Maps
Animated Buttons
Modal Boxes
Modal Images
Tooltips
Loaders
JS Animations
Progress Bars
Dropdowns
Slideshow
Side Navigation
HTML Includes
Color Palettes
Code Coloring
CSS Introduction
Previous
Next Chapter
What is CSS?
CSS saves a lot of work. It can control the layout of multiple web pages
all at once
<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.
CSS removed the style formatting from the HTML page!
CSS Syntax
A CSS rule-set consists of a selector and a declaration block:
Example
p{
color: red;
text-align: center;
}
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.
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.
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
#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, only <p> elements with class="center" will be centeraligned:
Example
p.center {
text-align: center;
color: red;
}
Try it Yourself
HTML elements can also refer to more than one class.
In the example below, the <p> element will be styled according to
class="center" and to class="large":
Example
<p class="center large">This paragraph refers to two classes.</p>
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;
}
Example
h1, h2, p {
text-align: center;
color: red;
}
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
When a browser reads a style sheet, it will format the HTML document
according to the information in the style sheet.
Inline style
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.
Here is how the "myStyle.css" looks:
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 p
correct way is: margin-left:20px;
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 p
Use this method sparingly!
Example
Assume that an external style sheet has the following style for the <h1>
element:
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:
1. Inline style (inside an HTML element)
2. External and internal style sheets (in the head section)
3. Browser default
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 Colors
Previous
Next Chapter
Color Names
Colors set by using color names:
Example
Color
Name
Red
Green
Blue
Orange
Yellow
Cyan
Black
Try it Yourself
Note: Color names are case-insensitive: "Red" is the same as "red" or "RED".
HTML and CSS supports 140 standard color names.
Red
Green
Blue
255
rgb(255, 0, 0)
Example
Color
RGB
rgb(255,0,0)
rgb(0,255,0)
rgb(0,0,255)
rgb(255,165,0)
rgb(255,255,0)
rgb(0,255,255)
Try it Yourself
Shades of grey are often defined using equal values for all the 3 light sources:
Example
Color
RGB
rgb(0,0,0)
rgb(128,128,128)
rgb(255,255,255)
Try it Yourself
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
Color
HEX
#FF0000
#00FF00
#0000FF
#FFA500
#FFFF00
#00FFFF
Try it Yourself
Shades of grey are often defined using equal values for all the 3 light sources:
Example
Color
HEX
#000000
#808080
#FFFFFF
Try it Yourself
Previous
Next Chapter
W3SCHOOLS EXAMS
HTML, CSS, JavaScript, PHP, jQuery, Bootstrap and XML Certifications
COLOR PICKER
LEARN MORE:
Color Converter
Google Maps
Animated Buttons
Modal Boxes
Modal Images
Tooltips
Loaders
JS Animations
Progress Bars
Dropdowns
Slideshow
Side Navigation
HTML Includes
Color Palettes
Code Coloring
CSS Backgrounds
Previous
Next Chapter
The CSS background properties are used to define the background effects
for elements.
CSS background properties:
background-color
background-image
background-repeat
background-attachment
background-position
Background Color
Example
body {
background-color: lightblue;
}
Try it Yourself
With CSS, a color is most often specified by:
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
Below is an example of a bad combination of text and background image. The text is hardly
readable:
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
If the image above is repeated only horizontally (background-repeat: repeat-x;), the
background will look better:
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.
The position of the image is specified by the background-position property:
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.
Description
background
background-attachment
Sets whether a background image is fixed or scrolls with the rest of the p
background-color
background-image
background-position
background-repeat
Previous
Next Chapter
W3SCHOOLS EXAMS
HTML, CSS, JavaScript, PHP, jQuery, Bootstrap and XML Certifications
COLOR PICKER
LEARN MORE:
Color Converter
Google Maps
Animated Buttons
Modal Boxes
Modal Images
Tooltips
Loaders
JS Animations
Progress Bars
Dropdowns
Slideshow
Side Navigation
HTML Includes
Color Palettes
Code Coloring
CSS Borders
Previous
Next Chapter
Border Style
The border-style property specifies what kind of border to display.
The following values are allowed:
groove - Defines a 3D grooved border. The effect depends on the bordercolor value
ridge - Defines a 3D ridged border. The effect depends on the bordercolor value
outset - Defines a 3D outset border. The effect depends on the bordercolor value
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.
A groove border. The effect depends on the border-color value.
A ridge border. The effect depends on the border-color value.
An inset border. The effect depends on the border-color value.
An outset border. The effect depends on the border-color value.
No border.
A hidden border.
A mixed border.
Try it Yourself
Note: None of the OTHER CSS border properties described below will have ANY effect
theborder-style property 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 color can be set by:
transparent
The border-color property can have from one to four values (for the top
border, right border, bottom border, and the left border).
If border-color is not set, it inherits the color of the element.
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
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
The example above gives the same result as this:
Example
p{
border-style: dotted solid;
}
Try it Yourself
So, here is how it works:
If the border-style property has four values:
border-style: dotted;
o all four borders are dotted
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.
Set the style of the bottom border
This example demonstrates how to set the style of the bottom border.
Set the width of the left border
This example demonstrates how to set the width of the left border.
Set the color of the four borders
This example demonstrates how to set the color of the four borders. It can have
from one to four colors.
Set the color of the right border
This example demonstrates how to set the color of the right border.
Description
border
border-bottom
border-bottom-color
border-bottom-style
border-bottom-width
border-color
border-left
border-left-color
border-left-style
border-left-width
border-right
border-right-color
border-right-style
border-right-width
border-style
border-top
border-top-color
border-top-style
border-top-width
border-width
CSS Margins
Previous
Next Chapter
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 color!
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
inherit - specifies that the margin should be inherited from the parent
element
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:
If the margin property has four values:
margin: 25px;
o all four margins are 25px
Example
div {
width: 300px;
margin: auto;
border: 1px solid red;
}
Try it Yourself
Description
margin
margin-bottom
margin-left
margin-right
margin-top
Previous
Next Chapter
CSS Padding
Previous
Next Chapter
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
inherit - specifies that the padding should be inherited from the parent
element
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
So, here is how it works:
If the padding property has four values:
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.
Set the right padding
This example demonstrates how to set the right padding of a <p> element.
Set the top padding
This example demonstrates how to set the top padding of a <p> element.
Set the bottom padding
This example demonstrates how to set the bottom padding of a <p> element.
Description
padding
A shorthand property for setting all the padding properties in one decla
padding-bottom
padding-left
padding-right
padding-top
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.
Example
div {
max-width: 500px;
height: 100px;
border: 3px solid #73AD21;
}
Try it Yourself
Description
height
max-height
max-width
min-height
min-width
width
Previous
Next Chapter
W3SCHOOLS EXAMS
HTML, CSS, JavaScript, PHP, jQuery, Bootstrap and XML Certifications
COLOR PICKER
LEARN MORE:
Color Converter
Google Maps
Animated Buttons
Modal Boxes
Modal Images
Tooltips
Loaders
JS Animations
Progress Bars
Dropdowns
Slideshow
Side Navigation
HTML Includes
Color Palettes
Code Coloring
SHARE THIS PAGE
CSS Text
Previous
Next Chapter
TEXT FORMATTING
This text is styled with some of the text formatting
properties.
The
heading
uses
the
text-align,
texttransform,
and
color
properties.
The
paragraph
is
indented, aligned, and the space between characters is
s p e c i f i e d . T h e u n d e r l i n e i s r e m o v e d f r o m t h i s c o l o r e d " 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 define
thebackground-color property.
Text Alignment
The text-align property is used to set the horizontal alignment of a text.
Example
h1 {
text-align: center;
}
h2 {
text-align: left;
}
h3 {
text-align: right;
}
Try it Yourself
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):
Example
div {
text-align: justify;
}
Try it Yourself
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:
Example
a{
text-decoration: none;
}
Try it Yourself
The other text-decoration values are used to decorate text:
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 confuses
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:
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.
The following example demonstrates how to increase or decrease the space
between characters:
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.
The following example demonstrates how to increase or decrease the space
between words:
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.
Vertical alignment of an image
This example demonstrates how to set the vertical align of an image in a text.
Add shadow to text
This example demonstrates how to add shadow to text.
CSS Fonts
Previous
Next Chapter
The CSS font properties define the font family, boldness, size, and the style
of a text.
generic family - a group of font families with a similar look (like "Serif"
or "Monospace")
font family - a specific font family (like "Times New Roman" or "Arial")
Generic family
Font family
Description
Serif
Sans-serif
Arial
Verdana
Monospace
Courier New
Lucida Console
Note: On computer screens, sans-serif fonts are considered easier to read than serif fo
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".
More than one font family is specified in a comma-separated list:
Example
p{
font-family: "Times New Roman", Times, serif;
}
Try it Yourself
For commonly used font combinations, look at our Web Safe Font Combinations.
Font Style
The font-style property is mostly used to specify italic text.
This property has three values:
oblique - The text is "leaning" (oblique is very similar to italic, but less
supported)
Example
p.normal {
font-style: normal;
}
p.italic {
font-style: italic;
}
p.oblique {
font-style: oblique;
}
Try it Yourself
Font Size
Does not allow a user to change the text size in all browsers (bad for
accessibility reasons)
Absolute size is useful when the physical size of the output is known
Relative size:
Note: If you do not specify a font size, the default size for normal text, like paragraphs
(16px=1em).
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.
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
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.
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
allows all browsers to zoom or resize the text!
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 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.
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 of
the font properties in one declaration.
Description
font
font-family
font-size
font-style
font-variant
font-weight
CSS Links
Previous
Next Chapter
Link Button
Link Button
Styling Links
Links can be styled with any CSS property (e.g. color, font-family, background, etc.).
Example
a{
color: hotpink;
}
Try it Yourself
In addition, links can be styled differently depending on what state they are in.
The four links states are:
Example
/* unvisited link */
a:link {
color: red;
}
/* visited link */
a:visited {
color: green;
}
/* mouse over link */
a:hover {
color: hotpink;
}
/* 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 links:
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 for links:
Example
a:link {
background-color: yellow;
}
a:visited {
background-color: cyan;
}
a:hover {
background-color: lightgreen;
}
a:active {
background-color: hotpink;
}
Try it Yourself
Example
a:link, a:visited {
background-color: #f44336;
color: white;
padding: 14px 25px;
text-align: center;
text-decoration: none;
display: inline-block;
}
a:hover, a:active {
background-color: red;
}
Try it Yourself
More Examples
Add different styles to hyperlinks
This example demonstrates how to add other styles to hyperlinks.
Advanced - Create a link button with borders
Another example of how to create link boxes/buttons.
Previous
Next Chapter
W3SCHOOLS EXAMS
HTML, CSS, JavaScript, PHP, jQuery, Bootstrap and XML Certifications
COLOR PICKER
LEARN MORE:
Color Converter
Google Maps
Animated Buttons
Modal Boxes
Modal Images
Tooltips
Loaders
JS Animations
Progress Bars
Dropdowns
Slideshow
Side Navigation
HTML Includes
Color Palettes
Code Coloring
CSS Lists
Previous
Next Chapter
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
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.
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
missing property will be inserted, if any.
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.
CSS Tables
Previous
Next Chapter
Contact
Cou
Alfreds Futterkiste
Maria Anders
Germ
Berglunds snabbkp
Christina Berglund
Swe
Francisco Chang
Mex
Ernst Handel
Roland Mendel
Aust
Island Trading
Helen Bennett
UK
Kniglich Essen
Philip Cramer
Germ
Yoshi Tannamuri
Can
Giovanni Rovelli
Italy
Table Borders
To specify table borders in CSS, use the border property.
The example below specifies a black border for <table>, <th>, and <td> elements:
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 both the table and
the <th> and <td> elements have separate borders.
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 for <table>:
Example
table {
border: 1px solid black;
}
Try it Yourself
Example
table {
width: 100%;
}
th {
height: 50px;
}
Try it Yourself
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.
The following example left-aligns the text in <th> elements:
Example
th {
text-align: left;
}
Try it Yourself
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:
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 padding property on
<td> and <th> elements:
Example
th, td {
padding: 15px;
text-align: left;
}
Try it Yourself
Horizontal Dividers
First Name
Last Name
Savings
Peter
Griffin
$100
Lois
Griffin
$150
Joe
Swanson
$300
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:
First Name
Last Name
Savings
Peter
Griffin
$100
Lois
Griffin
$150
Joe
Swanson
$300
First Name
Last Name
Savings
Peter
Griffin
$100
Lois
Griffin
$150
Example
tr:hover {background-color: #f5f5f5}
Try it Yourself
Striped Tables
Joe
Swanson
$300
For zebra-striped tables, use the nth-child() selector and add a background-color to all even
(or odd) table rows:
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> elements:
First Name
Last Name
Savings
Peter
Griffin
$100
Lois
Griffin
$150
Joe
Swanson
$300
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 to display the full
content:
First
Name
Last
Name
Point
s
Point
s
Point
s
Point
s
Point
s
Point
s
Point
s
Point
s
Jill
Smith
50
50
50
50
50
50
50
50
Eve
Jackson
94
94
94
94
94
94
94
94
Adam
Johnson
67
67
67
67
67
67
67
67
Add a container element (like <div>) with overflow-x:auto around the <table> element to
make it responsive:
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.
Set the position of the table caption
This example demonstrates how to position the table caption.
Description
border
border-collapse
border-spacing
caption-side
empty-cells
table-layout
Previous
Next Chapter
W3SCHOOLS EXAMS
HTML, CSS, JavaScript, PHP, jQuery, Bootstrap and XML Certifications
COLOR PICKER
LEARN MORE:
Color Converter
Google Maps
Animated Buttons
Modal Boxes
Modal Images
Tooltips
Loaders
JS Animations
Progress Bars
Dropdowns
Slideshow
Side Navigation
HTML Includes
Color Palettes
Code Coloring
SHARE THIS PAGE
Next Chapter
Content - The content of the box, where text and images appear
The box model allows us to add a border around elements, and to define space
between elements.
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 element with CSS, yo
width and height of the content area. To calculate the full size of an element, you mu
padding, borders and margins.
Example
div {
width: 320px;
padding: 10px;
border: 5px solid gray;
margin: 0;
}
Try it Yourself
Here is the math:
320px (width)
+ 20px (left + right padding)
+ 10px (left + right border)
+ 0px (left + right margin)
= 350px
Note for old IE: Internet Explorer 8 and earlier versions, include padding and border
property. To fix this problem, add a <!DOCTYPE html> to the HTML page.
CSS Outline
Previous
Next Chapter
CSS 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 outline-style property can have one of the following values:
ridge - Defines a 3D ridged outline. The effect depends on the outlinecolor value
inset - Defines a 3D inset outline. The effect depends on the outlinecolor value
outset - Defines a 3D outset outline. The effect depends on the outlinecolor value
The following example first sets a thin black border around each <p> element,
then it shows the differentoutline-style values:
Example
p{
border: 1px solid black;
outline-color: red;
}
p.dotted {outline-style: dotted;}
p.dashed {outline-style: dashed;}
p.solid {outline-style: solid;}
p.double {outline-style: double;}
p.groove {outline-style: groove;}
p.ridge {outline-style: ridge;}
p.inset {outline-style: inset;}
p.outset {outline-style: outset;}
Result:
A dotted outline.
A dashed outline.
A solid outline.
A double outline.
A groove outline. The effect depends on the outline-color value.
A ridge outline. The effect depends on the outline-color value.
An inset outline. The effect depends on the outline-color value.
An outset outline. The effect depends on the outline-color value.
Try it Yourself
Note: None of the OTHER CSS outline properties described below will have ANY effect
Outline Color
The outline-color property is used to set the color of the outline.
The color can be set by:
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
of the three pre-defined values: thin, medium, or thick.
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
outline-width
outline-style (required)
outline-color
Example
p{
border: 1px solid black;
outline: 5px dotted red;
}
Result:
An outline.
Try it Yourself
Description
outline
outline-color
outline-offset
outline-style
outline-width
The display property is the most important CSS property for controlling layout.
Block-level Elements
A block-level element always starts on a new line and takes up the full width available (stretches
out to the left and right as far as it can).
The <div> element is a block-level element.
Examples of block-level elements:
<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 width as necessary.
This is an inline <span> element inside a paragraph.
Examples of inline elements:
<span>
<a>
<img>
Display: none;
is commonly used with JavaScript to hide and show elements without deleting
and recreating them. Take a look at our last example on this page if you want to know how this
can be achieved.
display: none;
Example
li {
display: inline;
}
Try it Yourself
Note: Setting the display property of an element only changes how the element is displayed, NOT what
it is. So, an inline element with display: block; is not allowed to have other block elements inside it.
Example
span {
display: block;
}
Try it Yourself
Example
h1.hidden {
display: none;
}
Try it Yourself
visibility:hidden;
However, the element will still take up the same space as before. The element will be hidden, but
still affect the layout:
Example
h1.hidden {
visibility: hidden;
}
Try it Yourself
More Examples
display: none; versus visibility: hidden;
This example demonstrates display: none; versus visibility: hidden;
Using CSS together with JavaScript to show content
This example demonstrates how to use CSS and JavaScript to show an element on click.
Description
display
visibility
Previous
Next Chapter
W3SCHOOLS EXAMS
HTML, CSS, JavaScript, PHP, jQuery, Bootstrap and XML Certifications
COLOR PICKER
LEARN MORE:
Color Converter
Google Maps
Animated Buttons
Modal Boxes
Modal Images
Tooltips
Loaders
JS Animations
Progress Bars
Dropdowns
Slideshow
Side Navigation
HTML Includes
Color Palettes
Code Coloring
Note: The problem with the <div> above occurs when the browser window is smaller than the
width of the element. 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. This is important when making a site usable on small devices:
This <div> element has a max-width of 500px, and margin set to auto.
Tip: Resize the browser window to less than 500px wide, to see the difference between the two
divs!
Here is an example of the two divs above:
Example
div.ex1 {
width: 500px;
margin: auto;
border: 3px solid #73AD21;
}
div.ex2 {
max-width: 500px;
margin: auto;
Previous
Next Chapter
W3SCHOOLS EXAMS
HTML, CSS, JavaScript, PHP, jQuery, Bootstrap and XML Certifications
COLOR PICKER
LEARN MORE:
Color Converter
Google Maps
Animated Buttons
Modal Boxes
Modal Images
Tooltips
Loaders
JS Animations
Progress Bars
Dropdowns
Slideshow
Side Navigation
HTML Includes
Color Palettes
Code Coloring
SHARE THIS PAGE
The position property specifies the type of positioning method used for an element (static,
relative, fixed or absolute).
static
relative
fixed
absolute
Elements are then positioned using the top, bottom, left, and right properties. However, these
properties will not work unless the position property is set first. They also work differently
depending on the position value.
position: static;
HTML elements are positioned static by default.
Static positioned elements are not affected by the top, bottom, left, and right properties.
An element with position: static; is not positioned in any special way; it is always
positioned according to the normal flow of the page:
This <div> element has position: static;
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 position.
Setting the top, right, bottom, and left properties of a relatively-positioned element will cause it
to be adjusted away from its normal position. Other content will not be adjusted to fit into any
gap left by the element.
This <div> element has position: relative;
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 means it
always stays in the same place even if the page is scrolled. The top, right, bottom, and left
properties are used to position the element.
A fixed element does not leave a gap in the page where it would normally have been located.
Notice the fixed element in the lower-right corner of the page. Here is the CSS that is used:
Example
div.fixed {
position: fixed;
bottom: 0;
right: 0;
width: 300px;
border: 3px solid #73AD21;
}
Try it Yourself
This <div> element has position: fixed;
position: absolute;
An element with position: absolute; is positioned relative to the nearest positioned ancestor
(instead of positioned relative to the viewport, like fixed).
However; if an absolute positioned element has no positioned ancestors, it uses the document
body, and moves along with page scrolling.
Note: A "positioned" element is one whose position is anything except static.
Here is a simple example:
This <div> element has position: relative;
This <div> element has position: absolute;
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 should be placed in
front of, or behind, the others).
An element can have a positive or negative stack order:
This is a heading
Because the image has a z-index of -1, it will be placed behind the text.
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 lower stack order.
Note: If two positioned elements overlap without a z-index specified, the element positioned last in the
be shown on top.
Example
Bottom Left
Top Left
Top Right
Bottom Right
Centered
Try it Yourself:
Top Left Top Right Bottom Left Bottom Right Centered
More Examples
Set the shape of an element
This example demonstrates how to set the shape of an element. The element is clipped into this
shape, and displayed.
How to show overflow in an element using scroll
This example demonstrates how to set the overflow property to create a scroll bar when an
element's content is too big to fit in a specified area.
How to set the browser to automatically handle overflow
This example demonstrates how to set the browser to automatically handle overflow.
Change the cursor
This example demonstrates how to change the cursor.
Description
bottom
clip
cursor
left
overflow
overflow-x
Specifies what to do with the left/right edges of the content if it overflows the e
content area
overflow-y
position
right
top
z-index
Previous
Next Chapter
W3SCHOOLS EXAMS
COLOR PICKER
LEARN MORE:
Color Converter
Google Maps
Animated Buttons
Modal Boxes
Modal Images
Tooltips
Loaders
JS Animations
Progress Bars
Dropdowns
Slideshow
Side Navigation
HTML Includes
Color Palettes
Code Coloring
Example
img {
float: right;
margin: 0 0 10px 10px;
}
Try it Yourself
Example
div {
clear: left;
}
Try it Yourself
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 image.
An image with a caption that floats to the right
Let an image with a caption float to the right.
Let the first letter of a paragraph float to the left
Let the first letter of a paragraph float to the left and style the letter.
Creating a horizontal menu
Use float with a list of hyperlinks to create a horizontal menu.
Creating a homepage without tables
Use float to create a homepage with a header, footer, left content and main content.
Description
clear
Specifies on which sides of an element where floating elements are not allo
float
overflow
overflow-x
overflow-y
Previous
Next Chapter
W3SCHOOLS EXAMS
HTML, CSS, JavaScript, PHP, jQuery, Bootstrap and XML Certifications
COLOR PICKER
LEARN MORE:
Color Converter
Google Maps
Animated Buttons
Modal Boxes
Modal Images
Tooltips
Loaders
JS Animations
Progress Bars
Dropdowns
Slideshow
Side Navigation
HTML Includes
Color Palettes
Code Coloring
SHARE THIS PAGE
Examples
The old way - using float (notice that we also need to specify a clear property
for the element after the floating boxes):
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 display property (notice that noclear property is needed):
Example
.floating-box {
display: inline-block;
width: 150px;
height: 75px;
margin: 10px;
border: 3px solid #73AD21;
}
Try it Yourself
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 can overlap
elements.
Tip: When aligning elements with position, always define margin and padding for
the <body> element. This is to avoid visual differences in different browsers.
There is also a problem with IE8 and earlier, when using position. If a container element (in
our case <div class="container">) has a specified width, and the !DOCTYPE declaration is
missing, IE8 and earlier versions will add a 17px margin on the right side. This seems to be
space reserved for a scrollbar. So, always set the !DOCTYPE declaration when using position:
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
the <body> element. This is to avoid visual differences in different browsers.
There is also a problem with IE8 and earlier, when using float. If the !DOCTYPE declaration is
missing, IE8 and earlier versions will add a 17px margin on the right side. This seems to be
space reserved for a scrollbar. So, always set the !DOCTYPE declaration when using float:
Example
body {
margin: 0;
padding: 0;
}
.right {
float: right;
width: 300px;
background-color: #b0e0e6;
}
Try it Yourself
Previous
Next Chapter
W3SCHOOLS EXAMS
HTML, CSS, JavaScript, PHP, jQuery, Bootstrap and XML Certifications
COLOR PICKER
LEARN MORE:
Color Converter
Google Maps
Animated Buttons
Modal Boxes
Modal Images
Tooltips
Loaders
JS Animations
Progress Bars
Dropdowns
Slideshow
Side Navigation
HTML Includes
Color Palettes
Code Coloring
SHARE THIS PAGE
CSS Combinators
Previous
Next Chapter
CSS Combinators
A combinator is something that explains the relationship between the selectors.
A CSS selector can contain more than one simple selector. Between the simple selectors, we can
include a combinator.
There are four different combinators in CSS3:
Descendant Selector
The descendant selector matches all elements that are descendants of a specified element.
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 specified element.
The following example selects all <p> elements that are immediate children of a <div> element:
Example
div > p {
background-color: yellow;
}
Try it Yourself
Example
div + p {
background-color: yellow;
}
Try it Yourself
Example
div ~ p {
background-color: yellow;
}
Try it Yourself
Previous
Next Chapter
W3SCHOOLS EXAMS
HTML, CSS, JavaScript, PHP, jQuery, Bootstrap and XML Certifications
COLOR PICKER
LEARN MORE:
Color Converter
Google Maps
Animated Buttons
Modal Boxes
Modal Images
Tooltips
Loaders
JS Animations
Progress Bars
Dropdowns
Slideshow
Side Navigation
HTML Includes
Color Palettes
Code Coloring
CSS Pseudo-classes
Previous
Next Chapter
Syntax
The syntax of pseudo-classes:
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;
}
/* mouse over link */
a:hover {
color: #FF00FF;
}
/* selected link */
a:active {
color: #0000FF;
}
Try it Yourself
Note: a:hover MUST come after a:link and a:visited in the CSS definition in order to be effective!a
come after a:hover in the CSS definition in order to be effective! Pseudo-class names are not case-sensi
Example
a.highlight:hover {
color: #ff0000;
}
Try it Yourself
Hover on <div>
An example of using the :hover pseudo-class on a <div> element:
Example
div:hover {
background-color: blue;
}
Try it Yourself
Example
p:first-child {
color: blue;
}
Try it Yourself
Example
p i:first-child {
color: blue;
}
Try it Yourself
Example
p:first-child i {
color: blue;
}
Try it Yourself
The :lang pseudo-class allows you to define special rules for different languages.
In the example below, :lang defines the quotation marks for <q> elements with lang="no":
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.
Example
Example description
:active
a:active
:checked
input:checked
:disabled
input:disabled
:empty
p:empty
:enabled
input:enabled
:first-child
p:first-child
Selects every <p> elements that is the first child of its paren
:first-of-type
p:first-of-type
:focus
input:focus
:hover
a:hover
:in-range
input:in-range
:invalid
input:invalid
:lang(language)
p:lang(it)
:last-child
p:last-child
Selects every <p> elements that is the last child of its paren
:last-of-type
p:last-of-type
Selects every <p> element that is the last <p> element of its
:link
a:link
:not(selector)
:not(p)
:nth-child(n)
p:nth-child(2)
Selects every <p> element that is the second child of its par
:nth-last-child(n)
p:nth-last-child(2)
Selects every <p> element that is the second child of its par
from the last child
:nth-last-of-type(n)
p:nth-last-of-type(2)
:nth-of-type(n)
p:nth-of-type(2)
:only-of-type
p:only-of-type
:only-child
p:only-child
Selects every <p> element that is the only child of its paren
:optional
input:optional
:out-of-range
input:out-of-range
:read-only
input:read-only
:read-write
input:read-write
:required
input:required
:root
root
:target
#news:target
:valid
input:valid
:visited
a:visited
Example
Example description
::after
p::after
::before
p::before
::first-letter
p::first-letter
::first-line
p::first-line
::selection
p::selection
Previous
Next Chapter
W3SCHOOLS EXAMS
HTML, CSS, JavaScript, PHP, jQuery, Bootstrap and XML Certifications
COLOR PICKER
LEARN MORE:
Color Converter
Google Maps
Animated Buttons
Modal Boxes
Modal Images
Tooltips
Loaders
JS Animations
Progress Bars
Dropdowns
Slideshow
Side Navigation
HTML Includes
Color Palettes
Code Coloring
CSS Pseudo-elements
Previous
Next Chapter
Syntax
The syntax of pseudo-elements:
selector::pseudo-element {
property:value;
}
Notice the double colon notation - ::first-line versus :first-line
The double colon replaced the single-colon notation for pseudo-elements in CSS3. This was an attempt f
distinguish between pseudo-classes and pseudo-elements.
The single-colon syntax was used for both pseudo-classes and pseudo-elements in CSS2 and CSS1.
For backward compatibility, the single-colon syntax is acceptable for CSS2 and CSS1 pseudo-elements.
The ::first-line pseudo-element is used to add a special style to the first line of a text.
The following example formats the first line of the text in all <p> elements:
Example
p::first-line {
color: #ff0000;
font-variant: small-caps;
}
Try it Yourself
Note: The ::first-line pseudo-element can only be applied to block-level elements.
The following properties apply to the ::first-line pseudo-element:
font properties
color properties
background properties
word-spacing
letter-spacing
text-decoration
vertical-align
text-transform
line-height
clear
Example
p::first-letter {
color: #ff0000;
font-size: xx-large;
}
Try it Yourself
Note: The ::first-letter pseudo-element can only be applied to block-level elements.
The following properties apply to the ::first-letter pseudo- element:
font properties
color properties
background properties
margin properties
padding properties
border properties
text-decoration
text-transform
line-height
float
clear
Example
p.intro::first-letter {
color: #ff0000;
font-size:200%;
}
Try it Yourself
The example above will display the first letter of paragraphs with class="intro", in red and in a
larger size.
Multiple Pseudo-elements
Several pseudo-elements can also be combined.
In the following example, the first letter of a paragraph will be red, in an xx-large font size. The
rest of the first line will be blue, and in small-caps. The rest of the paragraph will be the default
font size and color:
Example
p::first-letter {
color: #ff0000;
font-size: xx-large;
}
p::first-line {
color: #0000ff;
font-variant: small-caps;
}
Try it Yourself
Example
h1::before {
content: url(smiley.gif);
}
Try it Yourself
Example
h1::after {
content: url(smiley.gif);
}
Try it Yourself
Example
::selection {
color: red;
background: yellow;
}
Try it Yourself
Example
Example description
::after
p::after
::before
p::before
::first-letter
p::first-letter
::first-line
p::first-line
::selection
p::selection
Example
Example description
:active
a:active
:checked
input:checked
:disabled
input:disabled
:empty
p:empty
:enabled
input:enabled
:first-child
p:first-child
Selects every <p> elements that is the first child of its paren
:first-of-type
p:first-of-type
:focus
input:focus
:hover
a:hover
:in-range
input:in-range
:invalid
input:invalid
:lang(language)
p:lang(it)
:last-child
p:last-child
Selects every <p> elements that is the last child of its paren
:last-of-type
p:last-of-type
Selects every <p> element that is the last <p> element of its
:link
a:link
:not(selector)
:not(p)
:nth-child(n)
p:nth-child(2)
Selects every <p> element that is the second child of its par
:nth-last-child(n)
p:nth-last-child(2)
Selects every <p> element that is the second child of its par
from the last child
:nth-last-of-type(n)
p:nth-last-of-type(2)
:nth-of-type(n)
p:nth-of-type(2)
:only-of-type
p:only-of-type
:only-child
p:only-child
Selects every <p> element that is the only child of its paren
:optional
input:optional
:out-of-range
input:out-of-range
:read-only
input:read-only
:read-write
input:read-write
:required
input:required
:root
root
:target
#news:target
:valid
input:valid
:visited
a:visited
Previous
Next Chapter
W3SCHOOLS EXAMS
HTML, CSS, JavaScript, PHP, jQuery, Bootstrap and XML Certifications
COLOR PICKER
LEARN MORE:
Color Converter
Google Maps
Animated Buttons
Modal Boxes
Modal Images
Tooltips
Loaders
JS Animations
Progress Bars
Dropdowns
Slideshow
Side Navigation
HTML Includes
Color Palettes
Code Coloring
Home
News
Contact
About
Horizontal
Home
News
Contact
About
Home
News
Contact
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.
Example
<ul>
<li><a
<li><a
<li><a
<li><a
</ul>
href="default.asp">Home</a></li>
href="news.asp">News</a></li>
href="contact.asp">Contact</a></li>
href="about.asp">About</a></li>
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:
You can also set the width of <ul>, and remove the width of <a>, as they will
take up the full width available when displayed as block elements. This will
produce the same result as our previous example:
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;
}
/* Change the link color on hover */
li a:hover {
background-color: #555;
color: white;
}
Try it Yourself
Home
News
Contact
About
Example
.active {
background-color: #4CAF50;
color: white;
}
Try it Yourself
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;
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
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
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
Create a basic horizontal navigation bar with a dark background color and
change the background color of the links when the user moves the mouse over
them:
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;
}
/* Change the link color to #111 (black) on hover */
li a:hover {
background-color: #111;
}
Try it Yourself
Add an "active" class to the current link to let the user know which page he/she
is on:
Home
News
Contact
About
Example
.active {
background-color: #4CAF50;
}
Try it Yourself
Right-Align Links
Right-align links by floating the list items to the right (float:right;):
Home
News
Contact
About
Example
<ul>
<li><a href="#home">Home</a></li>
<li><a href="#news">News</a></li>
<li><a href="#contact">Contact</a></li>
<li style="float:right"><a class="active" href="#about">About</a></li>
</ul>
Try it Yourself
Border Dividers
Add the border-right property to <li> to create link dividers:
Home
News
Contact
About
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
Note: These examples might not work properly on mobile devices.
Home
News
Contact
About
Example
ul {
border: 1px solid #e7e7e7;
background-color: #f3f3f3;
}
li a {
color: #666;
}
Try it Yourself
More Examples
Responsive Topnav
How to use CSS3 media queries to create a responsive top navigation.
Responsive Sidenav
How to use CSS3 media queries to create a responsive side navigation.
Dropdown Navbar
How to add a dropdown menu inside a navigation bar.
CSS Dropdowns
Previous
Next Chapter
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.
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:autoto 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:
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;
}
/* The container <div> - needed to position the dropdown content */
.dropdown {
position: relative;
display: inline-block;
}
/* Dropdown Content (Hidden by Default) */
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}
/* Links inside the dropdown */
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
/* Change color of dropdown links on hover */
.dropdown-content a:hover {background-color: #f1f1f1}
/* Show the dropdown menu on hover */
.dropdown:hover .dropdown-content {
display: block;
}
/* 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.
Previous
Next Chapter
W3SCHOOLS EXAMS
HTML, CSS, JavaScript, PHP, jQuery, Bootstrap and XML Certifications
COLOR PICKER
LEARN MORE:
Color Converter
Google Maps
Animated Buttons
Modal Boxes
Modal Images
Tooltips
Loaders
JS Animations
Progress Bars
Dropdowns
Slideshow
Side Navigation
HTML Includes
Color Palettes
Code Coloring
SHARE THIS PAGE
CSS Tooltip
Previous
Next Chapter
Left
Basic Tooltip
Create a tooltip that appears when the user moves the mouse over an element:
Example
<style>
/* Tooltip container */
.tooltip {
position: relative;
display: inline-block;
border-bottom: 1px dotted black; /* If you want dots under the hoverable text */
}
/* Tooltip text */
.tooltip .tooltiptext {
visibility: hidden;
width: 120px;
background-color: black;
color: #fff;
text-align: center;
padding: 5px 0;
border-radius: 6px;
/* Position the tooltip text - see examples below! */
position: absolute;
z-index: 1;
}
/* Show the tooltip text when you mouse over the tooltip container */
.tooltip:hover .tooltiptext {
visibility: visible;
}
</style>
<div class="tooltip">Hover over me
<span class="tooltiptext">Tooltip text</span>
</div>
Try it Yourself
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> withclass="tooltip".
Positioning Tooltips
In this example, the tooltip is placed to the right (left:105%) of the "hoverable" text (<div>).
Also note thattop:-5px is used to place it in the middle of its container element. We use the
number 5 because the tooltip text has a top and bottom padding of 5px. If you increase its
padding, also increase the value of the topproperty 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.
Right Tooltip
.tooltip .tooltiptext {
top: -5px;
left: 105%;
}
Result:
Hover over me
Try it Yourself
Left Tooltip
.tooltip .tooltiptext {
top: -5px;
right: 105%;
}
Result:
Hover over me
Try it Yourself
If you want the tooltip to appear on top or on the bottom, see examples below. Note that we use
the margin-left property with a value of minus 60 pixels. This is to center the tooltip
above/below the hoverable text. It is set to the half of the tooltip's width (120/2 = 60).
Top Tooltip
.tooltip .tooltiptext {
width: 120px;
bottom: 100%;
left: 50%;
margin-left: -60px; /* Use half of the width (120/2 = 60), to center the tooltip */
}
Result:
Hover over me
Try it Yourself
Bottom Tooltip
.tooltip .tooltiptext {
width: 120px;
top: 100%;
left: 50%;
margin-left: -60px; /* Use half of the width (120/2 = 60), to center the tooltip */
}
Result:
Hover over me
Try it Yourself
Tooltip Arrows
To create an arrow that should appear from a specific side of the tooltip, add "empty" content
after tooltip, with the pseudo-element class ::after together with the content property. The
arrow itself is created using borders. This will make the tooltip look like a speech bubble.
This example demonstrates how to add an arrow to the bottom of the tooltip:
Bottom Arrow
.tooltip .tooltiptext::after {
content: " ";
position: absolute;
top: 100%; /* At the bottom of the tooltip */
left: 50%;
margin-left: -5px;
border-width: 5px;
border-style: solid;
border-color: black transparent transparent transparent;
}
Result:
Hover over me
Try it Yourself
Example Explained
Position the arrow inside the tooltip: top: 100% will place the arrow at the bottom of the
tooltip. left: 50%will center the arrow.
Note: The border-width property specifies the size of the arrow. If you change this, also
change the margin-left value to the same. This will keep the arrow centered.
The border-color is used to transform the content into an arrow. We set the top border to black,
and the rest to transparent. If all sides were black, you would end up with a black square box.
This example demonstrates how to add an arrow to the top of the tooltip. Notice that we set the
bottom border color this time:
Top Arrow
.tooltip .tooltiptext::after {
content: " ";
position: absolute;
bottom: 100%; /* At the top of the tooltip */
left: 50%;
margin-left: -5px;
border-width: 5px;
border-style: solid;
border-color: transparent transparent black transparent;
}
Result:
Hover over me
Try it Yourself
This example demonstrates how to add an arrow to the left of the tooltip:
Left Arrow
.tooltip .tooltiptext::after {
content: " ";
position: absolute;
top: 50%;
right: 100%; /* To the left of the tooltip */
margin-top: -5px;
border-width: 5px;
border-style: solid;
border-color: transparent black transparent transparent;
}
Result:
Hover over me
Try it Yourself
This example demonstrates how to add an arrow to the right of the tooltip:
Right Arrow
.tooltip .tooltiptext::after {
content: " ";
position: absolute;
top: 50%;
left: 100%; /* To the right of the tooltip */
margin-top: -5px;
border-width: 5px;
border-style: solid;
border-color: transparent transparent transparent black;
}
Result:
Hover over me
Try it Yourself
Example
.tooltip .tooltiptext {
opacity: 0;
transition: opacity 1s;
}
.tooltip:hover .tooltiptext {
opacity: 1;
}
Try it Yourself
Note: You will learn more about transitions and animations later in our tutorial.
Previous
Next Chapter
W3SCHOOLS EXAMS
HTML, CSS, JavaScript, PHP, jQuery, Bootstrap and XML Certifications
COLOR PICKER
LEARN MORE:
Color Converter
Google Maps
Animated Buttons
Modal Boxes
Modal Images
Tooltips
Loaders
JS Animations
Progress Bars
Dropdowns
Slideshow
Side Navigation
HTML Includes
Color Palettes
Code Coloring
SHARE THIS PAGE
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="forest.jpg">
<img src="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="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="mountains.jpg">
<img src="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
More Examples
Previous
Next Chapter
W3SCHOOLS EXAMS
HTML, CSS, JavaScript, PHP, jQuery, Bootstrap and XML Certifications
COLOR PICKER
LEARN MORE:
Color Converter
Google Maps
Animated Buttons
Modal Boxes
Modal Images
Tooltips
Loaders
JS Animations
Progress Bars
Dropdowns
Slideshow
Side Navigation
HTML Includes
Color Palettes
Code Coloring
SHARE THIS PAGE
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
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.
Previous
Next Chapter
W3SCHOOLS EXAMS
HTML, CSS, JavaScript, PHP, jQuery, Bootstrap and XML Certifications
COLOR PICKER
LEARN MORE:
Color Converter
Google Maps
Animated Buttons
Modal Boxes
Modal Images
Tooltips
Loaders
JS Animations
Progress Bars
Dropdowns
Slideshow
Side Navigation
HTML Includes
Color Palettes
Code Coloring
SHARE THIS PAGE
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.
In the following example the CSS specifies which part of the "img_navsprites.gif" image to
show:
Example
#home {
width: 46px;
height: 44px;
background: url(img_navsprites.gif) 0 0;
}
Try it Yourself
Example explained:
background: url(img_navsprites.gif) 0 0;
Example
#navlist {
position: relative;
}
#navlist li {
margin: 0;
padding: 0;
list-style: none;
position: absolute;
top: 0;
}
#navlist li, #navlist a {
height: 44px;
display: block;
}
#home {
left: 0px;
width: 46px;
background: url('img_navsprites.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:
#navlist li, #navlist a {height:44px;display:block;} - the height of all the images are 44px
#home {left:0px;width:46px;} - Positioned all the way to the left, and the width of the
image is 46px
#prev {left:63px;width:43px;} - Positioned 63px to the right (#home width 46px + some
extra space between items), and the width is 43px.
Tip: The :hover selector can be used on all elements, not only on links.
Our new image ("img_navsprites_hover.gif") contains three navigation images and three images
to use for hover effects:
Because this is one single image, and not six separate files, there will be no loading delay when
a user hovers over the image.
We only add three lines of code to add the hover effect:
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:
Previous
Next Chapter
W3SCHOOLS EXAMS
HTML, CSS, JavaScript, PHP, jQuery, Bootstrap and XML Certifications
COLOR PICKER
LEARN MORE:
Color Converter
Google Maps
Animated Buttons
Modal Boxes
Modal Images
Tooltips
Loaders
JS Animations
Progress Bars
Dropdowns
Slideshow
Side Navigation
HTML Includes
Color Palettes
Code Coloring
Example
a[target] {
background-color: yellow;
}
Try it Yourself
Example
a[target="_blank"] {
background-color: yellow;
}
Try it Yourself
Example
[title~="flower"] {
border: 5px solid yellow;
}
Try it Yourself
The example above will match elements with title="flower", title="summer
flower", and title="flower new", but not title="my-flower" or title="flowers".
Example
[class|="top"] {
background: yellow;
}
Try it Yourself
Example
[class^="top"] {
background: yellow;
}
Try it Yourself
Example
[class$="test"] {
background: yellow;
}
Try it Yourself
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
Tip: Visit our CSS Forms Tutorial for more examples on how to style forms with
CSS.
CSS Forms
Previous
Next Chapter
Last Name
State
Try it Yourself
Example
input {
width: 100%;
}
Try it Yourself
The example above applies to all <input> elements. If you only want to style a specific input
type, you can use attribute selectors:
etc..
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:
First Name
Last Name
Example
input[type=text] {
width: 100%;
padding: 12px 20px;
margin: 8px 0;
box-sizing: border-box;
}
Try it Yourself
Note that we have set the box-sizing property to border-box. This makes sure that the padding an
borders are included in the total width and height of the elements.
Read more about the box-sizing property in our CSS3 Box Sizing chapter.
Bordered Inputs
Use the border property to change the border size and color, and use the borderradius property to add rounded corners:
First Name
Example
input[type=text] {
border: 2px solid red;
border-radius: 4px;
}
Try it Yourself
If you only want a bottom border, use the border-bottom property:
First Name
Example
input[type=text] {
border: none;
border-bottom: 2px solid red;
}
Try it Yourself
Colored Inputs
Use the background-color property to add a background color to the input, and
the color property to change the text color:
John
Example
input[type=text] {
background-color: #3CBC8D;
color: white;
}
Try it Yourself
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:
Example
input[type=text]:focus {
background-color: lightblue;
}
Try it Yourself
Example
input[type=text]:focus {
border: 3px solid #555;
}
Try it Yourself
Example
input[type=text] {
background-color: white;
background-image: url('searchicon.png');
background-position: 10px 10px;
background-repeat: no-repeat;
padding-left: 40px;
}
Try it Yourself
Example
input[type=text] {
-webkit-transition: width 0.4s ease-in-out;
transition: width 0.4s ease-in-out;
}
input[type=text]:focus {
width: 100%;
}
Try it Yourself
Styling Textareas
Tip: Use the resize property to prevent textareas from being resized (disable the "grabber" in
the bottom right corner):
Example
textarea {
width: 100%;
height: 150px;
padding: 12px 20px;
box-sizing: border-box;
border: 2px solid #ccc;
border-radius: 4px;
background-color: #f8f8f8;
resize: none;
}
Try it Yourself
Previous
Next Chapter
W3SCHOOLS EXAMS
HTML, CSS, JavaScript, PHP, jQuery, Bootstrap and XML Certifications
COLOR PICKER
LEARN MORE:
Color Converter
Google Maps
Animated Buttons
Modal Boxes
Modal Images
Tooltips
Loaders
JS Animations
Progress Bars
Dropdowns
Slideshow
Side Navigation
HTML Includes
Color Palettes
Code Coloring
CSS Counters
Previous
Next Chapter
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
Description
content
counter-increment
counter-reset
CSS3 Introduction
Previous
Next Chapter
CSS3
CSS3 Modules
CSS3 has been split into "modules". It contains the "old CSS specification"
(which has been split into smaller pieces). In addition, new modules are added.
Some of the most important CSS3 modules are:
Selectors
Box Model
Text Effects
2D/3D Transformations
Animations
User Interface
Next Chapter
Browser Support
The numbers in the table specify the first browser version that fully supports the
property.
Numbers followed by -webkit- or -moz- specify the first version that worked
with a prefix.
Property
border-radius
5.0
4.0 -webkit-
9.0
4.0
3.0 -moz-
5.0
3.1 -webkit-
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
Tip: The border-radius property is actually a shorthand property for the border-top
radius,border-top-right-radius, border-bottom-right-radius and border-bo
radiusproperties.
Four values: first value applies to top-left, second value applies to topright, third value applies to bottom-right, and fourth value applies to
bottom-left corner
Three values: first value applies to top-left, second value applies to topright and bottom-left, and third value applies to bottom-right
Two values: first value applies to top-left and bottom-right corner, and
the second value applies to top-right and bottom-left corner
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
You could also create elliptical corners:
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
Description
border-radius
border-top-left-radius
border-top-right-radius
border-bottom-right-radius
border-bottom-left-radius
Browser Support
The numbers in the table specify the first browser version that fully supports the property.
Numbers followed by -webkit-, -moz-, or -o- specify the first version that worked with a prefix.
Property
border-image
16.0
4.0 -webkit-
11.0
15.0
3.5 -moz-
6.0
3.1 -webkit-
The border-image property takes the image and slices it into nine sections, like a tic-tac-toe
board. It then places the corners at the corners, and the middle sections are repeated or stretched
as you specify.
Note: For border-image to work, the element also needs the border property set!
Here, the middle sections of the image are repeated to create the border:
An image as a border!
Here is the code:
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!
Here is the code:
Example
#borderimg {
border: 10px solid transparent;
padding: 15px;
-webkit-border-image: url(border.png) 30 stretch; /* Safari 3.1-5 */
-o-border-image: url(border.png) 30 stretch; /* Opera 11-12.1 */
border-image: url(border.png) 30 stretch;
}
Try it Yourself
Tip: The border-image property is actually a shorthand property for the border-image-source
image-slice, border-image-width, border-image-outset and border-image-repe
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 */
Description
border-image
border-image-source
border-image-slice
border-image-width
border-image-outset
Specifies the amount by which the border image area extends beyond the
border-image-repeat
Previous
Next Chapter
W3SCHOOLS EXAMS
HTML, CSS, JavaScript, PHP, jQuery, Bootstrap and XML Certifications
COLOR PICKER
LEARN MORE:
Color Converter
Google Maps
Animated Buttons
Modal Boxes
Modal Images
Tooltips
Loaders
JS Animations
Progress Bars
Dropdowns
Slideshow
Side Navigation
HTML Includes
Color Palettes
Code Coloring
CSS3 Backgrounds
Previous
Next Chapter
CSS3 Backgrounds
CSS3 contains a few new background properties, which allow greater control of
the background element.
In this chapter you will learn how to add multiple background images to one
element.
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 the
property.
Numbers followed by -webkit-, -moz-, or -o- specify the first version that
worked with a prefix.
Property
background-image
(with multiple backgrounds)
4.0
9.0
3.6
3.1
4.0
1.0 -webkit-
9.0
4.0
3.6 -moz-
4.1
3.0 -webkit-
background-origin
1.0
9.0
4.0
3.0
background-clip
4.0
9.0
4.0
3.0
background-size
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
background properties (as above) or thebackground shorthand property.
The following example uses the background shorthand property (same result
as example above):
Example
#example1 {
background: url(img_flwr.gif) right bottom no-repeat, url(paper.gif) left top
repeat;
}
Try it Yourself
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 contain keyword scales the background image to be as large as possible
(but both its width and its height must fit inside the content area). As such,
depending on the proportions of the background image and the background
positioning area, there may be some areas of the background which are not
covered by the background image.
The cover keyword scales the background image so that the content area is
completely covered by the background image (both its width and height are
equal to or exceed the content area). As such, some parts of the background
image may not be visible in the background positioning area.
The following example illustrates the use of contain and cover:
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
background-size value for each image:
Example
#example1 {
background: url(img_flwr.gif) left top no-repeat, url(img_flwr.gif) right
bottom no-repeat, url(paper.gif) left top repeat;
background-size: 50px, 130px, auto;
}
Try it Yourself
Fill the entire page with the image (no white space)
The following example shows how to do it; Use the html element (the html
element is always at least the height of the browser window). Then set a fixed
and centered background on it. Then adjust its size with the background-size
property:
Example
html {
background: url(img_flower.jpg) no-repeat center fixed;
background-size: cover;
}
Try it Yourself
border-box - the background image starts from the upper left corner of
the border
padding-box - (default) the background image starts from the upper left
corner of the padding edge
content-box - the background image starts from the upper left corner of
the content
Example
#example1 {
border: 10px solid black;
padding: 35px;
background: url(img_flwr.gif);
background-repeat: no-repeat;
background-origin: content-box;
}
Try it Yourself
Example
#example1 {
border: 10px dotted black;
padding: 35px;
background: yellow;
background-clip: content-box;
}
Try it Yourself
Description
background
background-clip
background-image
background-origin
background-size
CSS3 Colors
Previous
CSS3 Colors
CSS supports color names, hexadecimal and RGB colors.
In addition, CSS3 also introduces:
RGBA colors
HSL colors
HSLA colors
opacity
Browser Support
Next Chapter
The numbers in the table specify the first browser version that fully supports
CSS3 color values/property.
Property
RGBA, HSL, and HSLA
4.0
9.0
3.0
3.1
opacity
4.0
9.0
2.0
3.1
RGBA Colors
RGBA color values are an extension of RGB color values with an alpha channel which specifies the opacity for a color.
An RGBA color value is specified with: rgba(red, green, blue, alpha). The alpha
parameter is a number between 0.0 (fully transparent) and 1.0 (fully opaque).
rgba(255,
rgba(255,
rgba(255,
rgba(255,
0,
0,
0,
0,
0,
0,
0,
0,
0.2);
0.4);
0.6);
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.
100%,
100%,
100%,
100%,
30%);
50%);
70%);
90%);
Example
#p1
#p2
#p3
#p4
{background-color:
{background-color:
{background-color:
{background-color:
hsl(120,
hsl(120,
hsl(120,
hsl(120,
100%, 50%);}
100%, 75%);}
100%, 25%);}
60%, 70%);}
/* green */
/* light green */
/* dark green */
/* pastel green */
Try it Yourself
HSLA Colors
HSLA color values are an extension of HSL color values with an alpha channel which specifies the opacity for a color.
100%,
100%,
100%,
100%,
30%,
50%,
70%,
90%,
0.3);
0.3);
0.3);
0.3);
Example
#p1 {background-color:
*/
#p2 {background-color:
opacity */
#p3 {background-color:
opacity */
#p4 {background-color:
opacity */
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)
and 1.0 (fully opaque).
rgb(255,
rgb(255,
rgb(255,
rgb(255,
0,
0,
0,
0,
0);opacity:0.2;
0);opacity:0.4;
0);opacity:0.6;
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 opacity */
#p3 {background-color:rgb(0,0,255);opacity:0.6;} /* blue with opacity */
Try it Yourself
CSS3 Gradients
Previous
Next Chapter
Gradient Background
CSS3 gradients let you display smooth transitions between two or more
specified colors.
Earlier, you had to use images for these effects. However, by using CSS3
gradients you can reduce download time and bandwidth usage. In addition,
elements with gradients look better when zoomed, because the gradient is
generated by the browser.
CSS3 defines two types of gradients:
Browser Support
The numbers in the table specify the first browser version that fully supports the
property.
Numbers followed by -webkit-, -moz-, or -o- specify the first version that
worked with a prefix.
Property
linear-gradient
26.0
10.0 -webkit-
10.0
16.0
3.6 -moz-
6.1
5.1 -webkit-
radial-gradient
26.0
10.0 -webkit-
10.0
16.0
3.6 -moz-
6.1
5.1 -webkit-
repeating-linear-gradient
26.0
10.0 -webkit-
10.0
16.0
3.6 -moz-
6.1
5.1 -webkit-
repeating-radial-gradient
26.0
10.0 -webkit-
10.0
16.0
3.6 -moz-
6.1
5.1 -webkit-
Syntax
background: linear-gradient(direction, color-stop1, color-stop2, ...);
Linear Gradient - Top to Bottom (this is default)
The following example shows a linear gradient that starts at the top. It starts
red, transitioning to yellow:
Example
#grad {
background:
background:
background:
background:
background:
}
Try it Yourself
Linear Gradient - Left to Right
The following example shows a linear gradient that starts from the left. It starts
red, transitioning to yellow:
Example
#grad {
background:
background:
6.0 */
background:
*/
background:
*/
background:
}
Try it Yourself
Linear Gradient - Diagonal
You can make a gradient diagonally by specifying both the horizontal and
vertical starting positions.
The following example shows a linear gradient that starts at top left (and goes
to bottom right). It starts red, transitioning to yellow:
Example
#grad {
background: red; /* For browsers that do not support gradients */
background:
6.0 */
background:
to 12.0 */
background:
3.6 to 15 */
background:
syntax */
}
Try it Yourself
Using Angles
If you want more control over the direction of the gradient, you can define an
angle, instead of the predefined directions (to bottom, to top, to right, to left, to
bottom right, etc.).
Syntax
background: linear-gradient(angle, color-stop1, color-stop2);
The angle is specified as an angle between a horizontal line and the gradient
line.
The following example shows how to use angles on linear gradients:
Example
#grad {
background:
background:
6.0 */
background:
12.0 */
background:
15 */
background:
}
Try it Yourself
Example
#grad {
background:
background:
6.0 */
background:
*/
background:
*/
background:
}
Try it Yourself
The following example shows how to create a linear gradient (from left to right)
with the color of the rainbow and some text:
Gradient Background
Example
#grad {
background: red; /* For browsers that do not support gradients */
/* For Safari 5.1 to 6.0 */
background: -webkit-lineargradient(left,red,orange,yellow,green,blue,indigo,violet);
/* For Opera 11.1 to 12.0 */
background: -o-lineargradient(left,red,orange,yellow,green,blue,indigo,violet);
/* For Fx 3.6 to 15 */
background: -moz-lineargradient(left,red,orange,yellow,green,blue,indigo,violet);
/* Standard syntax */
background: linear-gradient(to right,
red,orange,yellow,green,blue,indigo,violet);
}
Try it Yourself
Using Transparency
CSS3 gradients also support transparency, which can be used to create fading
effects.
To add transparency, we use the rgba() function to define the color stops. The
last parameter in the rgba() function can be a value from 0 to 1, and it defines
the transparency of the color: 0 indicates full transparency, 1 indicates full color
(no transparency).
The following example shows a linear gradient that starts from the left. It starts
fully transparent, transitioning to full color red:
Example
#grad {
background: red; /* For browsers that do not support gradients */
background: -webkit-lineargradient(left,rgba(255,0,0,0),rgba(255,0,0,1)); /*Safari 5.1-6*/
background: -o-lineargradient(right,rgba(255,0,0,0),rgba(255,0,0,1)); /*Opera 11.1-12*/
background: -moz-lineargradient(right,rgba(255,0,0,0),rgba(255,0,0,1)); /*Fx 3.6-15*/
background: linear-gradient(to right, rgba(255,0,0,0),
rgba(255,0,0,1)); /*Standard*/
}
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%, green 20%);
/* 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 20%);
/* Standard syntax */
background: repeating-linear-gradient(red, yellow 10%, green 20%);
}
Try it Yourself
Syntax
background: radial-gradient(shape size at position, start-color, ..., last-color);
By default, shape is ellipse, size is farthest-corner, and position is center.
Radial Gradient - Evenly Spaced Color Stops (this is default)
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 5.1 to 6.0
*/
background: -o-radial-gradient(red, yellow, green); /* For Opera 11.6 to 12.0
*/
background: -moz-radial-gradient(red, yellow, green); /* For Firefox 3.6 to
15 */
background: radial-gradient(red, yellow, green); /* Standard syntax */
}
Try it Yourself
Radial Gradient - Differently Spaced Color Stops
The following example shows a radial gradient with differently spaced color
stops:
Example
#grad {
background: red; /* For browsers that do not support gradients */
background: -webkit-radial-gradient(red 5%, yellow 15%, green 60%); /*
Safari 5.1-6.0 */
background: -o-radial-gradient(red 5%, yellow 15%, green 60%); /* For
Opera 11.6-12.0 */
background: -moz-radial-gradient(red 5%, yellow 15%, green 60%); /* For
Firefox 3.6-15 */
background: radial-gradient(red 5%, yellow 15%, green 60%); /* Standard
syntax */
}
Try it Yourself
Set Shape
The shape parameter defines the shape. It can take the value circle or ellipse.
The default value is ellipse.
The following example shows a radial gradient with the shape of a circle:
Example
#grad {
background: red; /* For browsers that do not support gradients */
background: -webkit-radial-gradient(circle, red, yellow, green); /* Safari */
background: -o-radial-gradient(circle, red, yellow, green); /* Opera 11.6 to
12.0 */
background: -moz-radial-gradient(circle, red, yellow, green); /* Firefox 3.6 to
15 */
background: radial-gradient(circle, red, yellow, green); /* Standard syntax */
}
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, yellow,
black);
/* For Opera 11.6 to 12.0 */
background: -o-radial-gradient(60% 55%, closest-side, red, yellow, black);
/* For Firefox 3.6 to 15 */
background: -moz-radial-gradient(60% 55%, closest-side, red, yellow,
black);
/* Standard syntax */
background: radial-gradient(closest-side at 60% 55%, red, yellow, black);
}
#grad2 {
/* Safari 5.1 to 6.0 */
background: -webkit-radial-gradient(60% 55%, farthest-side, red, yellow,
black);
/* Opera 11.6 to 12.0 */
background: -o-radial-gradient(60% 55%, farthest-side, red, yellow, black);
/* For Firefox 3.6 to 15 */
background: -moz-radial-gradient(60% 55%, farthest-side, red, yellow,
black);
/* Standard syntax */
background: radial-gradient(farthest-side at 60% 55%, red, yellow, black);
}
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%, green 15%);
/* 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 15%);
/* Standard syntax */
background: repeating-radial-gradient(red, yellow 10%, green 15%);
}
Try it Yourself
Next Chapter
Box Shadow
text-shadow
box-shadow
Browser Support
The numbers in the table specify the first browser version that fully supports the
property.
Numbers followed by -webkit- or -moz- specifies the first version that worked
with a prefix.
Property
text-shadow
4.0
10.0
3.5
4.0
box-shadow
10.0
4.0 -webkit-
9.0
4.0
3.5 -moz-
5.1
3.1 -webkit-
h1 {
text-shadow: 2px 2px;
}
Try it Yourself
Next, add a color to the shadow:
Multiple Shadows
To add more than one shadow to the text, you can add a comma-separated list
of shadows.
The following example shows a red and blue neon glow shadow:
Example
div {
box-shadow: 10px 10px;
}
Try it Yourself
Next, add a color to the shadow:
This is a yellow <div> element with a grey box-shadow
Example
div {
box-shadow: 10px 10px grey;
}
Try it Yourself
Next, add a blur effect to the shadow:
This is a yellow <div> element with a blurred, grey box-shadow
Example
div {
box-shadow: 10px 10px 5px grey;
}
Try it Yourself
You can also add shadows to the ::before and ::after pseudo-classes, to create
an interesting effect:
Example
#boxshadow {
position: relative;
box-shadow: 1px 2px 4px rgba(0, 0, 0, .5);
padding: 10px;
background: white;
}
#boxshadow img {
width: 100%;
border: 1px solid #8a4419;
border-style: inset;
}
#boxshadow::after {
content: '';
position: absolute;
z-index: -1; /* hide shadow behind image */
box-shadow: 0 15px 20px rgba(0, 0, 0, 0.3);
width: 70%;
left: 15%; /* one half of the remaining 30% */
height: 100px;
bottom: 0;
}
Try it Yourself
Cards
An example of using the box-shadow property to create paper-like cards:
1
January 1, 2016
Hardanger, Norway
Example
div.card {
width: 250px;
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0,
0.19);
text-align: center;
}
Try it (Text Card) Try it (Image Card)
Property
Description
box-shadow
text-shadow
Previous
Next Chapter
CSS3 Text
Previous
Next Chapter
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
text-overflow
4.0
6.0
7.0
3.1
word-wrap
23.0
5.5
3.5
6.1
word-break
4.0
5.5
15.0
3.1
Example
p.test1 {
white-space: nowrap;
width: 200px;
border: 1px solid #000000;
overflow: hidden;
text-overflow: clip;
}
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 over the element:
Example
div.test:hover {
text-overflow: inherit;
overflow: visible;
}
Try it Yourself
Example
Allow long words to be able to be broken and wrap onto the next line:
p{
word-wrap: break-word;
}
Try it Yourself
Example
p.test1 {
word-break: keep-all;
}
p.test2 {
word-break: break-all;
}
Try it Yourself
Property
Description
text-align-last
text-emphasis
text-justify
text-overflow
word-break
word-wrap
Previous
Next Chapter
Browser Support
The numbers in the table specify the first browser version that fully supports the property.
Property
@font-face
4.0
9.0
3.5
3.2
9.0*
4.0
3.5
3.1
WOFF
9.0
5.0
3.6
5.1
WOFF2
Not supported
36.0
35.0*
Not supported
SVG
Not supported
4.0
Not supported
3.2
EOT
6.0
Not supported
Not supported
Not supported
Tip: Always use lowercase letters for the font URL. Uppercase letters can give unexpected
To use the font for an HTML element, refer to the name of the font (myFirstFont) through
the font-familyproperty:
Example
@font-face {
font-family: myFirstFont;
src: url(sansation_light.woff);
}
div {
font-family: myFirstFont;
}
Try it Yourself
You must add another @font-face rule containing descriptors for bold text:
Example
@font-face {
font-family: myFirstFont;
src: url(sansation_bold.woff);
font-weight: bold;
}
Try it Yourself
The file "sansation_bold.woff" is another font file, that contains the bold characters for the
Sansation font.
Browsers will use this whenever a piece of text with the font-family "myFirstFont" should render
as bold.
This way you can have many @font-face rules for the same font.
Descriptor
Values
Description
font-family
name
src
URL
font-stretch
normal
condensed
ultra-condensed
extra-condensed
semi-condensed
expanded
semi-expanded
extra-expanded
ultra-expanded
font-style
normal
italic
oblique
font-weight
normal
bold
100
200
300
400
500
600
700
800
900
unicode-range
unicode-range
Previous
Next Chapter
W3SCHOOLS EXAMS
HTML, CSS, JavaScript, PHP, jQuery, Bootstrap and XML Certifications
COLOR PICKER
LEARN MORE:
Color Converter
Google Maps
Animated Buttons
Modal Boxes
Modal Images
Tooltips
Loaders
JS Animations
Progress Bars
Dropdowns
Slideshow
Side Navigation
HTML Includes
Color Palettes
Code Coloring
SHARE THIS PAGE
CSS3 2D Transforms
Previous
Next Chapter
CSS3 Transforms
CSS3 transforms allow you to translate, rotate, scale, and skew elements.
A transformation is an effect that lets an element change shape, size and
position.
Property
transform
36.0
4.0 -webkit-
10.0
9.0 -ms-
16.0
3.5 -moz-
3.2 -webkit-
transform-origin
(two-value syntax)
36.0
4.0 -webkit-
10.0
9.0 -ms-
16.0
3.5 -moz-
3.2 -webkit-
CSS3 2D Transforms
In this chapter you will learn about the following 2D transformation methods:
translate()
rotate()
scale()
skewX()
skewY()
matrix()
Example
div {
-ms-transform: translate(50px,100px); /* IE 9 */
-webkit-transform: translate(50px,100px); /* Safari */
transform: translate(50px,100px);
}
Try it Yourself
Example
div {
-ms-transform: rotate(20deg); /* IE 9 */
-webkit-transform: rotate(20deg); /* Safari */
transform: rotate(20deg);
}
Try it Yourself
Using negative values will rotate the element counter-clockwise.
The following example rotates the <div> element counter-clockwise with 20
degrees:
Example
div {
-ms-transform: rotate(-20deg); /* IE 9 */
-webkit-transform: rotate(-20deg); /* Safari */
transform: rotate(-20deg);
}
Try it Yourself
Example
div {
-ms-transform: scale(2,3); /* IE 9 */
-webkit-transform: scale(2,3); /* Safari */
transform: scale(2,3);
}
Try it Yourself
The following example decreases the <div> element to be half of its original
width and height:
Example
div {
-ms-transform: scale(0.5,0.5); /* IE 9 */
-webkit-transform: scale(0.5,0.5); /* Safari */
transform: scale(0.5,0.5);
}
Try it Yourself
The following example skews the <div> element 20 degrees along the X-axis:
Example
div {
-ms-transform: skewX(20deg); /* IE 9 */
-webkit-transform: skewX(20deg); /* Safari */
transform: skewX(20deg);
}
Try it Yourself
Example
div {
-ms-transform: skewY(20deg); /* IE 9 */
-webkit-transform: skewY(20deg); /* Safari */
transform: skewY(20deg);
}
Try it Yourself
Example
div {
-ms-transform: skew(20deg, 10deg); /* IE 9 */
-webkit-transform: skew(20deg, 10deg); /* Safari */
transform: skew(20deg, 10deg);
}
Try it Yourself
If the second parameter is not specified, it has a zero value. So, the following
example skews the <div> element 20 degrees along the X-axis:
Example
div {
-ms-transform: skew(20deg); /* IE 9 */
-webkit-transform: skew(20deg); /* Safari */
transform: skew(20deg);
}
Try it Yourself
The matrix() method combines all the 2D transform methods into one.
The matrix() method take six parameters, containing mathematic functions,
which allows you to rotate, scale, move (translate), and skew elements.
The parameters are as follow:
matrix(scaleX(),skewY(),skewX(),scaleY(),translateX(),translateY()):
Example
div {
-ms-transform: matrix(1, -0.3, 0, 1, 0, 0); /* IE 9 */
Property
Description
transform
transform-origin
2D Transform Methods
Function
Description
matrix(n,n,n,n,n,n)
translate(x,y)
translateX(n)
translateY(n)
scale(x,y)
scaleX(n)
scaleY(n)
rotate(angle)
skew(x-angle,y-angle)
skewX(angle)
skewY(angle)
Previous
Next Chapter
CSS3 3D Transforms
Previous
Next Chapter
CSS3 3D Transforms
CSS3 allows you to format your elements using 3D transformations.
Mouse over the elements below to see the difference between a 2D and a 3D transformation:
2D rotate
3D rotate
36.0
12.0 -webkit-
10.0
16.0
10.0 -moz-
4.0 -webkit-
transform-origin
(three-value syntax)
36.0
12.0 -webkit-
10.0
16.0
10.0 -moz-
4.0 -webkit-
transform-style
36.0
12.0 -webkit-
11.0
16.0
10.0 -moz-
4.0 -webkit-
perspective
36.0
12.0 -webkit-
10.0
16.0
10.0 -moz-
4.0 -webkit-
perspective-origin
36.0
12.0 -webkit-
10.0
16.0
10.0 -moz-
4.0 -webkit-
backface-visibility
36.0
12.0 -webkit-
10.0
16.0
10.0 -moz-
4.0 -webkit-
CSS3 3D Transforms
In this chapter you will learn about the following 3D transformation methods:
rotateX()
rotateY()
rotateZ()
The rotateX() method rotates an element around its X-axis at a given degree:
Example
div {
-webkit-transform: rotateX(150deg); /* Safari */
transform: rotateX(150deg);
}
Try it Yourself
The rotateY() method rotates an element around its Y-axis at a given degree:
Example
div {
-webkit-transform: rotateY(130deg); /* Safari */
transform: rotateY(130deg);
}
Try it Yourself
Example
div {
-webkit-transform: rotateZ(90deg); /* Safari */
transform: rotateZ(90deg);
}
Try it Yourself
Property
Description
transform
transform-origin
transform-style
perspective
perspective-origin
backface-visibility
Defines whether or not an element should be visible when not facing the screen
3D Transform Methods
Function
Description
matrix3d
(n,n,n,n,n,n,n,n,n,n,n,n,n,n,n,n)
translate3d(x,y,z)
Defines a 3D translation
translateX(x)
translateY(y)
translateZ(z)
scale3d(x,y,z)
scaleX(x)
scaleY(y)
scaleZ(z)
rotate3d(x,y,z,angle)
Defines a 3D rotation
rotateX(angle)
rotateY(angle)
rotateZ(angle)
perspective(n)
Previous
Next Chapter
W3SCHOOLS EXAMS
HTML, CSS, JavaScript, PHP, jQuery, Bootstrap and XML Certifications
COLOR PICKER
LEARN MORE:
Color Converter
Google Maps
Animated Buttons
Modal Boxes
Modal Images
Tooltips
Loaders
JS Animations
Progress Bars
Dropdowns
Slideshow
Side Navigation
HTML Includes
Color Palettes
Code Coloring
CSS3 Transitions
Previous
Next Chapter
CSS3 Transitions
CSS3 transitions allows you to change property values smoothly (from one
value to another), over a given duration.
Example: Mouse over the element below to see a CSS3 transition effect:
CSS3
The numbers in the table specify the first browser version that fully supports the
property.
Numbers followed by -webkit-, -moz-, or -o- specify the first version that
worked with a prefix.
Property
transition
26.0
4.0 -webkit-
10.0
16.0
4.0 -moz-
6.1
3.1 -webkit-
transition-delay
26.0
4.0 -webkit-
10.0
16.0
4.0 -moz-
6.1
3.1 -webkit-
transition-duration
26.0
4.0 -webkit-
10.0
16.0
4.0 -moz-
6.1
3.1 -webkit-
transition-property
26.0
4.0 -webkit-
10.0
16.0
4.0 -moz-
6.1
3.1 -webkit-
transition-timing-function
26.0
4.0 -webkit-
10.0
16.0
4.0 -moz-
6.1
3.1 -webkit-
Note: If the duration part is not specified, the transition will have no effect,
because the default value is 0.
The following example shows a 100px * 100px red <div> element. The <div>
element has also specified a transition effect for the width property, with a
duration of 2 seconds:
Example
div {
width: 100px;
height: 100px;
background: red;
-webkit-transition: width 2s; /* Safari */
transition: width 2s;
}
The transition effect will start when the specified CSS property (width) changes
value.
Now, let us specify a new value for the width property when a user mouses over
the <div> element:
Example
div:hover {
width: 300px;
}
Try it Yourself
Notice that when the cursor mouses out of the element, it will gradually change
back to its original style.
Example
div {
-webkit-transition: width 2s, height 4s; /* Safari */
transition: width 2s, height 4s;
}
Try it Yourself
ease - specifies a transition effect with a slow start, then fast, then end
linear - specifies a transition effect with the same speed from start to end
function
The following example shows the some of the different speed curves that can be
used:
Example
#div1
#div2
#div3
#div4
#div5
{transition-timing-function:
{transition-timing-function:
{transition-timing-function:
{transition-timing-function:
{transition-timing-function:
linear;}
ease;}
ease-in;}
ease-out;}
ease-in-out;}
Try it Yourself
The transition-delay property specifies a delay (in seconds) for the transition
effect.
The following example has a 1 second delay before starting:
Example
div {
-webkit-transition-delay: 1s; /* Safari */
transition-delay: 1s;
}
Try it Yourself
Transition + Transformation
The following example also adds a transformation to the transition effect:
Example
div {
-webkit-transition: width 2s, height 2s, -webkit-transform 2s; /* Safari */
transition: width 2s, height 2s, transform 2s;
}
Try it Yourself
Example
div {
transition-property: width;
transition-duration: 2s;
transition-timing-function: linear;
transition-delay: 1s;
}
Try it Yourself
or by using the shorthand property transition:
Example
div {
transition: width 2s linear 1s;
}
Try it Yourself
Property
Description
transition
A shorthand property for setting the four transition properties into a single prop
transition-delay
transition-duration
transition-property
Specifies the name of the CSS property the transition effect is for
transition-timing-function
Previous
Next Chapter
W3SCHOOLS EXAMS
HTML, CSS, JavaScript, PHP, jQuery, Bootstrap and XML Certifications
COLOR PICKER
LEARN MORE:
Color Converter
Google Maps
Animated Buttons
Modal Boxes
Modal Images
Tooltips
Loaders
JS Animations
Progress Bars
Dropdowns
Slideshow
Side Navigation
HTML Includes
Color Palettes
Code Coloring
CSS3 Animations
Previous
Next Chapter
CSS3 Animations
CSS3 animations allows animation of most HTML elements without using
JavaScript or Flash!
CSS3
Animation
Property
@keyframes
43.0
4.0 -webkit-
10.0
16.0
5.0 -moz-
9.0
4.0 -webkit-
animation
43.0
4.0 -webkit-
10.0
16.0
5.0 -moz-
9.0
4.0 -webkit-
Keyframes hold what styles the element will have at certain times.
Example
/* The animation code */
@keyframes example {
from {background-color: red;}
to {background-color: yellow;}
}
/* The element to apply the animation to */
div {
width: 100px;
height: 100px;
background-color: red;
animation-name: example;
animation-duration: 4s;
}
Try it Yourself
Note: If the animation-duration property is not specified, the animation will
have no effect, because the default value is 0.
In the example above we have specified when the style will change by using the
keywords "from" and "to" (which represents 0% (start) and 100% (complete)).
It is also possible to use percent. By using percent, you can add as many style
changes as you like.
The following example will change the background-color of the <div> element
when the animation is 25% complete, 50% complete, and again when the
animation is 100% complete:
Example
/* The animation code */
@keyframes example {
0% {background-color: red;}
25% {background-color: yellow;}
50% {background-color: blue;}
100% {background-color: green;}
}
/* The element to apply the animation to */
div {
width: 100px;
height: 100px;
background-color: red;
animation-name: example;
animation-duration: 4s;
}
Try it Yourself
The following example will change both the background-color and the position
of the <div> element when the animation is 25% complete, 50% complete, and
again when the animation is 100% complete:
Example
/* The animation code */
@keyframes example {
0% {background-color: red; left:0px; top:0px;}
25% {background-color: yellow; left:200px; top:0px;}
50% {background-color: blue; left:200px; top:200px;}
75% {background-color: green; left:0px; top:200px;}
100% {background-color: red; left:0px; top:0px;}
}
/* The element to apply the animation to */
div {
width: 100px;
height: 100px;
position: relative;
background-color: red;
animation-name: example;
animation-duration: 4s;
}
Try it Yourself
Delay an Animation
The animation-delay property specifies a delay for the start of an animation.
The following example has a 2 seconds delay before starting the animation:
Example
div {
width: 100px;
height: 100px;
position: relative;
background-color: red;
animation-name: example;
animation-duration: 4s;
animation-delay: 2s;
}
Try it Yourself
Example
div {
width: 100px;
height: 100px;
position: relative;
background-color: red;
animation-name: example;
animation-duration: 4s;
animation-iteration-count: 3;
}
Try it Yourself
The following example uses the value "infinite" to make the animation continue
for ever:
Example
div {
width: 100px;
height: 100px;
position: relative;
background-color: red;
animation-name: example;
animation-duration: 4s;
animation-iteration-count: infinite;
}
Try it Yourself
Example
div {
width: 100px;
height: 100px;
position: relative;
background-color: red;
animation-name: example;
animation-duration: 4s;
animation-iteration-count: 3;
animation-direction: reverse;
}
Try it Yourself
The following example uses the value "alternate" to make the animation first
run forward, then backward, then forward:
Example
div {
width: 100px;
height: 100px;
position: relative;
background-color: red;
animation-name: example;
animation-duration: 4s;
animation-iteration-count: 3;
animation-direction: alternate;
}
Try it Yourself
ease - specifies an animation with a slow start, then fast, then end slowly
(this is default)
linear - specifies an animation with the same speed from start to end
The following example shows the some of the different speed curves that can be
used:
Example
#div1
#div2
#div3
#div4
#div5
{animation-timing-function:
{animation-timing-function:
{animation-timing-function:
{animation-timing-function:
{animation-timing-function:
linear;}
ease;}
ease-in;}
ease-out;}
ease-in-out;}
Try it Yourself
Example
div {
animation-name: example;
animation-duration: 5s;
animation-timing-function: linear;
animation-delay: 2s;
animation-iteration-count: infinite;
animation-direction: alternate;
}
Try it Yourself
The same animation effect as above can be achieved by using the
shorthand animation property:
Example
div {
animation: example 5s linear 2s infinite alternate;
}
Try it Yourself
Property
Description
@keyframes
animation
animation-delay
animation-direction
animation-duration
animation-fill-mode
Specifies a style for the element when the animation is not pla
is finished, or when it has a delay)
animation-iteration-count
animation-name
animation-play-state
animation-timing-function
Previous
Next Chapter
CSS Images
Previous
Next Chapter
Rounded Images
Example
Rounded Image:
img {
border-radius: 8px;
}
Try it Yourself
Example
Circled Image:
img {
border-radius: 50%;
}
Try it Yourself
Thumbnail Images
Use the border property to create thumbnail images.
Thumbnail Image:
Example
img {
border: 1px solid #ddd;
border-radius: 4px;
padding: 5px;
}
<img src="paris.jpg" alt="Paris">
Try it Yourself
Thumbnail Image as Link:
Example
a{
display: inline-block;
border: 1px solid #ddd;
border-radius: 4px;
padding: 5px;
transition: 0.3s;
}
a:hover {
box-shadow: 0 0 2px 1px rgba
(0, 140, 186, 0.5);
}
<a href="paris.jpg">
<img src="paris.jpg" alt="Paris">
</a>
Try it Yourself
Responsive Images
Responsive images will automatically adjust to fit the size of the screen.
If you want an image to scale down if it has to, but never scale up to be larger
than its original size, add the following:
Example
img {
max-width: 100%;
height: auto;
}
Try it Yourself
Tip: Read more about Responsive Web Design in our CSS RWD Tutorial.
Image Text
How to position text in an image:
Example
Bottom Left
Top Left
Top Right
Bottom Right
Centered
Try it Yourself:
Top Left Top Right Bottom Left Bottom Right Centered
Example
div.polaroid {
width: 80%;
background-color: white;
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
}
img {width: 100%}
div.container {
text-align: center;
padding: 10px 20px;
}
Try it Yourself
Image Filters
The CSS filter property adds visual effects (like blur and saturation) to an
element.
Note: The filter property is not supported in Internet Explorer, Edge 12, or
Safari 5.1 and earlier.
Example
Change the color of all images to black and white (100% gray):
img {
-webkit-filter: grayscale(100%); /* Chrome, Safari, Opera */
filter: grayscale(100%);
}
Try it Yourself
Tip: Go to our CSS filter Reference to learn more about CSS filters.
Example
.responsive {
padding: 0 6px;
float: left;
width: 24.99999%;
}
@media only screen and (max-width: 700px){
.responsive {
width: 49.99999%;
margin: 6px 0;
}
}
@media only screen and (max-width: 500px){
.responsive {
width: 100%;
}
}
Try it Yourself
Tip: Read more about Responsive Web Design in our CSS RWD Tutorial.
First, use CSS to create a modal window (dialog box), and hide it by default.
Then, use a JavaScript to show the modal window and to display the image
inside the modal, when a user clicks on the image:
Example
// Get the modal
var modal = document.getElementById('myModal');
// Get the image and insert it inside the modal - use its "alt" text as a caption
var img = document.getElementById('myImg');
var modalImg = document.getElementById("img01");
var captionText = document.getElementById("caption");
img.onclick = function(){
modal.style.display = "block";
modalImg.src = this.src;
modalImg.alt = this.alt;
captionText.innerHTML = this.alt;
}
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
}
Try it Yourself
Previous
Next Chapter
CSS Buttons
Previous
Next Chapter
Example
.button {
background-color: #4CAF50; /* Green */
border: none;
color: white;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
}
Try it Yourself
Button Colors
Green Blue Red Gray Black
Example
.button1
.button2
.button3
.button4
.button5
{background-color:
{background-color:
{background-color:
{background-color:
{background-color:
#4CAF50;} /* Green */
#008CBA;} /* Blue */
#f44336;} /* Red */
#e7e7e7; color: black;} /* Gray */
#555555;} /* Black */
Try it Yourself
Button Sizes
10px 12px 16px 20px 24px
Use the font-size property to change the font size of a button:
Example
.button1
.button2
.button3
.button4
.button5
{font-size:
{font-size:
{font-size:
{font-size:
{font-size:
10px;}
12px;}
16px;}
20px;}
24px;}
Try it Yourself
Use the padding property to change the padding of a button:
10px 24px 12px 28px 14px 40px 32px 16px 16px
Example
.button1
.button2
.button3
.button4
.button5
{padding:
{padding:
{padding:
{padding:
{padding:
10px 24px;}
12px 28px;}
14px 40px;}
32px 16px;}
16px;}
Try it Yourself
Rounded Buttons
2px 4px 8px 12px 50%
Use the border-radius property to add rounded corners to a button:
Example
.button1
.button2
.button3
.button4
.button5
{border-radius:
{border-radius:
{border-radius:
{border-radius:
{border-radius:
2px;}
4px;}
8px;}
12px;}
50%;}
Try it Yourself
Example
.button1 {
background-color: white;
color: black;
border: 2px solid #4CAF50; /* Green */
}
...
Try it Yourself
Hoverable Buttons
Example
.button {
-webkit-transition-duration: 0.4s; /* Safari */
transition-duration: 0.4s;
}
.button:hover {
background-color: #4CAF50; /* Green */
color: white;
}
...
Try it Yourself
Shadow Buttons
Shadow Button Shadow on hover
Use the box-shadow property to add shadows to a button:
Example
.button1 {
box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2), 0 6px 20px 0 rgba(0,0,0,0.19);
}
.button2:hover {
box-shadow: 0 12px 16px 0 rgba(0,0,0,0.24), 0 17px 50px 0 rgba(0,0,0,0.19);
}
Try it Yourself
Disabled Buttons
Normal Button Disabled Button
Use the opacity property to add transparency to a button (creates a "disabled"
look).
Tip: You can also add the cursor property with a value of "not-allowed", which
will display a "no parking sign" when you mouse over the button:
Example
.disabled {
opacity: 0.6;
cursor: not-allowed;
}
Try it Yourself
Button Width
250px
50% 100%
By default, the size of the button is determined by its text content (as wide as
its content). Use the widthproperty to change the width of a button:
Example
.button1 {width: 250px;}
.button2 {width: 50%;}
.button3 {width: 100%;}
Try it Yourself
Button Groups
ButtonButtonButtonButton
Remove margins and add float:left to each button to create a button group:
Example
.button {
float: left;
}
Try it Yourself
Example
.button {
float: left;
border: 1px solid green
}
Try it Yourself
Animated Buttons
Example
Add an arrow on hover:
Hover
Try it Yourself
Example
Add a "ripple" effect on click:
Click
Try it Yourself
Example
Add a "pressed" effect on click:
Click
Try it Yourself
Previous
Next Chapter
W3SCHOOLS EXAMS
LEARN MORE:
Color Converter
Google Maps
Animated Buttons
Modal Boxes
Modal Images
Tooltips
Loaders
JS Animations
Progress Bars
Dropdowns
Slideshow
Side Navigation
HTML Includes
Color Palettes
Code Coloring
SHARE THIS PAGE
Next Chapter
Simple Pagination
If you have a website with lots of pages, you may wish to add some sort of
pagination to each page:
Example
ul.pagination {
display: inline-block;
padding: 0;
margin: 0;
}
ul.pagination li {display: inline;}
ul.pagination li a {
color: black;
float: left;
padding: 8px 16px;
text-decoration: none;
}
Try it Yourself
Highlight the current page with an .active class, and use the :hover selector to
change the color of each page link when moving the mouse over them:
Example
ul.pagination li a.active {
background-color: #4CAF50;
color: white;
}
ul.pagination li a:hover:not(.active) {background-color: #ddd;}
Try it Yourself
Add the border-radius property if you want a rounded "active" and "hover"
button:
Example
ul.pagination li a {
border-radius: 5px;
}
ul.pagination li a.active {
border-radius: 5px;
}
Try it Yourself
Add the transition property to the page links to create a transition effect on
hover:
Example
ul.pagination li a {
transition: background-color .3s;
}
Try it Yourself
Bordered Pagination
Example
ul.pagination li a {
border: 1px solid #ddd; /* Gray */
}
Try it Yourself
Rounded Borders
Tip: Add rounded borders to your first and last link in the pagination:
Example
.pagination li:first-child a {
border-top-left-radius: 5px;
border-bottom-left-radius: 5px;
}
.pagination li:last-child a {
border-top-right-radius: 5px;
border-bottom-right-radius: 5px;
}
Try it Yourself
Example
ul.pagination li a {
margin: 0 4px; /* 0 is for top and bottom. Feel free to change it */
}
Try it Yourself
Pagination Size
Example
ul.pagination li a {
font-size: 22px;
}
Try it Yourself
Centered Pagination
To center the pagination, wrap a container element around it (like <div>), and
add text-align:center
Example
div.center {
text-align: center;
}
Try it Yourself
More Examples
Example
Try it Yourself
Breadcrumbs
Home
Pictures
Summer 15
Italy
Example
ul.breadcrumb {
padding: 8px 16px;
list-style: none;
background-color: #eee;
}
ul.breadcrumb li {display: inline;}
ul.breadcrumb li+li:before {
padding: 8px;
color: black;
content: "/\00a0";
}
Try it Yourself
Previous
Next Chapter
Next Chapter
Browser Support
The numbers in the table specify the first browser version that fully supports the
property.
Numbers followed by -webkit- or -moz- specify the first version that worked
with a prefix.
Property
column-count
50.0
4.0 -webkit-
10.0
2.0 -moz-
9.0
3.1 -webkit-
column-gap
50.0
4.0 -webkit-
10.0
2.0 -moz-
9.0
3.1 -webkit-
column-rule
50.0
4.0 -webkit-
10.0
2.0 -moz-
9.0
3.1 -webkit-
column-rule-color
50.0
4.0 -webkit-
10.0
2.0 -moz-
9.0
3.1 -webkit-
column-rule-style
50.0
4.0 -webkit-
10.0
2.0 -moz-
9.0
3.1 -webkit-
column-rule-width
50.0
4.0 -webkit-
10.0
2.0 -moz-
9.0
3.1 -webkit-
column-width
50.0
4.0 -webkit-
10.0
2.0 -moz-
9.0
3.1 -webkit-
column-count
column-gap
column-rule-style
column-rule-width
column-rule-color
column-rule
column-span
column-width
Example
div {
-webkit-column-count: 3; /* Chrome, Safari, Opera */
-moz-column-count: 3; /* Firefox */
column-count: 3;
}
Try it Yourself
Example
div {
-webkit-column-gap: 40px; /* Chrome, Safari, Opera */
-moz-column-gap: 40px; /* Firefox */
column-gap: 40px;
}
Try it Yourself
Example
div {
-webkit-column-rule-style: solid; /* Chrome, Safari, Opera */
-moz-column-rule-style: solid; /* Firefox */
column-rule-style: solid;
}
Try it Yourself
The column-rule-width property specifies the width of the rule between
columns:
Example
div {
-webkit-column-rule-width: 1px; /* Chrome, Safari, Opera */
-moz-column-rule-width: 1px; /* Firefox */
column-rule-width: 1px;
}
Try it Yourself
The column-rule-color property specifies the color of the rule between
columns:
Example
div {
-webkit-column-rule-color: lightblue; /* Chrome, Safari, Opera */
-moz-column-rule-color: lightblue; /* Firefox */
column-rule-color: lightblue;
}
Try it Yourself
The column-rule property is a shorthand property for setting all the columnrule-* properties above.
The following example sets the width, style, and color of the rule between
columns:
Example
div {
-webkit-column-rule: 1px solid lightblue; /* Chrome, Safari, Opera */
-moz-column-rule: 1px solid lightblue; /* Firefox */
column-rule: 1px solid lightblue;
}
Try it Yourself
Example
h2 {
-webkit-column-span: all; /* Chrome, Safari, Opera */
column-span: all;
}
Try it Yourself
Example
div {
-webkit-column-width: 100px; /* Chrome, Safari, Opera */
column-width: 100px;
}
Try it Yourself
Property
Description
column-count
column-fill
column-gap
column-rule
column-rule-color
column-rule-style
column-rule-width
column-span
column-width
columns
Previous
Next Chapter
Next Chapter
resize
outline-offset
Browser Support
The numbers in the table specify the first browser version that fully supports the
property.
Numbers followed by -webkit- or -moz- specify the first version that worked
with a prefix.
Property
resize
4.0
Not supported
5.0
4.0 -moz-
4.0
outline-offset
4.0
Not supported
5.0
4.0 -moz-
4.0
CSS3 Resizing
The resize property specifies whether or not an element should be resizable by
the user.
This div element is resizable by the user (works in Chrome, Firefox, Safari and
Opera).
The following example lets the user resize only the width of a <div> element:
Example
div {
resize: horizontal;
overflow: auto;
}
Try it Yourself
The following example lets the user resize only the height of a <div> element:
Example
div {
resize: vertical;
overflow: auto;
}
Try it Yourself
The following example lets the user resize both the height and the width of a
<div> element:
Example
div {
resize: both;
overflow: auto;
}
Try it Yourself
Example
div {
border: 1px solid black;
outline: 1px solid red;
outline-offset: 15px;
}
Try it Yourself
Property
Description
box-sizing
nav-down
nav-index
nav-left
nav-right
nav-up
outline-offset
resize
Previous
Next Chapter
Browser Support
The numbers in the table specify the first browser version that fully supports the
property.
Numbers followed by -webkit- or -moz- specify the first version that worked
with a prefix.
Property
box-sizing
10.0
8.0
29.0
5.1
4.0 -webkit-
2.0 -moz-
3.1 -webkit-
The two <div> elements above end up with different sizes in the result
(because div2 has a padding specified):
Example
.div1 {
width: 300px;
height: 100px;
border: 1px solid blue;
}
.div2 {
width: 300px;
height: 100px;
padding: 50px;
Try it Yourself
So, for a long time web developers have specified a smaller width value than
they wanted, because they had to subtract out the padding and borders.
With CSS3, the box-sizing property solves this problem.
Hooray!
Here is the same example as above, with box-sizing: border-box; added to both
<div> elements:
Example
.div1 {
width: 300px;
height: 100px;
border: 1px solid blue;
box-sizing: border-box;
}
.div2 {
width: 300px;
height: 100px;
padding: 50px;
border: 1px solid red;
box-sizing: border-box;
}
Try it Yourself
Since the result of using the box-sizing: border-box; is so much better, many
developers want all elements on their pages to work this way.
The code below ensures that all elements are sized in this more intuitive way.
Many browsers already use box-sizing: border-box; for many form elements
(but not all - which is why inputs and text areas look different at width:
100%;).
Applying this to all elements is safe and wise:
Example
*{
box-sizing: border-box;
}
Try it Yourself
Previous
Next Chapter
CSS3 Flexbox
Flexible boxes, or flexbox, is a new layout mode in CSS3.
Use of flexbox ensures that elements behave predictably when the page layout must
accommodate different screen sizes and different display devices.
For many applications, the flexible box model provides an improvement over the block model in
that it does not use floats, nor do the flex container's margins collapse with the margins of its
contents.
Browser Support
The numbers in the table specify the first browser version that fully supports the feature.
Numbers followed by -webkit- or -moz- specify the first version that worked with a prefix.
Property
Basic support
(single-line flexbox)
29.0
21.0 -webkit-
11.0
22.0
18.0 -moz-
6.1 -webkit-
Multi-line flexbox
29.0
21.0 -webkit-
11.0
28.0
6.1 -webkit-
Example
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
display: -webkit-flex;
display: flex;
width: 400px;
height: 250px;
background-color: lightgrey;
}
.flex-item {
background-color: cornflowerblue;
width: 100px;
height: 100px;
margin: 10px;
}
</style>
</head>
<body>
<div class="flex-container">
<div class="flex-item">flex item 1</div>
<div class="flex-item">flex item 2</div>
<div class="flex-item">flex item 3</div>
</div>
</body>
</html>
Try it Yourself
It is also possible to change the direction of the flex line.
If we set the direction property to rtl (right-to-left), the text is drawn right to left, and also the
flex line changes direction, which will change the page layout:
Example
body {
direction: rtl;
}
.flex-container {
display: -webkit-flex;
display: flex;
width: 400px;
height: 250px;
background-color: lightgrey;
}
.flex-item {
background-color: cornflowerblue;
width: 100px;
height: 100px;
margin: 10px;
}
Try it Yourself
Flex Direction
The flex-direction property specifies the direction of the flexible items inside the flex
container. The default value of flex-direction is row (left-to-right, top-to-bottom).
The other values are as follows:
row-reverse
column
column-reverse
- If the writing-mode (direction) is left to right, the flex items will be laid
out right to left
- If the writing system is horizontal, the flex items will be laid out vertically
- Same as column, but reversed
The following example shows the result of using the row-reverse value:
Example
.flex-container {
display: -webkit-flex;
display: flex;
-webkit-flex-direction: row-reverse;
flex-direction: row-reverse;
width: 400px;
height: 250px;
background-color: lightgrey;
}
Try it Yourself
The following example shows the result of using the column value:
Example
.flex-container {
display: -webkit-flex;
display: flex;
-webkit-flex-direction: column;
flex-direction: column;
width: 400px;
height: 250px;
background-color: lightgrey;
}
Try it Yourself
The following example shows the result of using the column-reverse value:
Example
.flex-container {
display: -webkit-flex;
display: flex;
-webkit-flex-direction: column-reverse;
flex-direction: column-reverse;
width: 400px;
height: 250px;
background-color: lightgrey;
}
Try it Yourself
flex-start
flex-end
center
space-between
space-around
- Items are positioned with space before, between, and after the lines
The following example shows the result of using the flex-end value:
Example
.flex-container {
display: -webkit-flex;
display: flex;
-webkit-justify-content: flex-end;
justify-content: flex-end;
width: 400px;
height: 250px;
background-color: lightgrey;
}
Try it Yourself
The following example shows the result of using the center value:
Example
.flex-container {
display: -webkit-flex;
display: flex;
-webkit-justify-content: center;
justify-content: center;
width: 400px;
height: 250px;
background-color: lightgrey;
}
Try it Yourself
The following example shows the result of using the space-between value:
Example
.flex-container {
display: -webkit-flex;
display: flex;
-webkit-justify-content: space-between;
justify-content: space-between;
width: 400px;
height: 250px;
background-color: lightgrey;
}
Try it Yourself
The following example shows the result of using the space-around value:
Example
.flex-container {
display: -webkit-flex;
display: flex;
-webkit-justify-content: space-around;
justify-content: space-around;
width: 400px;
height: 250px;
background-color: lightgrey;
}
Try it Yourself
stretch
flex-start
flex-end
center
baseline
The following example shows the result of using the stretch value (this is the default value):
Example
.flex-container {
display: -webkit-flex;
display: flex;
-webkit-align-items: stretch;
align-items: stretch;
width: 400px;
height: 250px;
background-color: lightgrey;
}
Try it Yourself
The following example shows the result of using the flex-start value:
Example
.flex-container {
display: -webkit-flex;
display: flex;
-webkit-align-items: flex-start;
align-items: flex-start;
width: 400px;
height: 250px;
background-color: lightgrey;
}
Try it Yourself
The following example shows the result of using the flex-end value:
Example
.flex-container {
display: -webkit-flex;
display: flex;
-webkit-align-items: flex-end;
align-items: flex-end;
width: 400px;
height: 250px;
background-color: lightgrey;
}
Try it Yourself
The following example shows the result of using the center value:
Example
.flex-container {
display: -webkit-flex;
display: flex;
-webkit-align-items: center;
align-items: center;
width: 400px;
height: 250px;
background-color: lightgrey;
}
Try it Yourself
The following example shows the result of using the baseline value:
Example
.flex-container {
display: -webkit-flex;
display: flex;
-webkit-align-items: baseline;
align-items: baseline;
width: 400px;
height: 250px;
background-color: lightgrey;
}
Try it Yourself
nowrap
wrap
wrap-reverse
The following example shows the result of using the nowrap value (this is the default value):
Example
.flex-container {
display: -webkit-flex;
display: flex;
-webkit-flex-wrap: nowrap;
flex-wrap: nowrap;
width: 300px;
height: 250px;
background-color: lightgrey;
}
Try it Yourself
The following example shows the result of using the wrap value:
Example
.flex-container {
display: -webkit-flex;
display: flex;
-webkit-flex-wrap: wrap;
flex-wrap: wrap;
width: 300px;
height: 250px;
background-color: lightgrey;
}
Try it Yourself
The following example shows the result of using the wrap-reverse value:
Example
.flex-container {
display: -webkit-flex;
display: flex;
-webkit-flex-wrap: wrap-reverse;
flex-wrap: wrap-reverse;
width: 300px;
height: 250px;
background-color: lightgrey;
}
Try it Yourself
stretch
flex-start
flex-end
center
space-between
space-around
- Lines are evenly distributed in the flex container, with half-size spaces
on either end
The following example shows the result of using the center value:
Example
.flex-container {
display: -webkit-flex;
display: flex;
-webkit-flex-wrap: wrap;
flex-wrap: wrap;
-webkit-align-content: center;
align-content: center;
width: 300px;
height: 300px;
background-color: lightgrey;
}
Try it Yourself
Example
.flex-item {
background-color: cornflowerblue;
width: 100px;
height: 100px;
margin: 10px;
}
.first {
-webkit-order: -1;
order: -1;
}
Try it Yourself
Margin
Setting margin: auto; will absorb extra space. It can be used to push flex items into different
positions.
In the following example we set margin-right: auto; on the first flex item. This will cause all
the extra space to be absorbed to the right of that element:
Example
.flex-item {
background-color: cornflowerblue;
width: 75px;
height: 75px;
margin: 10px;
}
.flex-item:first-child {
margin-right: auto;
}
Try it Yourself
Perfect Centering
In the following example we will solve an almost daily problem: perfect centering.
It is very easy with flexbox. Setting margin: auto; will make the item perfectly centered in
both axis:
Example
.flex-item {
background-color: cornflowerblue;
width: 75px;
height: 75px;
margin: auto;
}
Try it Yourself
align-self
The align-self property of flex items overrides the flex container's align-items property for
that item. It has the same possible values as the align-items property.
The following example sets different align-self values to each flex item:
Example
.flex-item {
background-color: cornflowerblue;
width: 60px;
min-height: 100px;
margin: 10px;
}
.item1 {
-webkit-align-self: flex-start;
align-self: flex-start;
}
.item2 {
-webkit-align-self: flex-end;
align-self: flex-end;
}
.item3 {
-webkit-align-self: center;
align-self: center;
}
.item4 {
-webkit-align-self: baseline;
align-self: baseline;
}
.item5 {
-webkit-align-self: stretch;
align-self: stretch;
}
Try it Yourself
flex
The flex property specifies the length of the flex item, relative to the rest of the flex items inside
the same container.
In the following example, the first flex item will consume 2/4 of the free space, and the other two
flex items will consume 1/4 of the free space each:
Example
.flex-item {
background-color: cornflowerblue;
margin: 10px;
}
.item1 {
-webkit-flex: 2;
flex: 2;
}
.item2 {
-webkit-flex: 1;
flex: 1;
}
.item3 {
-webkit-flex: 1;
flex: 1;
}
Try it Yourself
More Examples
Create a responsive website with flexbox
This example demonstrates how to create a responsive website layout with flexbox.
Property
Description
display
flex-direction
justify-content
Horizontally aligns the flex items when the items do not use all available space
axis
align-items
Vertically aligns the flex items when the items do not use all available space on
flex-wrap
Specifies whether the flex items should wrap or not, if there is not enough room
one flex line
align-content
flex-flow
order
Specifies the order of a flexible item relative to the rest of the flex items inside
container
align-self
flex
Specifies the length of a flex item, relative to the rest of the flex items inside th
container
Previous
Next Chapter
W3SCHOOLS EXAMS
HTML, CSS, JavaScript, PHP, jQuery, Bootstrap and XML Certifications
COLOR PICKER
LEARN MORE:
Color Converter
Google Maps
Animated Buttons
Modal Boxes
Modal Images
Tooltips
Loaders
JS Animations
Progress Bars
Dropdowns
Slideshow
Side Navigation
HTML Includes
Color Palettes
Code Coloring
Media queries in CSS3 extend the CSS2 media types idea: Instead of looking for
a type of device, they look at the capability of the device.
Media queries can be used to check many things, such as:
resolution
Using media queries are a popular technique for delivering a tailored style sheet
to tablets, iPhone, and Androids.
Browser Support
The numbers in the table specifies the first browser version that fully supports
the @media rule.
Property
@media
21.0
9.0
3.5
4.0
The result of the query is true if the specified media type matches the type of
device the document is being displayed on and all expressions in the media
query are true. When a media query is true, the corresponding style sheet or
style rules are applied, following the normal cascading rules.
Unless you use the not or only operators, the media type is optional and
the all type will be implied.
You can also have different stylesheets for different media:
Description
all
screen
speech
One way to use media queries is to have an alternate CSS section right inside
your style sheet.
The following example changes the background-color to lightgreen if the
viewport is 480 pixels wide or wider (if the viewport is less than 480 pixels, the
background-color will be pink):
Example
@media screen and (min-width: 480px) {
body {
background-color: lightgreen;
}
}
Try it Yourself
The following example shows a menu that will float to the left of the page if the
viewport is 480 pixels wide or wider (if the viewport is less than 480 pixels, the
menu will be on top of the content):
Example
@media screen and (min-width: 480px) {
#leftsidebar {width: 200px; float: left;}
#main {margin-left:216px;}
}
Try it Yourself
Previous
Next Chapter
Example 1
<!DOCTYPE html>
<html>
<head>
<style>
ul {
list-style-type: none;
}
ul li a {
color: green;
text-decoration: none;
padding: 3px;
display: block;
}
</style>
</head>
<body>
<ul>
<li><a data-email="johndoe@example.com" hre
f="mailto:johndoe@example.com">John Doe</a></li>
<li><a data-email="marymoe@example.com" hre
f="mailto:marymoe@example.com">Mary Moe</a></li>
<li><a dataemail="amandapanda@example.com"href="mailto:amandapanda@example.com">
Amanda Panda</a></li>
</ul>
</body>
</html>
Try it Yourself
Notice the data-email attribute. In HTML5, we can use attributes prefixed
with data- to store information. We will use the data- attribute later.
Example 2
@media screen and (max-width: 699px) and (min-width: 520px) {
ul li a {
padding-left: 30px;
background: url(email-icon.png) left center no-repeat;
}
}
Try it Yourself
Example 3
@media screen and (max-width: 1000px) and (min-width: 700px) {
ul li a:before {
content: "Email: ";
font-style: italic;
color: #666666;
}
}
Try it Yourself
Example 4
@media screen and (min-width: 1001px) {
ul li a:after {
content: " (" attr(data-email) ")";
font-size: 12px;
font-style: italic;
color: #666666;
}
}
Try it Yourself
Here, we do not have to write an additional media query block, we can just
append an additional media query to our already existing one using a comma
(this will behave like an OR operator):
Example 5
@media screen and (max-width: 699px) and (min-width: 520px), (min-width:
1151px) {
ul li a {
padding-left: 30px;
background: url(email-icon.png) left center no-repeat;
}
}
Try it Yourself
More Examples
Use the list of email links on a sidebar in a web page
This example puts the list of email links into the left sidebar of a webpage.
Previous
Next Chapter