Manipulate Text
Colors, Boxes
<!DOCTYPE html>
<html>
<head>
<style>
body {background-color:lightgrey}
h1 {color:blue}
p {color:green}
</style>
</head>
CSS styling can be added to HTML elements the following 3 ways:
The common way to add styling, is to put CSS syntax in separate CSS files.
In this tutorial the examples are simplified to make it easier for you to try it yourself.
You will learn much more about CSS in our CSS Tutorial |
Inline styling is useful for applying a unique style to a single HTML element:
Inline styling uses the style attribute.
This line styling changes the text color and the left margin of single paragraph:
<p style="color:green;margin-left:20px;">This is a paragraph.</p>
The CSS style background-color defines the background color for an HTML element:
<body style="background-color:lightgrey">
The CSS style font-family defines the font to be used for in HTML element:
<!DOCTYPE html>
<html>
<body>
<h1 style="font-family:verdana">This is a heading</h1>
<p style="font-family:courier">This is a paragraph.</p>
</body>
</html>
Obsolete style attributes, like bgcolor, and tags like <font>, do not validate in HTML5. |
The CSS style font-size defines the text size to be used for in HTML element:
<!DOCTYPE html>
<html>
<body>
<h1 style="font-size:300%">This is a heading</h1>
<p style="font-size:160%">This is a paragraph.</p>
</body>
</html>
The CSS style color defines the text color to be used for in HTML element:
<!DOCTYPE html>
<html>
<body>
<h1 style="color:blue">This is a heading</h1>
<p style="color:red">This is a paragraph.</p>
</body>
</html>
The CSS style text-align specifies the horizontal text alignment for an element:
<!DOCTYPE html>
<html>
<body>
<h1 style="text-align:center">Center-aligned heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
The text-align property makes the old <center> tag obsolete.
An internal style sheet can be used to define a common style for all HTML elements on a page.
Internal styles are defined in the <head> section of an HTML page, in the <style> element:
<!DOCTYPE html>
<html>
<head>
<style>
body {background-color:lightgrey}
h1 {color:blue}
p {color:green}
</style>
</head>
External style sheet are ideal when the style is applied to many pages.
With external style sheets, you can change the look of an entire site by changing one file.
External styles are defined in the <head> section of an HTML page, in the <link> element:
<DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>I am formatted with an external style sheet</h1>
<p>Me too!</p>
</body>
</html>
All the examples above use CSS to style HTML elements in a general way.
The CSS styles define an equal style for all equal elements.
To define a special style for a special element, add an id attribute to the element:
<p id="p01">I am different</p>
Now you can define a different style for this single element:
p#p01 {
color:blue;
}
To define a style for a special type (class) of elements, add a class attribute to the element:
<p class="error">I am different</p>
Now you can define a different style for this type (class) of element:
p.error {
color:red;
}
Use id to address single elements. Use class to address groups of elements. |
In older HTML versions, several tags and attributes were used to style documents.
These tags are not supported in HTML5.
Avoid using these elements: <font>, <center> and <strike>.
Avoid using these attributes: color and bgcolor.
HTML with CSS background-color
HTML styled using the id attribute
HTML styled using the class attribute
Exercise 1 » Exercise 2 » Exercise 3 » Exercise 4 »
Tag |
Description |
Defines style information for a document |
|
Defines the relationship between a document and an external resource |
Links are found in nearly all web pages. Links allow users to click their way from page to page.
HTML links are hyperlinks.
A hyperlink is an element, a text, or an image that you can click on, and jump to another document.
In HTML, links are defined with the <a> tag:
<a href="url">link text</a>
<a href="http://www.w3schools.com/html/">Visit our HTML tutorial</a>
The href attribute specifies the destination address (http://www.w3schools.com/html/)
The link text is the visible part (Visit our HTML tutorial).
Clicking on the link text, will send you to the specified address.
The link text does not have to be text. It can be an HTML image or any other HTML element. |
The example above used an absolute URL (A full web address).
A local link (link to the same web site) is specified with a relative URL (without http://www....).
<a href="html_images.asp">HTML Images</a>
When you move the mouse cursor over a link, two things will normally happen:
By default, links will appear as this in all browsers:
You can change the default colors, using styles:
<style>
a a:link {color:#000000; background-color:transparent}
a:visited {color:#000000; background-color:transparent}
a:hover {color:#ff0000; background-color:transparent}
a:active {color:#ff0000; background-color:transparent}
</style>
The target attribute specifies where to open the linked document.
This example will open the linked document in a new browser window or in a new tab:
<a href="http://www.w3schools.com/" target="_blank">Visit W3Schools!</a>
Target Value |
Description |
_blank |
Opens the linked document in a new window or tab |
_self |
Opens the linked document in the same frame as it was clicked (this is default) |
_parent |
Opens the linked document in the parent frame |
_top |
Opens the linked document in the full body of the window |
framename |
Opens the linked document in a named frame |
If your webpage is locked in a frame, you can use target="_top" to break out of the frame:
<a href="http://www.w3schools.com/html/" target="_top">HTML5 tutorial!</a>
It is common to use images as links:
<a href="default.asp">
<img src="smiley.gif" alt="HTML tutorial" style="width:42px;height:42px;border:0">
</a>
border:0 is added to prevent IE9 (and earlier) from displaying a border around the image. |
The id attribute can be used to create bookmarks inside HTML documents.
Bookmarks are not displayed in any special way. They are invisible to the reader.
Add an id attribute to any <a> element:
<a id="tips">Useful Tips Section</a>
Then create a link to the <a> element (Useful Tips Section):
<a href="#tips">Visit the Useful Tips Section</a>
Or, create a link to the <a> element (Useful Tips Section) from another page:
<a href="http://www.w3schools.com/html_links.htm#tips">Visit the Useful Tips Section</a>
Without a trailing slash on subfolder addresses, you might generate two requests to the server. |
An HTML link, using an absolute URL
An HTML link, using a relative URL
Changing the color of an HTML link
Removing the underline from HTML links
Setting the target of an HTML link
An HTML a mailto link with a subject
Exercise 1 » Exercise 2 » Exercise 3 » Exercise 4 » Exercise 5 »
Tag |
Description |
Defines a hyperlink |
In HTML, images are defined with the <img> tag.
The <img> tag is empty, it contains attributes only, and does not have a closing tag.
The src attribute defines the url (web address) of the image:
<img src="url" alt="some_text">
The alt attribute specifies an alternate text for the image, if it cannot be displayed.
The value of the alt attribute should describe the image in words:
<img src="html5.gif" alt="The official HTML5 Icon">
The alt attribute is required. A web page will not validate correctly without it.
Screen readers are software programs that can read what is displayed on a screen.
Used on the web, screen readers can "reproduce" HTML as text-to-speech, sound icons, or braille output.
Screen readers are used by people who are blind, visually impaired, or learning disabled.
Screen readers can read the alt attribute. |
You can use the style attribute to specify the width and height of an image.
The values are specified in pixels (use px after the value):
<img src="html5.gif" alt="HTML5 Icon" style="width:128px;height:128px">
Alternatively, you can use width and height attributes.
The values are specified in pixels (without px after the value):
<img src="html5.gif" alt="HTML5 Icon" width="128" height="128">
Both the width, the height, and the style attributes, are valid in the latest HTML5 standard.
We suggest you use the style attribute. It prevents styles sheets to change the default size of images:
<!DOCTYPE html>
<html>
<head>
<style>
img { width:100%; }
</style>
</head>
<body>
<img src="html5.gif" alt="HTML5 Icon" style="width:128px;height:128px">
<img src="html5.gif" alt="HTML5 Icon" width="128" height="128">
</body>
</html>
At W3schools we prefer to use the style attribute. |
If not specified, the browser expects to find the image in the same folder as the web page.
However, it is common on the web, to store images in a sub-folder, and refer to the folder in the image name:
<img src="/images/html5.gif" alt="HTML5 Icon" style="width:128px;height:128px">
If a browser cannot find an image, it will display a broken link icon:
<img src="wrongname.gif" alt="HTML5 Icon" style="width:128px;height:128px">
Some web sites store their images on image servers.
Actually, you can access images from any web address in the world:
<img src="http://www.w3schools.com/images/w3schools_green.jpg">
The GIF standard allows moving images:
<img src="programming.gif" alt="Computer Man" style="width:48px;height:48px">
Note that the syntax of inserting moving images is no different from non-moving images.
It is common to use images as links:
<a href="default.asp">
<img src="smiley.gif" alt="HTML tutorial" style="width:42px;height:42px;border:0">
</a>
We have added border:0 to prevent IE9 (and earlier) from displaying a border around the image. |
For an image, you can create an image map, with clickable areas:
<img src="planets.gif" alt="Planets" usemap="#planetmap" style="width:145px;height:126px">
<map name="planetmap">
<area shape="rect" coords="0,0,82,126" alt="Sun" href="sun.htm">
<area shape="circle" coords="90,58,3" alt="Mercury" href="mercur.htm">
<area shape="circle" coords="124,58,8" alt="Venus" href="venus.htm">
</map>
You can let an image float to the left or right of a paragraph:
<p>
<img src="smiley.gif" alt="Smiley face" style="float:left;width:42px;height:42px">
A paragraph with an image. The image floats to the left of the text.
</p>
Loading images takes time. Large images can slow down your page. Use images carefully. |
HTML image height and width using attributes
HTML image height and width using CSS
HTML image height and width using both
An HTML image in another folder
An HTML image with a broken link
An HTML image on another server
An HTML image map with clickable regions
Exercise 1 » Exercise 2 » Exercise 3 » Exercise 4 »
Tag |
Description |
Defines an image |
|
Defines an image-map |
|
Defines a clickable area inside an image-map |
Number |
First Name |
Last Name |
Points |
1 |
Eve |
Jackson |
94 |
2 |
John |
Doe |
80 |
3 |
Adam |
Johnson |
67 |
4 |
Jill |
Smith |
50 |
<table style="width:100%">
<tr>
<td>Jill</td>
<td>Smith</td>
<td>50</td>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
</table>
Example explained:
Tables are defined with the <table> tag.
Tables are divided into table rows with the <tr> tag.
Table rows are divided into table data with the <td> tag.
A table row can also be divided into table headings with the <th> tag.
Table data <td> are the data containers of the table. |
If you do not specify a border for the table, it will be displayed without borders.
A border can be added using the border attribute:
<table border="1" style="width:100%">
<tr>
<td>Jill</td>
<td>Smith</td>
<td>50</td>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
</table>
The border attribute is on its way out of the HTML standard! It is better to use CSS. |
To add borders, use the CSS border property:
table, th, td {
border: 1px solid black;
}
Remember to define borders for both the table and the table cells.
If you want the borders to collapse into one border, add CSS border-collapse:
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
Cell padding specifies the space between the cell content and its borders.
If you do not specify a padding, the table cells will be displayed without padding.
To set the padding, use the CSS padding property:
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th,td {
padding: 15px;
}
Table headings are defined with the <th> tag.
By default, all major browsers display table headings as bold and centered:
<table style="width:100%">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Points</th>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
</table>
To left-align the table headings, use the CSS text-align property:
th {
text-align: left;
}
Border spacing specifies the space between the cells.
To set the border spacing for a table, use the CSS border-spacing property:
table {
border-spacing: 5px;
}
If the table has collapsed borders, border-spacing has no effect. |
To make a cell span more than one column, use the colspan attribute:
<table style="width:100%">
<tr>
<th>Name</th>
<th colspan="2">Telephone</th>
</tr>
<tr>
<td>Bill Gates</td>
<td>555 77 854</td>
<td>555 77 855</td>
</tr>
</table>
To make a cell span more than one row, use the rowspan attribute:
<table style="width:100%">
<tr>
<th>First Name:</th>
<td>Bill Gates</td>
</tr>
<tr>
<th rowspan="2">Telephone:</th>
<td>555 77 854</td>
</tr>
<tr>
<td>555 77 855</td>
</tr>
</table>
To add a caption to a table, use the <caption> tag:
<table style="width:100%">
<caption>Monthly savings</caption>
<tr>
<th>Month</th>
<th>Savings</th>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$50</td>
</tr>
</table>
The <caption> tag must be inserted immediately after the <table> tag. |
Most of the examples above use a style attribute (width="100%") to define the width of each table.
This makes it easy to define different widths for different tables.
The styles in the <head> section, however, define a style for all tables in a page.
To define a special style for a special table, add an id attribute to the table:
<table id="t01">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Points</th>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
</table>
table#t01 {
width: 100%;
background-color: #f1f1c1;
}
table#t01 tr:nth-child(even) {
background-color: #eee;
}
table#t01 tr:nth-child(odd) {
background-color: #fff;
}
table#t01 th {
color: white;
background-color: black;
}
An HTML table with collapsed borders
An HTML table with cell padding
An HTML table with left-aligned headings
Horizontal/Vertical HTML table headings
HTML table cells that span more than one column
HTML table cells that span more than one row
An HTML table with cell spacing
An HTML table with HTML tags inside