W3schools: CSS Introduction
W3schools: CSS Introduction
asp
w3schools.com
THE WORLD'S LARGEST WEB DEVELOPER SITE
CSS Introduction
❮ Previous Next ❯
What is CSS?
CSS stands for Cascading Style Sheets
CSS describes how HTML elements are to be displayed on screen, paper, or in
other media
CSS saves a lot of work. It can control the layout of multiple web pages all at once
External stylesheets are stored in CSS files
1 of 8 07/03/2017 9:20
CSS Introduction https://www.w3schools.com/css/css_intro.asp
Welcome to My Homepage
Use the menu to select different Stylesheets
Stylesheet 1
Stylesheet 2
Stylesheet 3
Stylesheet 4
No Stylesheet
No Styles
This page uses DIV elements to group different sections of the
HTML page. Click here to see how the page looks like with no
2 of 8 07/03/2017 9:20
CSS Introduction https://www.w3schools.com/css/css_intro.asp
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
When tags like <font>, and color attributes were added to the HTML 3.2 specification, it
started a nightmare for web developers. Development of large websites, where fonts and
color information were added to every single page, became a long and expensive process.
To solve this problem, the World Wide Web Consortium (W3C) created CSS.
With an external stylesheet file, you can change the look of an entire website by changing
just one file!
❮ Previous Next ❯
3 of 8 07/03/2017 9:20
CSS Syntax and Selectors https://www.w3schools.com/css/css_syntax.asp
w3schools.com
THE WORLD'S LARGEST WEB DEVELOPER SITE
MT4 MultiTerminal
Ads by Google
CSS Syntax
A CSS rule-set consists of a selector and a declaration block:
Each declaration includes a CSS property name and a value, separated by a colon.
A CSS declaration always ends with a semicolon, and declaration blocks are surrounded by
curly braces.
In the following example all <p> elements will be center-aligned, with a red text color:
Example
p {
color: red;
text-align: center;
}
Try it Yourself »
1 of 10 07/03/2017 9:21
CSS Syntax and Selectors https://www.w3schools.com/css/css_syntax.asp
CSS Selectors
CSS selectors are used to "find" (or select) HTML elements based on their element name,
id, class, attribute, and more.
You can select all <p> elements on a page like this (in this case, all <p> elements will be
center-aligned, with a red text color):
Example
p {
text-align: center;
color: red;
}
Try it Yourself »
The id Selector
The id selector uses the id attribute of an HTML element to select a specific element.
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
2 of 10 07/03/2017 9:21
CSS Syntax and Selectors https://www.w3schools.com/css/css_syntax.asp
#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 center-aligned:
Example
p.center {
text-align: center;
color: red;
}
Try it Yourself »
3 of 10 07/03/2017 9:21
CSS Syntax and Selectors https://www.w3schools.com/css/css_syntax.asp
In the example below, the <p> element will be styled according to class="center" and to
class="large":
Example
Try it Yourself »
Grouping Selectors
If you have elements with the same style definitions, like this:
h1 {
text-align: center;
color: red;
}
h2 {
text-align: center;
color: red;
}
p {
text-align: center;
color: red;
}
In the example below we have grouped the selectors from the code above:
Example
h1, h2, p {
4 of 10 07/03/2017 9:21
CSS Syntax and Selectors https://www.w3schools.com/css/css_syntax.asp
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.
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 »
❮ Previous Next ❯
5 of 10 07/03/2017 9:21
CSS How to https://www.w3schools.com/css/css_howto.asp
w3schools.com
THE WORLD'S LARGEST WEB DEVELOPER SITE
When a browser reads a style sheet, it will format the HTML document according to the
information in the style sheet.
Each page must include a reference to the external style sheet file inside the <link>
element. The <link> element goes inside the <head> section:
Example
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>
1 of 10 07/03/2017 9:21
CSS How to https://www.w3schools.com/css/css_howto.asp
Try it Yourself »
An external style sheet can be written in any text editor. The file should not contain any
html tags. The style sheet file must be saved with a .css extension.
body {
background-color: lightblue;
}
h1 {
color: navy;
margin-left: 20px;
}
Note: Do not add a space between the property value and the unit (such as
margin-left: 20 px; ). The correct way is: margin-left: 20px;
Internal styles are defined within the <style> element, inside the <head> section of an
HTML page:
Example
<head>
<style>
body {
background-color: linen;
}
h1 {
2 of 10 07/03/2017 9:21
CSS How to https://www.w3schools.com/css/css_howto.asp
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
Try it Yourself »
Tip: An inline style loses many of the advantages of a style sheet (by mixing content
with presentation). Use this method sparingly.
Example
Assume that an external style sheet has the following style for the <h1> element:
h1 {
color: navy;
}
3 of 10 07/03/2017 9:21
CSS How to https://www.w3schools.com/css/css_howto.asp
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 »
4 of 10 07/03/2017 9:21
CSS How to https://www.w3schools.com/css/css_howto.asp
Cascading Order
What style will be used when there is more than one style specified for an HTML element?
Generally speaking we can say that all the styles will "cascade" into a new "virtual" style
sheet by the following rules, where number one has the highest priority:
So, an inline style (inside a specific HTML element) has the highest priority, which means
that it will override a style defined inside the <head> tag, or in an external style sheet, or a
browser default value.
Try it Yourself »
❮ Previous Next ❯
5 of 10 07/03/2017 9:21
CSS Colors https://www.w3schools.com/css/css_colors.asp
w3schools.com
THE WORLD'S LARGEST WEB DEVELOPER SITE
CSS Colors
❮ Previous Next ❯
Color Names
Colors set by using color names:
Example
Color Name
Red
Green
Blue
Orange
Yellow
Cyan
Black
1 of 9 07/03/2017 9:22
CSS Colors https://www.w3schools.com/css/css_colors.asp
Note: Color names are case-insensitive: "Red" is the same as "red" or "RED".
Each parameter (red, green, blue) defines the intensity of the color between 0 and 255.
For example, rgb(255,0,0) is displayed as red, because red is set to its highest value
(255) and the others are set to 0. Experiment by mixing the RGB values below:
255 0 0
rgb(255, 0, 0)
Example
Color RGB
rgb(255,0,0)
rgb(0,255,0)
rgb(0,0,255)
rgb(255,165,0)
2 of 9 07/03/2017 9:22
CSS Colors https://www.w3schools.com/css/css_colors.asp
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
3 of 9 07/03/2017 9:22
CSS Colors https://www.w3schools.com/css/css_colors.asp
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 »
Advanced colors: In our CSS3 Colors Tutorial, you will learn about HSL and RGBa
colors.
❮ Previous Next ❯
4 of 9 07/03/2017 9:22
CSS Backgrounds https://www.w3schools.com/css/css_background.asp
w3schools.com
THE WORLD'S LARGEST WEB DEVELOPER SITE
CSS Backgrounds
❮ Previous Next ❯
background-color
background-image
background-repeat
background-attachment
background-position
Background Color
The background-color property specifies the background color of an element.
Example
body {
background-color: lightblue;
}
Try it Yourself »
1 of 10 07/03/2017 9:23
CSS Backgrounds https://www.w3schools.com/css/css_background.asp
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
2 of 10 07/03/2017 9:23
CSS Backgrounds https://www.w3schools.com/css/css_background.asp
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.
Some images should be repeated only horizontally or vertically, or they will look strange,
like this:
Example
body {
background-image: url("gradient_bg.png");
}
Try it Yourself »
3 of 10 07/03/2017 9:23
CSS Backgrounds https://www.w3schools.com/css/css_background.asp
Example
body {
background-image: url("gradient_bg.png");
background-repeat: repeat-x;
}
Try it Yourself »
Example
body {
background-image: url("img_tree.png");
background-repeat: no-repeat;
}
Try it Yourself »
In the example above, the background image is shown in the same place as the text. We
want to change the position of the image, so that it does not disturb the text too much.
Example
body {
background-image: url("img_tree.png");
background-repeat: no-repeat;
background-position: right top;
}
4 of 10 07/03/2017 9:23
CSS Backgrounds https://www.w3schools.com/css/css_background.asp
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
5 of 10 07/03/2017 9:23
CSS Borders https://www.w3schools.com/css/css_border.asp
w3schools.com
THE WORLD'S LARGEST WEB DEVELOPER SITE
CSS Borders
❮ Previous Next ❯
Border Style
The border-style property specifies what kind of border to display.
1 of 13 07/03/2017 9:23
CSS Borders https://www.w3schools.com/css/css_border.asp
groove - Defines a 3D grooved border. The effect depends on the border-color value
ridge - Defines a 3D ridged border. The effect depends on the border-color value
inset - Defines a 3D inset border. The effect depends on the border-color value
outset - Defines a 3D outset border. The effect depends on the border-color value
none - Defines no border
hidden - Defines a hidden border
The border-style property can have from one to four values (for the top border, right
border, bottom border, and the left border).
Example
Result:
2 of 13 07/03/2017 9:23
CSS Borders https://www.w3schools.com/css/css_border.asp
A dotted border.
A dashed border.
A solid border.
A double border.
No border.
A hidden border.
A mixed border.
Try it Yourself »
Note: None of the OTHER CSS border properties described below will have ANY effect
unless the border-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).
3 of 13 07/03/2017 9:23
CSS Borders https://www.w3schools.com/css/css_border.asp
5px border-width
Example
p.one {
border-style: solid;
border-width: 5px;
}
p.two {
border-style: solid;
border-width: medium;
}
p.three {
border-style: solid;
border-width: 2px 10px 4px 20px;
}
Try it Yourself »
Border Color
The border-color property is used to set the color of the four borders.
The border-color property can have from one to four values (for the top border, right
border, bottom border, and the left border).
Red border
Example
p.one {
4 of 13 07/03/2017 9:23
CSS Borders https://www.w3schools.com/css/css_border.asp
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 »
In CSS, there is also properties for specifying each of the borders (top, right, bottom, and
left):
Example
p {
border-top-style: dotted;
border-right-style: solid;
border-bottom-style: dotted;
border-left-style: solid;
}
Try it Yourself »
Example
5 of 13 07/03/2017 9:23
CSS Borders https://www.w3schools.com/css/css_border.asp
p {
border-style: dotted solid;
}
Try it Yourself »
border-style: dotted;
all four borders are dotted
The border-style property is used in the example above. However, it also works with
border-width and border-color .
To shorten the code, it is also possible to specify all the individual border properties in one
property.
The border property is a shorthand property for the following individual border properties:
border-width
6 of 13 07/03/2017 9:23
CSS Borders https://www.w3schools.com/css/css_border.asp
border-style (required)
border-color
Example
p {
border: 5px solid red;
}
Result:
Some text
Try it Yourself »
You can also specify all the individual border properties for just one side:
Left Border
p {
border-left: 6px solid red;
background-color: lightgrey;
}
Result:
Some text
Try it Yourself »
Bottom Border
p {
border-bottom: 6px solid red;
background-color: lightgrey;
}
Result:
7 of 13 07/03/2017 9:23
CSS Borders https://www.w3schools.com/css/css_border.asp
Some text
Try it Yourself »
Rounded Borders
The border-radius property is used to add rounded borders to an element:
Normal border
Round border
Rounder border
Roundest border
Example
p {
border: 2px solid red;
border-radius: 5px;
}
Try it Yourself »
Note: The border-radius property is not supported in IE8 and earlier versions.
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.
8 of 13 07/03/2017 9:23
CSS Borders https://www.w3schools.com/css/css_border.asp
This example demonstrates how to set the color of the four borders. It can have from one
to four colors.
border-radius Sets all the four border-*-radius properties for rounded corners
9 of 13 07/03/2017 9:23
CSS Borders https://www.w3schools.com/css/css_border.asp
❮ Previous Next ❯
COLOR PICKER
10 of 13 07/03/2017 9:23
CSS Margin https://www.w3schools.com/css/css_margin.asp
w3schools.com
THE WORLD'S LARGEST WEB DEVELOPER SITE
CSS Margins
❮ Previous Next ❯
CSS Margins
The CSS margin properties are used to generate space around elements.
The margin properties set the size of the white space outside the border.
With CSS, you have full control over the margins. There are CSS properties for setting the
margin for each side of an element (top, right, bottom, and left).
margin-top
margin-right
margin-bottom
margin-left
1 of 9 07/03/2017 9:24
CSS Margin https://www.w3schools.com/css/css_margin.asp
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 margin property is a shorthand property for the following individual margin properties:
margin-top
margin-right
margin-bottom
margin-left
Example
p {
margin: 100px 150px 100px 80px;
}
2 of 9 07/03/2017 9:24
CSS Margin https://www.w3schools.com/css/css_margin.asp
Try it Yourself »
margin: 25px;
all four margins are 25px
The element will then take up the specified width, and the remaining space will be split
equally between the left and right margins:
Example
div {
width: 300px;
margin: auto;
border: 1px solid red;
}
3 of 9 07/03/2017 9:24
CSS Margin https://www.w3schools.com/css/css_margin.asp
Try it Yourself »
Example
div.container {
border: 1px solid red;
margin-left: 100px;
}
p.one {
margin-left: inherit;
}
Try it Yourself »
Margin Collapse
Top and bottom margins of elements are sometimes collapsed into a single margin that is
equal to the largest of the two margins.
This does not happen on left and right margins! Only top and bottom margins!
Example
h1 {
margin: 0 0 50px 0;
}
h2 {
margin: 20px 0 0 0;
}
Try it Yourself »
4 of 9 07/03/2017 9:24
CSS Margin https://www.w3schools.com/css/css_margin.asp
In the example above, the <h1> element has a bottom margin of 50px. The <h2> element
has a top margin set to 20px.
Common sense would seem to suggest that the vertical margin between the <h1> and the
<h2> would be a total of 70px (50px + 20px). But due to margin collapse, the actual
margin ends up being 50px.
❮ Previous Next ❯
5 of 9 07/03/2017 9:24
CSS Padding https://www.w3schools.com/css/css_padding.asp
w3schools.com
THE WORLD'S LARGEST WEB DEVELOPER SITE
CSS Padding
❮ Previous Next ❯
CSS Padding
The CSS padding properties are used to generate space around content.
The padding clears an area around the content (inside the border) of an element.
With CSS, you have full control over the padding. There are CSS properties for setting the
padding for each side of an element (top, right, bottom, and left).
padding-top
padding-right
padding-bottom
padding-left
1 of 8 07/03/2017 9:26
CSS Padding https://www.w3schools.com/css/css_padding.asp
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 »
The padding property is a shorthand property for the following individual padding
properties:
padding-top
padding-right
padding-bottom
padding-left
Example
p {
padding: 50px 30px 50px 80px;
}
2 of 8 07/03/2017 9:26
CSS Padding https://www.w3schools.com/css/css_padding.asp
Try it Yourself »
padding: 25px;
all four paddings are 25px
Example
div.ex1 {
padding: 25px 50px 75px 100px;
}
div.ex2 {
padding: 25px 50px 75px;
}
div.ex3 {
padding: 25px 50px;
}
div.ex4 {
padding: 25px;
}
3 of 8 07/03/2017 9:26
CSS Padding https://www.w3schools.com/css/css_padding.asp
Try it Yourself »
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.
padding A shorthand property for setting all the padding properties in one
declaration
❮ Previous Next ❯
4 of 8 07/03/2017 9:26
CSS Height and Width Dimensions https://www.w3schools.com/css/css_dimension.asp
w3schools.com
THE WORLD'S LARGEST WEB DEVELOPER SITE
The height and width can be set to auto (this is default. Means that the browser
calculates the height and width), or be specified in length values, like px, cm, etc., or in
percent (%) of the containing block.
Example
div {
height: 200px;
width: 50%;
1 of 8 07/03/2017 9:27
CSS Height and Width Dimensions https://www.w3schools.com/css/css_dimension.asp
background-color: powderblue;
}
Try it Yourself »
Example
div {
height: 100px;
width: 500px;
background-color: powderblue;
}
Try it Yourself »
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!
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.
2 of 8 07/03/2017 9:27
CSS Height and Width Dimensions https://www.w3schools.com/css/css_dimension.asp
Tip: Drag the browser window to smaller than 500px wide, to see the difference between
the two divs!
The following example shows a <div> element with a height of 100 pixels and a
max-width of 500 pixels:
Example
div {
max-width: 500px;
height: 100px;
background-color: powderblue;
}
Try it Yourself »
3 of 8 07/03/2017 9:27
CSS Height and Width Dimensions https://www.w3schools.com/css/css_dimension.asp
❮ Previous Next ❯
4 of 8 07/03/2017 9:27
CSS Box Model https://www.w3schools.com/css/css_boxmodel.asp
w3schools.com
THE WORLD'S LARGEST WEB DEVELOPER SITE
The CSS box model is essentially a box that wraps around every HTML element. It consists
of: margins, borders, padding, and the actual content. The image below illustrates the box
model:
Margin
Border
Padding
Content
1 of 8 07/03/2017 9:27
CSS Box Model https://www.w3schools.com/css/css_boxmodel.asp
Content - The content of the box, where text and images appear
Padding - Clears an area around the content. The padding is transparent
Border - A border that goes around the padding and content
Margin - Clears an area outside the border. The margin is transparent
The box model allows us to add a border around elements, and to define space between
elements.
Example
div {
width: 300px;
border: 25px solid green;
padding: 25px;
margin: 25px;
}
Try it Yourself »
Important: When you set the width and height properties of an element with CSS,
you just set the width and height of the content area. To calculate the full size of an
element, you must also add padding, borders and margins.
2 of 8 07/03/2017 9:27
CSS Box Model https://www.w3schools.com/css/css_boxmodel.asp
Example
div {
width: 320px;
padding: 10px;
border: 5px solid gray;
margin: 0;
}
Try it Yourself »
320px (width)
+ 20px (left + right padding)
+ 10px (left + right border)
+ 0px (left + right margin)
= 350px
Total element width = width + left padding + right padding + left border + right border +
left margin + right margin
Total element height = height + top padding + bottom padding + top border + bottom
border + top margin + bottom margin
Note for old IE: Internet Explorer 8 and earlier versions, include padding and border in
the width property. To fix this problem, add a <!DOCTYPE html> to the HTML page.
❮ Previous Next ❯
3 of 8 07/03/2017 9:27
CSS Outline Properties https://www.w3schools.com/css/css_outline.asp
w3schools.com
THE WORLD'S LARGEST WEB DEVELOPER SITE
CSS Outline
❮ Previous Next ❯
CSS Outline
The CSS outline properties specify the style, color, and width of an outline.
An outline is a line that is drawn around elements (outside the borders) to make the
element "stand out".
However, the outline property is different from the border property - The outline is NOT a
part of an element's dimensions; the element's total width and height is not affected by the
width of the outline.
Outline
Border
Content
1 of 9 07/03/2017 9:28
CSS Outline Properties https://www.w3schools.com/css/css_outline.asp
This element has a thin black border and a double outline that is 10px wide and
green.
Outline Style
The outline-style property specifies the style of the outline.
The following example first sets a thin black border around each <p> element, then it
shows the different outline-style values:
Example
p {
border: 1px solid black;
outline-color: red;
}
Result:
A dotted outline.
2 of 9 07/03/2017 9:28
CSS Outline Properties https://www.w3schools.com/css/css_outline.asp
A dashed outline.
A solid outline.
A double outline.
Try it Yourself »
Note: None of the OTHER CSS outline properties described below will have ANY effect
unless the outline-style property is set!
Outline Color
The outline-color property is used to set the color of the outline.
Example
p {
border: 1px solid black;
outline-style: double;
outline-color: red;
}
3 of 9 07/03/2017 9:28
CSS Outline Properties https://www.w3schools.com/css/css_outline.asp
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.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 »
4 of 9 07/03/2017 9:28
CSS Outline Properties https://www.w3schools.com/css/css_outline.asp
To shorten the code, it is also possible to specify all the individual outline properties in one
property.
The outline property is a shorthand property for the following individual outline
properties:
outline-width
outline-style (required)
outline-color
Example
p {
border: 1px solid black;
outline: 5px dotted red;
}
Result:
An outline.
Try it Yourself »
outline-offset Specifies the space between an outline and the edge or border of an
element
5 of 9 07/03/2017 9:28
CSS Text https://www.w3schools.com/css/css_text.asp
w3schools.com
THE WORLD'S LARGEST WEB DEVELOPER SITE
CSS Text
❮ Previous Next ❯
TEXT FORMATTING
This text is styled with some of the text formatting
properties. The heading uses the text-align, text-transform, and
color properties. The paragraph is indented, aligned, and the
space between characters is specified. The underline is removed
from this colored "Try it Yourself" link.
Text Color
The color property is used to set the color of the text.
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;
1 of 13 07/03/2017 9:28
CSS Text https://www.w3schools.com/css/css_text.asp
h1 {
color: green;
}
Try it Yourself »
Note: For W3C compliant CSS: If you define the color property, you must also
define the background-color .
Text Alignment
The text-align property is used to set the horizontal alignment of a text.
The following example shows center aligned, and left and right aligned text (left alignment
is default if text direction is left-to-right, and right alignment is default if text direction is
right-to-left):
Example
h1 {
text-align: center;
}
h2 {
text-align: left;
}
h3 {
text-align: right;
}
2 of 13 07/03/2017 9:28
CSS Text https://www.w3schools.com/css/css_text.asp
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 »
Example
h1 {
text-decoration: overline;
}
h2 {
text-decoration: line-through;
}
3 of 13 07/03/2017 9:28
CSS Text https://www.w3schools.com/css/css_text.asp
h3 {
text-decoration: underline;
}
Try it Yourself »
Note: It is not recommended to underline text that is not a link, as this often confuses
the reader.
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
4 of 13 07/03/2017 9:28
CSS Text https://www.w3schools.com/css/css_text.asp
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;
}
5 of 13 07/03/2017 9:28
CSS Text https://www.w3schools.com/css/css_text.asp
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 »
Text Shadow
The text-shadow property adds shadow to text.
The following example specifies the position of the horizontal shadow (3px), the position of
6 of 13 07/03/2017 9:28
CSS Text https://www.w3schools.com/css/css_text.asp
the vertical shadow (2px) and the color of the shadow (red):
Example
h1 {
text-shadow: 3px 2px red;
}
Try it Yourself »
More Examples
Disable text wrapping inside an element
This example demonstrates how to disable text wrapping inside an element.
7 of 13 07/03/2017 9:28
CSS Text https://www.w3schools.com/css/css_text.asp
Property Description
unicode-bidi Used together with the direction property to set or return whether the
text should be overridden to support multiple languages in the same
document
❮ Previous Next ❯
8 of 13 07/03/2017 9:28
CSS Icons https://www.w3schools.com/css/css_icons.asp
w3schools.com
THE WORLD'S LARGEST WEB DEVELOPER SITE
CSS Icons
❮ Previous Next ❯
Add the name of the specified icon class to any inline HTML element (like <i> or <span> ).
All the icons in the icon libraries below, are scalable vectors that can be customized with
CSS (size, color, shadow, etc.)
Example
1 of 8 07/03/2017 9:30
CSS Icons https://www.w3schools.com/css/css_icons.asp
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-
awesome/4.7.0/css/font-awesome.min.css">
</head>
<body>
</body>
</html>
Result:
Try It Yourself »
Bootstrap Icons
To use the Bootstrap glyphicons, add the following line inside the <head> section of your
HTML page:
Example
<!DOCTYPE html>
<html>
2 of 8 07/03/2017 9:30
CSS Icons https://www.w3schools.com/css/css_icons.asp
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap
/3.3.7/css/bootstrap.min.css">
</head>
<body>
</body>
</html>
Result:
☁✉
Try It Yourself »
Google Icons
To use the Google icons, add the following line inside the <head> section of your HTML
page:
Example
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://fonts.googleapis.com
/icon?family=Material+Icons">
</head>
<body>
<i class="material-icons">cloud</i>
<i class="material-icons">favorite</i>
<i class="material-icons">attachment</i>
3 of 8 07/03/2017 9:30
CSS Icons https://www.w3schools.com/css/css_icons.asp
<i class="material-icons">computer</i>
<i class="material-icons">traffic</i>
</body>
</html>
Result:
Try It Yourself »
❮ Previous Next ❯
4 of 8 07/03/2017 9:30
CSS Styling Links https://www.w3schools.com/css/css_link.asp
w3schools.com
THE WORLD'S LARGEST WEB DEVELOPER SITE
CSS Links
❮ Previous Next ❯
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.
1 of 8 07/03/2017 9:31
CSS Styling Links https://www.w3schools.com/css/css_link.asp
Example
/* unvisited link */
a:link {
color: red;
}
/* visited link */
a:visited {
color: green;
}
/* selected link */
a:active {
color: blue;
}
Try it Yourself »
When setting the style for several link states, there are some order rules:
Text Decoration
2 of 8 07/03/2017 9:31
CSS Styling Links https://www.w3schools.com/css/css_link.asp
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:
3 of 8 07/03/2017 9:31
CSS Styling Links https://www.w3schools.com/css/css_link.asp
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 »
4 of 8 07/03/2017 9:31
CSS Styling Links https://www.w3schools.com/css/css_link.asp
More Examples
Add different styles to hyperlinks
This example demonstrates how to add other styles to hyperlinks.
❮ Previous Next ❯
COLOR PICKER
5 of 8 07/03/2017 9:31
CSS Styling Lists https://www.w3schools.com/css/css_list.asp
w3schools.com
THE WORLD'S LARGEST WEB DEVELOPER SITE
CSS Lists
❮ Previous Next ❯
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
1 of 9 07/03/2017 9:31
CSS Styling Lists https://www.w3schools.com/css/css_list.asp
The following example shows some of the available list item markers:
Example
ul.a {
list-style-type: circle;
}
ul.b {
list-style-type: square;
}
ol.c {
list-style-type: upper-roman;
}
ol.d {
list-style-type: lower-alpha;
}
Try it Yourself »
Note: Some of the values are for unordered lists, and some for ordered lists.
Example
ul {
list-style-image: url('sqpurple.gif');
2 of 9 07/03/2017 9:31
CSS Styling Lists https://www.w3schools.com/css/css_list.asp
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
3 of 9 07/03/2017 9:31
CSS Styling Lists https://www.w3schools.com/css/css_list.asp
Anything added to the <ol> or <ul> tag, affects the entire list, while properties added to
the <li> tag will affect the individual list items:
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
4 of 9 07/03/2017 9:31
CSS Styling Lists https://www.w3schools.com/css/css_list.asp
Coffee
Tea
Coca Cola
Try it Yourself »
More Examples
Customized list with a red left border
This example demonstrates how to create a list with a red left border.
list-style- Specifies if the list-item markers should appear inside or outside the
position content flow
❮ Previous Next ❯
5 of 9 07/03/2017 9:31
CSS Styling Tables https://www.w3schools.com/css/css_table.asp
w3schools.com
THE WORLD'S LARGEST WEB DEVELOPER SITE
CSS Tables
❮ Previous Next ❯
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:
1 of 14 07/03/2017 9:32
CSS Styling Tables https://www.w3schools.com/css/css_table.asp
Firstname Lastname
Peter Griffin
Lois Griffin
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.
Firstname Lastname
Peter Griffin
Lois Griffin
2 of 14 07/03/2017 9:32
CSS Styling Tables https://www.w3schools.com/css/css_table.asp
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>:
Firstname Lastname
Peter Griffin
Lois Griffin
Example
table {
border: 1px solid black;
}
Try it Yourself »
The example below sets the width of the table to 100%, and the height of the <th>
elements to 50px:
3 of 14 07/03/2017 9:32
CSS Styling Tables https://www.w3schools.com/css/css_table.asp
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.
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>.
4 of 14 07/03/2017 9:32
CSS Styling Tables https://www.w3schools.com/css/css_table.asp
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 {
5 of 14 07/03/2017 9:32
CSS Styling Tables https://www.w3schools.com/css/css_table.asp
padding: 15px;
text-align: left;
}
Try it Yourself »
Horizontal Dividers
First Name Last Name Savings
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:
6 of 14 07/03/2017 9:32
CSS Styling Tables https://www.w3schools.com/css/css_table.asp
Example
Try it Yourself »
Striped Tables
First Name Last Name Savings
For zebra-striped tables, use the nth-child() selector and add a background-color to
all even (or odd) table rows:
Example
Try it Yourself »
Table Color
The example below specifies the background color and text color of <th> elements:
7 of 14 07/03/2017 9:32
CSS Styling Tables https://www.w3schools.com/css/css_table.asp
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 Last Points Points Points Points Points Points Points Points
Name Name
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
8 of 14 07/03/2017 9:32
CSS Styling Tables https://www.w3schools.com/css/css_table.asp
<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.
Exercise 6 »
9 of 14 07/03/2017 9:32
CSS Layout - The display Property https://www.w3schools.com/css/css_display_visibility.asp
w3schools.com
THE WORLD'S LARGEST WEB DEVELOPER SITE
The display property is the most important CSS property for controlling layout.
Every HTML element has a default display value depending on what type of element it is.
The default display value for most elements is block or inline .
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).
<div>
<h1> - <h6>
<p>
<form>
<header>
<footer>
<section>
1 of 9 07/03/2017 9:32
CSS Layout - The display Property https://www.w3schools.com/css/css_display_visibility.asp
Inline Elements
An inline element does not start on a new line and only takes up as much width as
necessary.
<span>
<a>
<img>
Display: none;
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.
Changing an inline element to a block element, or vice versa, can be useful for making the
page look a specific way, and still follow the web standards.
Example
li {
display: inline;
}
Try it Yourself »
Note: Setting the display property of an element only changes how the element is
displayed, NOT what kind of element it is. So, an inline element with display:
block; is not allowed to have other block elements inside it.
2 of 9 07/03/2017 9:32
CSS Layout - The display Property https://www.w3schools.com/css/css_display_visibility.asp
Example
span {
display: block;
}
Try it Yourself »
Example
a {
display: block;
}
Try it Yourself »
Hiding an element can be done by setting the display property to none . The element will
3 of 9 07/03/2017 9:32
CSS Layout - The display Property https://www.w3schools.com/css/css_display_visibility.asp
be hidden, and the page will be displayed as if the element is not there:
Example
h1.hidden {
display: none;
}
Try it Yourself »
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
Differences between display: none; and visibility: hidden;
This example demonstrates display: none; versus visibility: hidden;
4 of 9 07/03/2017 9:32
CSS Layout - The display Property https://www.w3schools.com/css/css_display_visibility.asp
Property Description
❮ Previous Next ❯
COLOR PICKER
LEARN MORE
Tabs
Dropdowns
5 of 9 07/03/2017 9:32
CSS Layout - width and max-width https://www.w3schools.com/css/css_max-width.asp
w3schools.com
THE WORLD'S LARGEST WEB DEVELOPER SITE
Setting the width of a block-level element will prevent it from stretching out to the edges
of its container. Then, you can set the margins to auto, to horizontally center the element
within its container. The element will take up the specified width, and the remaining space
will be split equally between the two margins:
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:
Tip: Resize the browser window to less than 500px wide, to see the difference between the
two divs!
1 of 6 07/03/2017 9:33
CSS Layout - width and max-width https://www.w3schools.com/css/css_max-width.asp
Example
div.ex1 {
width: 500px;
margin: auto;
border: 3px solid #73AD21;
}
div.ex2 {
max-width: 500px;
margin: auto;
border: 3px solid #73AD21;
}
Try it Yourself »
❮ Previous Next ❯
2 of 6 07/03/2017 9:33
CSS Layout - The position Property https://www.w3schools.com/css/css_positioning.asp
w3schools.com
THE WORLD'S LARGEST WEB DEVELOPER SITE
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:
1 of 11 07/03/2017 9:35
CSS Layout - The position Property https://www.w3schools.com/css/css_positioning.asp
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.
Example
div.relative {
position: relative;
left: 30px;
border: 3px solid #73AD21;
}
Try it Yourself »
fixed;
2 of 11 07/03/2017 9:35
CSS Layout - The position Property https://www.w3schools.com/css/css_positioning.asp
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 »
position: absolute;
An element with position: absolute; is positioned relative to the nearest positioned
ancestor (instead of positioned relative to the viewport, like fixed).
3 of 11 07/03/2017 9:35
CSS Layout - The position Property https://www.w3schools.com/css/css_positioning.asp
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).
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; This <div> element has position:
fixed;
4 of 11 07/03/2017 9:35
CSS Layout - The position Property https://www.w3schools.com/css/css_positioning.asp
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 HTML code will be shown on top.
Example
Try it Yourself:
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.
5 of 11 07/03/2017 9:35
CSS Layout - The position Property https://www.w3schools.com/css/css_positioning.asp
This example demonstrates how to set the browser to automatically handle overflow.
❮ Previous Next ❯
6 of 11 07/03/2017 9:35
CSS Overflow https://www.w3schools.com/css/css_overflow.asp
w3schools.com
THE WORLD'S LARGEST WEB DEVELOPER SITE
CSS Overflow
The CSS overflow property specifies whether to clip content or to add scrollbars when the
content of an element is too big to fit in a specified area.
This text is really long and the height of its container is only 100 pixels. Therefore, a
scrollbar is added to help the reader to scroll the content. Lorem ipsum dolor sit amet,
consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet
dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci
visible - Default. The overflow is not clipped. It renders outside the element's box
hidden - The overflow is clipped, and the rest of the content will be invisible
scroll - The overflow is clipped, but a scrollbar is added to see the rest of the content
auto - If overflow is clipped, a scrollbar should be added to see the rest of the content
Note: The overflow property only works for block elements with a specified height.
Note: In OS X Lion (on Mac), scrollbars are hidden by default and only shown when
being used (even though "overflow:scroll" is set).
Visible
By default, the overflow is visible , meaning that it is not clipped and it renders outside
the element's box:
1 of 9 07/03/2017 9:36
CSS Overflow https://www.w3schools.com/css/css_overflow.asp
Example
div {
width: 200px;
height: 50px;
background-color: #eee;
overflow: visible;
}
Try it Yourself »
Hidden
With the hidden value, the overflow is clipped, and the rest of the content is hidden:
2 of 9 07/03/2017 9:36
CSS Overflow https://www.w3schools.com/css/css_overflow.asp
Example
div {
overflow: hidden;
}
Try it Yourself »
Scroll
Setting the value to scroll , the overflow is clipped and a scrollbar is added to scroll inside
the box. Note that this will add a scrollbar both horizontally and vertically (even if you do
not need it):
Example
div {
overflow: scroll;
}
Try it Yourself »
Auto
The auto value is similar to scroll , only it add scrollbars when necessary:
Example
3 of 9 07/03/2017 9:36
CSS Overflow https://www.w3schools.com/css/css_overflow.asp
div {
overflow: auto;
}
Try it Yourself »
Example
div {
overflow-x: hidden; /* Hide horizontal scrollbar */
overflow-y: scroll; /* Add vertical scrollbar */
}
Try it Yourself »
❮ Previous Next ❯
4 of 9 07/03/2017 9:36