TUTORIAL HOME
CSS Box Model
❮ Previous Next ❯
The CSS Box Model
All HTML elements can be considered as boxes. In CSS, the term “box model” is used when talking about design and layout.
The CSS box model is essentially a box that wraps around every HTML element. It consists of: margins, borders, padding, and the actual content. The image below illustrates the box model:
Explanation of the different parts:
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;
}
»
Width and Height of an Element
In order to set the width and height of an element correctly in all browsers, you need to know how the box model works.
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.
Assume we want to style a
Example
div {
width: 320px;
padding: 10px;
border: 5px solid gray;
margin: 0;
}
»
Here is the math:
320px (width)
+ 20px (left + right padding)
+ 10px (left + right border)
+ 0px (left + right margin)
= 350px
The total width of an element should be calculated like this:
Total element width = width + left padding + right padding + left border + right border + left margin + right margin
The total height of an element should be calculated like this:
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 to the HTML page.
Test Yourself with Exercises!
Exercise 1 » Exercise 2 » Exercise 3 » Exercise 4 »
❮ Previous Next ❯
CSS Outline
TUTORIAL HOME
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.
This element has a thin black border and an outline that is 10px wide and green.
Outline Style
The outline-style property specifies the style of the outline.
The outline-style property can have one of the following values:
dotted – Defines a dotted outline
dashed – Defines a dashed outline
solid – Defines a solid outline
double – Defines a double outline
groove – Defines a 3D grooved outline. The effect depends on the outline-color value
ridge – Defines a 3D ridged outline. The effect depends on the outline-color value
inset – Defines a 3D inset outline. The effect depends on the outline-color value
outset – Defines a 3D outset outline. The effect depends on the outline-color value
none – Defines no outline
hidden – Defines a hidden outline
The following example first sets a thin black border around each
element, then it shows the different outline-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.
»
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.
The color can be set by:
name – specify a color name, like “red”
RGB – specify a RGB value, like “rgb(255,0,0)”
Hex – specify a hex value, like “#ff0000”
invert – performs a color inversion (which ensures that the outline is visible, regardless of color background)
Example
p {
border: 1px solid black;
outline-style: double;
outline-color: red;
}
Result:
A colored outline.
»
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.
»
Outline – Shorthand property
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.
»
Test Yourself with Exercises!
Exercise 1 » Exercise 2 » Exercise 3 »
All CSS Outline Properties
Property Description
outline Sets all the outline properties in one declaration
outline-color Sets the color of an outline
outline-offset Specifies the space between an outline and the edge or border of an element
outline-style Sets the style of an outline
outline-width Sets the width of an outline
❮ Previous Next ❯
CSS Text
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 “” link.
Text Color
The color property is used to set the color of the text.
With CSS, a color is most often specified by:
a color name – like “red”
a HEX value – like “#ff0000”
an RGB value – like “rgb(255,0,0)”
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;
}
»
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.
A text can be left or right aligned, centered, or justified.
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;
}
»
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;
}
»
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;
}
»
The other text-decoration values are used to decorate text:
Example
h1 {
text-decoration: overline;
}
h2 {
text-decoration: line-through;
}
h3 {
text-decoration: underline;
}
»
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;
}
»
Text Indentation
The text-indent property is used to specify the indentation of the first line of a text:
Example
p {
text-indent: 50px;
}
»
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;
}
»
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;
}
»
Text Direction
The direction property is used to change the text direction of an element:
Example
div {
direction: rtl;
}
»
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;
}
»
Text Shadow
The text-shadow property adds shadow to text.
The following example specifies the position of the horizontal shadow (3px), the position of the vertical shadow (2px) and the color of the shadow (red):
Example
h1 {
text-shadow: 3px 2px red;
}
»
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.
Test Yourself with Exercises!
Exercise 1 » Exercise 2 » Exercise 3 » Exercise 4 » Exercise 5 »
All CSS Text Properties
Property Description
color Sets the color of text
direction Specifies the text direction/writing direction
letter-spacing Increases or decreases the space between characters in a text
line-height Sets the line height
text-align Specifies the horizontal alignment of text
text-decoration Specifies the decoration added to text
text-indent Specifies the indentation of the first line in a text-block
text-shadow Specifies the shadow effect added to text
text-transform Controls the capitalization of text
text-overflow Specifies how overflowed content that is not displayed should be signaled to the user
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
vertical-align Sets the vertical alignment of an element
white-space Specifies how white-space inside an element is handled
word-spacing Increases or decreases the space between words in a text
❮ Previous Next ❯
CSS Fonts
TUTORIAL HOME
CSS Fonts
❮ Previous Next ❯
The CSS font properties define the font family, boldness, size, and the style of a text.
Difference Between Serif and Sans-serif Fonts
CSS Font Families
In CSS, there are two types of font family names:
generic family – a group of font families with a similar look (like “Serif” or “Monospace”)
font family – a specific font family (like “Times New Roman” or “Arial”)
Generic family Font family Description
Serif Times New Roman
Georgia Serif fonts have small lines at the ends on some characters
Sans-serif Arial
Verdana “Sans” means without – these fonts do not have the lines at the ends of characters
Monospace Courier New
Lucida Console All monospace characters have the same width
Note: On computer screens, sans-serif fonts are considered easier to read than serif fonts.
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;
}
»
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:
normal – The text is shown normally
italic – The text is shown in italics
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;
}
»
Font Size
The font-size property sets the size of the text.
Being able to manage the text size is important in web design. However, you should not use font size adjustments to make paragraphs look like headings, or headings look like paragraphs.
Always use the proper HTML tags, like
–
for headings and
for paragraphs.
The font-size value can be an absolute, or relative size.
Absolute size:
Sets the text to a specified 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:
Sets the size relative to surrounding elements
Allows a user to change the text size in browsers
Note: If you do not specify a font size, the default size for normal text, like paragraphs, is 16px (16px=1em).
Set Font Size With Pixels
Setting the text size with pixels gives you full control over the text size:
Example
h1 {
font-size: 40px;
}
h2 {
font-size: 30px;
}
p {
font-size: 14px;
}
»
Tip: If you use pixels, you can still use the zoom tool to resize the entire page.
Set Font Size With Em
To allow users to resize the text (in the browser menu), many developers use em instead of pixels.
The em size unit is recommended by the W3C.
1em is equal to the current font size. The default text size in browsers is 16px. So, the default size of 1em is 16px.
The size can be calculated from pixels to em using this formula: pixels/16=em
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 */
}
»
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.
Use a Combination of Percent and Em
The solution that works in all browsers, is to set a default font-size in percent for the element:
Example
body {
font-size: 100%;
}
h1 {
font-size: 2.5em;
}
h2 {
font-size: 1.875em;
}
p {
font-size: 0.875em;
}
»
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;
}
»
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;
}
»
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.
Test Yourself with Exercises!
Exercise 1 » Exercise 2 » Exercise 3 » Exercise 4 » Exercise 5 »
All CSS Font Properties
Property Description
font Sets all the font properties in one declaration
font-family Specifies the font family for text
font-size Specifies the font size of text
font-style Specifies the font style for text
font-variant Specifies whether or not a text should be displayed in a small-caps font
font-weight Specifies the weight of a font
❮ Previous Next ❯
CSS Icons
TUTORIAL HOME
CSS Icons
❮ Previous Next ❯
How To Add Icons
The simplest way to add an icon to your HTML page, is with an icon library, such as Font Awesome.
Add the name of the specified icon class to any inline HTML element (like or ).
All the icons in the icon libraries below, are scalable vectors that can be customized with CSS (size, color, shadow, etc.)
Font Awesome Icons
To use the Font Awesome icons, add the following line inside the section of your HTML page:
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css“>
Note: No downloading or installation is required!
Example
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css“>
Result:
»
Bootstrap Icons
To use the Bootstrap glyphicons, add the following line inside the section of your HTML page:
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css“>
Note: No downloading or installation is required!
Example
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css“>
Result:
»
Google Icons
To use the Google icons, add the following line inside the section of your HTML page:
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons“>
Note: No downloading or installation is required!
Example
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons“>
cloud</i>
favorite
attachment
computer
traffic
Result:
»
For a complete list of all icons, visit our Icon Tutorial.
❮ Previous Next ❯
CSS Links
CSS Links
❮ Previous Next ❯
With CSS, links can be styled in different ways.
Text Link Text Link 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;
}
»
In addition, links can be styled differently depending on what state they are in.
The four links states are:
a:link – a normal, unvisited link
a:visited – a link the user has visited
a:hover – a link when the user mouses over it
a:active – a link the moment it is clicked
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;
}
»
When setting the style for several link states, there are some order rules:
a:hover MUST come after a:link and a:visited
a:active MUST come after a:hover
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;
}
»
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;
}
»
Advanced – Link Buttons
This example demonstrates a more advanced example where we combine several CSS properties to display links as boxes/buttons:
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;
}
»
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.
Test Yourself with Exercises!
Exercise 1 » Exercise 2 » Exercise 3 » Exercise 4 »
❮ Previous Next ❯
CSS Lists
TUTORIAL HOME
CSS Lists
❮ Previous Next ❯
Coffee
Tea
Coca Cola
Coffee
Tea
Coca Cola
HTML Lists and CSS List Properties
In HTML, there are two main types of lists:
unordered lists (
- ) – the list items are marked with bullets
- 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:Coffee
Tea
Coca Cola
Coffee
Tea
Coca Cola
»
More Examples
Customized list with a red left border
This example demonstrates how to create a list with a red left border.Full-width bordered list
This example demonstrates how to create a bordered list without bullets.All the different list-item markers for lists
This example demonstrates all the different list-item markers in CSS.Test Yourself with Exercises!
Exercise 1 » Exercise 2 » Exercise 3 » Exercise 4 »All CSS List Properties
Property Description
list-style Sets all the properties for a list in one declaration
list-style-image Specifies an image as the list-item marker
list-style-position Specifies if the list-item markers should appear inside or outside the content flow
list-style-type Specifies the type of list-item marker❮ Previous Next ❯
ordered lists (
- ) – the list items are marked with numbers or letters
The CSS list properties allow you to:
Set different list item markers for ordered lists
Set different list item markers for unordered lists
Set an image as the list item marker
Add background colors to lists and list items
Different List Item Markers
The list-style-type property specifies the type of list item marker.
The following example shows some of the available list item markers:
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;
}
»
Note: Some of the values are for unordered lists, and some for ordered lists.
An Image as The List Item Marker
The list-style-image property specifies an image as the list item marker:
Example
ul {
list-style-image: url(‘sqpurple.gif’);
}
»
Position The List Item Markers
The list-style-position property specifies whether the list-item markers should appear inside or outside the content flow:
Example
ul {
list-style-position: inside;
}
»
Remove Default Settings
The list-style-type:none property can also be used to remove the markers/bullets. Note that the list also has default margin and padding. To remove this, add margin:0 and padding:0 to
- or
- :
Example
ul {
list-style-type: none;
margin: 0;
padding: 0;
}
»
List – Shorthand property
The list-style property is a shorthand property. It is used to set all the list properties in one declaration:
Example
ul {
list-style: square inside url(“sqpurple.gif”);
}
»
When using the shorthand property, the order of the property values are:
list-style-type (if a list-style-image is specified, the value of this property will be displayed if the image for some reason cannot be displayed)
list-style-position (specifies whether the list-item markers should appear inside or outside the content flow)
list-style-image (specifies an image as the list item marker)
If one of the property values above are missing, the default value for the missing property will be inserted, if any.
Styling List With Colors
We can also style lists with colors, to make them look a little more interesting.
Anything added to the
- or
- tag, affects the entire list, while properties added to the
CSS Tables
TUTORIAL HOME
CSS Tables
❮ Previous Next ❯
The look of an HTML table can be greatly improved with CSS:
Company Contact Country
Alfreds Futterkiste Maria Anders Germany
Berglunds snabbköp Christina Berglund Sweden
Centro comercial Moctezuma Francisco Chang Mexico
Ernst Handel Roland Mendel Austria
Island Trading Helen Bennett UK
Königlich Essen Philip Cramer Germany
Laughing Bacchus Winecellars Yoshi Tannamuri Canada
Magazzini Alimentari Riuniti Giovanni Rovelli Italy
Table Borders
To specify table borders in CSS, use the border property.
The example below specifies a black border for
| , and | elements:
Example | and | elements have separate borders.
Collapse Table Borders Example table, th, td {
|
|---|
