Introduction to HTML
What is HTML?EpiphanTech.blogspot.com
HTML is a language for describing web pages.- HTML stands for Hyper Text Markup Language
- HTML is not a programming language, it is a markup language
- A markup language is a set of markup tags
- HTML uses markup tags to describe web pages
HTML Tags
HTML markup tags are usually called HTML tags- HTML tags are keywords surrounded by angle brackets like <html>
- HTML tags normally come in pairs like <b> and </b>
- The first tag in a pair is the start tag, the second tag is the end tag
·
Start and end tags are also called opening
tags and closing tags.
HTML Documents - Web Pages
- HTML documents describe web pages
- HTML documents contain HTML tags and plain text
- HTML documents are also called web pages
<html> <body> <h1>My First Heading</h1> <p>My first paragraph</p> </body> </html> |
Example Explained
- The text between <html> and </html> describes the web page
- The text between <body> and </body> is the visible page content
- The text between <h1> and </h1> is displayed as a heading
- The text between <p> and </p> is displayed as a paragraph
Basic HTML Examples
HTML Headings
HTML headings are defined with the <h1> to <h6> tags.<h1>This is a heading</h1> <h2>This is a heading</h2> <h3>This is a heading</h3> |
HTML Paragraphs
HTML paragraphs are defined with the <p> tag.<p>This is a paragraph</p> <p>This is another paragraph</p> |
HTML Links
HTML links are defined with the <a> tag.<a href="http://www.w3schools.com">This is a link</a> |
HTML Images
HTML images are defined with the <img> tag.<img src="constr4.gif" width="144" height="50" /> |
HTML Headings
Headings are important in HTML documents.
HTML Headings
Headings are defined with the <h1> to <h6> tags.<h1> defines the largest heading. <h6> defines the smallest heading.
<h1>This is a heading</h1> <h2>This is a heading</h2> <h3>This is a heading</h3> |
Headings Are Important
Use the HTML heading tags for headings only. Don't use headings to make something BIG or bold.Search engines use your headings to index the structure and content of your web pages.
Since users may skim your pages by its headings, it is important to use headings to show the document structure.
H1 headings should be used as main headings, followed by H2 headings, and less important H3 headings, and so on.
HTML Comments
Comments can be inserted in the HTML code to make it more readable and understandable. Comments are ignored by the browser and not displayed.Comments are written like this:
<!-- This is a comment --> |
HTML Tip - How to View HTML Source
Have you ever seen a Web page and wondered "Hey! How did they do that?"To find out, click the VIEW option in your browser's toolbar and select SOURCE or PAGE SOURCE. This will open a window that shows you the HTML code of the page.
You will learn more about HTML tag attributes in the next chapters of this tutorial.
Tag
|
Description
|
Defines an HTML document
|
|
Defines the document's body
|
|
Defines header 1 to header 6
|
|
Defines a comment
|
HTML Paragraphs
HTML documents are divided into paragraphs.
HTML Paragraphs
Paragraphs are defined with the <p> tag.<p>This is a paragraph</p> <p>This is another paragraph</p> |
Don't Forget the End Tag
Most browsers will display HTML correctly even if you forget the end tag:<p>This is a paragraph <p>This is another paragraph |
Note: Future version of HTML will not allow you to skip end tags.
HTML Line Breaks
Use the <br /> tag if you want a line break (a new line) without starting a new paragraph:<p>This is<br />a para<br />graph with line breaks</p> |
You can read more about empty HTML tags in the next chapters of this tutorial.
<br> or <br />
In XHTML, XML, and future versions of HTML, tags with no end tags (closing tags) are not allowed.Even if <br> works in all browsers, writing <br /> instead is more future proof.
HTML Rules (Lines)
The <hr /> tag is used to create an horizontal rule (line).Example:
<p>This is a paragraph</p> <hr /> <p>This is a paragraph</p> <hr /> <p>This is a paragraph</p> |
Tag
|
Description
|
Defines a paragraph
|
|
Inserts a single line break
|
|
Defines a horizontal rule
|
HTML Links
A link is the "address" to a document (or a
resource) on the web.
Hyperlinks, Anchors, and Links
In web terms, a hyperlink is a reference (an address) to a resource on the web.Hyperlinks can point to any resource on the web: an HTML page, an image, a sound file, a movie, etc.
An anchor is a term used to define a hyperlink destination inside a document.
The HTML anchor element <a>, is used to define both hyperlinks and anchors.
We will use the term HTML link when the <a> element points to a resource, and the term HTML anchor when the <a> elements defines an address inside a document..
An HTML Link
Link syntax:<a href="url">Link text</a> |
The element content (Link text) defines the part to be displayed.
Note: The element content don't have to be a text. You can link from an image or any other HTML element.
The href Attribute
The href attribute defines the link "address".This <a> element defines a link to W3Schools:
<a href="http://www.w3schools.com/">Visit W3Schools!</a> |
The target Attribute
The target attribute defines where the linked document will be opened.The code below will open the document in a new browser window:
<a href="http://www.w3schools.com/" target="_blank">Visit W3Schools!</a> |
The name Attribute
When the name attribute is used, the <a> element defines a named anchor inside a HTML document.Named anchor are not displayed in any special way. They are invisible to the reader.
Named anchor syntax:
<a name="label">Any content</a> |
<a href="#label">Any content</a> |
Example:
A named anchor inside an HTML document:
<a name="tips">Useful Tips
Section</a>
|
A link to the Useful Tips Section from the same
document:
<a href="#tips">
Jump to the Useful Tips Section</a>
|
A link to the Useful Tips Section from another document:
<a href="http://www.w3schools.com/html_tutorial.htm#tips">
Jump to the Useful Tips Section</a>
|
Link Tags
Tag
|
Description
|
Defines an anchor
|
HTML Images
With HTML you can display images in a document.
The Image Tag and the Src Attribute
In HTML, images are defined with the <img>
tag.
The <img> tag is empty, which means that it
contains attributes only and it has no closing tag.
To display an image on a page, you need to use the src
attribute. Src stands for "source". The value of
the src attribute is the URL of the image you want to
display on your page.
The syntax of defining an image:
<img src="url"> |
The URL points to the location where the image is stored.
An image named "boat.gif" located in the directory "images"
on "www.w3schools.com" has the URL:
http://www.w3schools.com/images/boat.gif.
The browser puts the image where
the image tag occurs in the document. If you put an image tag between two
paragraphs, the browser shows the first paragraph, then the image, and then the
second paragraph.
The Alt Attribute
The alt attribute is used to define an "alternate text" for an image. The value of the alt attribute is an author-defined text:<img src="boat.gif" alt="Big Boat"> |
The "alt" attribute tells the reader what he or
she is missing on a page if the browser can't load images. The browser will
then display the alternate text instead of the image. It is a good practice to
include the "alt" attribute for each image on a page, to improve the
display and usefulness of your document for people who have text-only browsers.
Basic Notes - Useful Tips
If an HTML file contains ten images - eleven files are
required to display the page right. Loading images take time, so my best advice
is: Use images carefully.
Image Tags
Tag
|
Description
|
Defines an image
|
|
Defines an image map
|
|
Defines a clickable area inside an image map
|
HTML Text Formatting
HTML Formatting Tags
HTML uses tags like <b>
and <i> for formatting output, like bold or italic text.
These HTML tags are called
formatting tags.
Refer to the bottom of this page
for a complete reference.
Text Formatting Tags
Tag
|
Description
|
Defines bold text
|
|
Defines big text
|
|
Defines emphasized text
|
|
Defines italic text
|
|
Defines small text
|
|
Defines strong text
|
|
Defines subscripted text
|
|
Defines superscripted text
|
|
Defines inserted text
|
|
Defines deleted text
|
|
Deprecated. Use <del> instead
|
|
Deprecated. Use <del> instead
|
|
Deprecated. Use styles instead
|
"Computer Output" Tags
Tag
|
Description
|
Defines computer code text
|
|
Defines keyboard text
|
|
Defines sample computer code
|
|
Defines teletype text
|
|
Defines a variable
|
|
Defines preformatted text
|
|
<listing>
|
Deprecated. Use <pre> instead
|
<plaintext>
|
Deprecated. Use <pre> instead
|
<xmp>
|
Deprecated. Use <pre> instead
|
Citations, Quotations, and Definition
Tags
Tag
|
Description
|
Defines an abbreviation
|
|
Defines an acronym
|
|
Defines an address element
|
|
Defines the text direction
|
|
Defines a long quotation
|
|
Defines a short quotation
|
|
Defines a citation
|
|
Defines a definition term
|
HTML Elements
An HTML element is everything from the start tag to the end tag:
Start tag
|
Element
content
|
End tag
|
<p>
|
This is a
paragraph
|
</p>
|
<a
href="default.htm" >
|
This is a
link
|
</a>
|
HTML Element Syntax
- An HTML element starts with a start tag
- An HTML element ends with an end tag
- The element content is everything between the start and end tag
- Some HTML elements have empty content
- Some HTML elements have a missing end tag
HTML Document Example
<html>
<body>
<p>This is my first paragraph</p> </body>
</html>
|
<p>This is my first paragraph</p> |
The element has a start tag <p> and an end tag </p>
The element content is: This is my first paragraph
<body> <p>This is my first paragraph</p> </body> |
The element has a start tag <body> and an end tag </body>
The element content is another element (a paragraph)
<html> <body> <p>This is my first paragraph</p> </body> </html> |
The element has a start tag <html> and an end tag </html>
The element content is another element (the body)
Empty HTML Elements
HTML elements without content
are called empty elements. Empty elements have no end tag.
<br /> is an empty element
without a closing tag.
In XHTML, XML, and future
versions of HTML, all elements must be closed.
Adding a slash to the start tag,
like <br />, is the proper way of closing empty elements, accepted by
HTML, XHTML and XML.
Even if <br> works in all
browsers, writing <br /> instead is more future proof.
HTML Tip - Lowercase Tags
HTML tags are not case sensitive: <P> means the same as <p>. Plenty of web sites use uppercase HTML tags in their pages.HTML Styles
The style attribute is a new HTML attribute. It introduces CSS to
HTML.
Look! Styles and colorsThis text is in Verdana and redThis text is in Times and green This text is 30 pixels high |
The HTML Style Attribute
The purpose of the style attribute is:
To provide a common way to
style all HTML elements.
Styles was introduced with HTML
4, as the new and preferred way to style HTML elements. With HTML styles,
styles can be added to HTML elements directly by using the style attribute, or
indirectly by in separate style sheets (CSS files).
You can learn everything about
styles and CSS in our CSS tutorial.
In our HTML tutorial we use the
style attribute to introduce you to HTML styles.
HTML Style Examples
style="background-color:yellow"
style="font-size:10px"
style="font-family:Times"
style="text-align:center"
These tags and attributes should be avoided:
Tags
|
Description
|
<center>
|
Defines centered content
|
<font> and <basefont>
|
Defines HTML fonts
|
<s> and <strikeout>
|
Defines strikeout text
|
<u>
|
Defines underlined text
|
|
|
Attributes
|
|
align
|
Defines the alignment of text
|
bgcolor
|
Defines the background color
|
color
|
Defines the text color
|
Style Examples:
Background Color
<body style="background-color:yellow">The style attribute defines a style for the <body> element.
The new style attribute makes the "old" bgcolor attribute obsolete.
Font Family, Color and Size
<p style="font-family:courier new; color:red; font-size:20px">The style attribute defines a style for the <p> element.
The new style attribute makes the old <font> tag obsolete.
Text Alignment
<h1 style="text-align:center">The style attribute defines a style for the <h1> element.
The new style attribute makes the old "align" attribute obsolete.
How to Use Styles
When a browser reads a style sheet, it will format the document according to it. There are three ways of inserting a style sheet:External Style Sheet
An external style sheet is ideal when the style is applied to many pages. With an external style sheet, you can change the look of an entire Web site by changing one file. Each page must link to the style sheet using the <link> tag. The <link> tag goes inside the head section.<head> <link rel="stylesheet" type="text/css" href="mystyle.css"> </head> |
Internal Style Sheet
An internal style sheet should be used when a single document has a unique style. You define internal styles in the head section with the <style> tag.<head> <style type="text/css"> body {background-color: red} p {margin-left: 20px} </style> </head> |
Inline Styles
An inline style should be used when a unique style is to be applied to a single occurrence of an element.To use inline styles you use the style attribute in the relevant tag. The style attribute can contain any CSS property. The example shows how to change the color and the left margin of a paragraph:
<p style="color: red; margin-left: 20px"> This is a paragraph </p> |
Style Tags
Tag
|
Description
|
Defines a style definition
|
|
Defines a resource reference
|
|
Defines a section in a document
|
|
Defines a section in a document
|
|
Deprecated. Use styles instead
|
|
Deprecated. Use styles instead
|
|
Deprecated. Use styles instead
|
HTML Tables
Tables
Tables are defined with the <table> tag. A table is divided into rows (with the <tr> tag), and each row is divided into data cells (with the <td> tag). The letters td stands for "table data," which is the content of a data cell. A data cell can contain text, images, lists, paragraphs, forms, horizontal rules, tables, etc.<table border="1"> <tr> <td>row 1, cell 1</td> <td>row 1, cell 2</td> </tr> <tr> <td>row 2, cell 1</td> <td>row 2, cell 2</td> </tr> </table> |
row 1, cell 1
|
row 1, cell 2
|
row 2, cell 1
|
row 2, cell 2
|
Tables and the Border Attribute
If you do not specify a border attribute the table will be displayed without any borders. Sometimes this can be useful, but most of the time, you want the borders to show.To display a table with borders, you will have to use the border attribute:
<table border="1"> <tr> <td>Row 1, cell 1</td> <td>Row 1, cell 2</td> </tr> </table> |
Headings in a Table
Headings in a table are defined with the <th> tag.<table border="1"> <tr> <th>Heading</th> <th>Another Heading</th> </tr> <tr> <td>row 1, cell 1</td> <td>row 1, cell 2</td> </tr> <tr> <td>row 2, cell 1</td> <td>row 2, cell 2</td> </tr> </table> |
Heading
|
Another Heading
|
row 1, cell 1
|
row 1, cell 2
|
row 2, cell 1
|
row 2, cell 2
|
Empty Cells in a Table
Table cells with no content are not displayed very well in most browsers.<table border="1"> <tr> <td>row 1, cell 1</td> <td>row 1, cell 2</td> </tr> <tr> <td>row 2, cell 1</td> <td></td> </tr> </table> |
How it looks in a browser:
row 1, cell 1
|
row 1, cell 2
|
row 2, cell 1
|
To avoid this, add a non-breaking space ( ) to empty data cells, to make the borders visible:
<table border="1"> <tr> <td>row 1, cell 1</td> <td>row 1, cell 2</td> </tr> <tr> <td>row 2, cell 1</td> <td> </td> </tr> </table> |
row 1, cell 1
|
row 1, cell 2
|
row 2, cell 1
|
|
|
HTML supports ordered, unordered and definition lists.
HTML Lists
|
Ordered list
Unordered Lists
An unordered list is a list of items. The list items are marked with bullets (typically small black circles).An unordered list starts with the <ul> tag. Each list item starts with the <li> tag.
<ul> <li>Coffee</li> <li>Milk</li> </ul> |
- Coffee
- Milk
Ordered Lists
An ordered list is also a list of items. The list items are marked with numbers.An ordered list starts with the <ol> tag. Each list item starts with the <li> tag.
<ol> <li>Coffee</li> <li>Milk</li> </ol> |
- Coffee
- Milk
Definition Lists
A definition list is not a list of items. This is a list of terms and explanation of the terms.A definition list starts with the <dl> tag. Each definition-list term starts with the <dt> tag. Each definition-list definition starts with the <dd> tag.
<dl> <dt>Coffee</dt> <dd>Black hot drink</dd> <dt>Milk</dt> <dd>White cold drink</dd> </dl> |
Coffee
Black hot drink
Milk
White cold drink
Inside a definition-list definition (the <dd> tag) you can put
paragraphs, line breaks, images, links, other lists, etc.List Tags
Tag
|
Description
|
Defines an ordered list
|
|
Defines an unordered list
|
|
Defines a list item
|
|
Defines a definition list
|
|
Defines a definition term
|
|
Defines a definition description
|
|
Deprecated. Use <ul> instead
|
|
Deprecated. Use <ul> instead
|
HTML Forms and Input
HTML Forms are used to select different kinds of user
input.
Examples
Text fieldsThis example demonstrates how to create text fields on an HTML page. A user can write text in a text field.
Password fields
This example demonstrates how to create a password field on an HTML page.
Forms
A form is an area that can contain form elements.Form elements are elements that allow the user to enter information (like text fields, textarea fields, drop-down menus, radio buttons, checkboxes, etc.) in a form.A form is defined with the <form> tag.
<form> <input> <input> </form> |
Input
The most used form tag is the <input> tag. The type of input is specified with the type attribute. The most commonly used input types are explained below.Text Fields
Text fields are used when you want the user to type letters, numbers, etc. in a form.<form> First name: <input type="text" name="firstname"> <br> Last name: <input type="text" name="lastname"> </form> |
How it looks in a browser:
Note that the form itself is not visible. Also note that in most browsers, the width of the text field is 20 characters by default.
Radio Buttons
Radio Buttons are used when you want the user to select one of a limited number of choices.<form> <input type="radio" name="sex" value="male"> Male <br> <input type="radio" name="sex" value="female"> Female </form> |
Note that only one option can be chosen.
Checkboxes
Checkboxes are used when you want the user to select one or more options of a limited number of choices.<form> I have a bike: <input type="checkbox" name="vehicle" value="Bike"> <br> I have a car: <input type="checkbox" name="vehicle" value="Car"> <br> I have an airplane: <input type="checkbox" name="vehicle" value="Airplane"> </form> |
How it looks in a browser:
The Form's Action Attribute and the Submit Button
When the user clicks on the "Submit" button, the content of the form is sent to the server. The form's action attribute defines the name of the file to send the content to. The file defined in the action attribute usually does something with the received input.<form name="input" action="html_form_submit.asp" method="get"> Username: <input type="text" name="user"> <input type="submit" value="Submit"> </form> |
If you type some characters in the text field above, and click the "Submit" button, the browser will send your input to a page called "html_form_submit.asp". The page will show you the received input.
Form Tags
Tag
|
Description
|
Defines a form for user input
|
|
Defines an input field
|
|
Defines a text-area (a multi-line text input control)
|
|
Defines a label to a control
|
|
Defines a fieldset
|
|
Defines a caption for a fieldset
|
|
Defines a selectable list (a drop-down box)
|
|
Defines an option group
|
|
Defines an option in the drop-down box
|
|
Defines a push button
|
|
<isindex>
|
Deprecated. Use <input> instead
|
HTML Colors
Colors are displayed combining RED, GREEN, and BLUE light.
Color Values
HTML colors are defined using a hexadecimal (hex) notation for the combination of Red, Green, and Blue color values (RGB). The lowest value that can be given to one of the light sources is 0 (hex 00). The highest value is 255 (hex FF).Hex values are written as 3 double digit numbers, starting with a # sign.
Color
|
Color HEX
|
Color RGB
|
|
#000000
|
rgb(0,0,0)
|
|
#FF0000
|
rgb(255,0,0)
|
|
#00FF00
|
rgb(0,255,0)
|
|
#0000FF
|
rgb(0,0,255)
|
|
#FFFF00
|
rgb(255,255,0)
|
|
#00FFFF
|
rgb(0,255,255)
|
|
#FF00FF
|
rgb(255,0,255)
|
|
#C0C0C0
|
rgb(192,192,192)
|
|
#FFFFFF
|
rgb(255,255,255)
|
16 Million Different Colors
The combination of Red, Green
and Blue values from 0 to 255 gives a total of more than 16 million different
colors to play with (256 x 256 x 256).
Most modern monitors are capable
of displaying at least 16384 different colors.
If you look at the color table
below, you will see the result of varying the red light from 0 to 255, while
keeping the green and blue light at zero.
To see a full list of color
mixes when the red light varies from 0 to 255, click on one of the hex or rgb
values below.
Red Light
|
HEX
|
RGB
|
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
Shades of Gray
Gray colors are displayed using an equal amount of power
to all of the light sources. To make it easier for you to select the right gray
color we have compiled a table of gray shades for you:
|
RGB(0,0,0)
|
#000000
|
|
RGB(8,8,8)
|
#080808
|
|
RGB(16,16,16)
|
#101010
|
|
RGB(24,24,24)
|
#181818
|
|
RGB(32,32,32)
|
#202020
|
|
RGB(40,40,40)
|
#282828
|
|
RGB(48,48,48)
|
#303030
|
|
RGB(56,56,56)
|
#383838
|
|
RGB(64,64,64)
|
#404040
|
|
RGB(72,72,72)
|
#484848
|
|
RGB(80,80,80)
|
#505050
|
|
RGB(88,88,88)
|
#585858
|
|
RGB(96,96,96)
|
#606060
|
|
RGB(104,104,104)
|
#686868
|
|
RGB(112,112,112)
|
#707070
|
|
RGB(120,120,120)
|
#787878
|
|
RGB(128,128,128)
|
#808080
|
|
RGB(136,136,136)
|
#888888
|
|
RGB(144,144,144)
|
#909090
|
|
RGB(152,152,152)
|
#989898
|
|
RGB(160,160,160)
|
#A0A0A0
|
|
RGB(168,168,168)
|
#A8A8A8
|
|
RGB(176,176,176)
|
#B0B0B0
|
|
RGB(184,184,184)
|
#B8B8B8
|
|
RGB(192,192,192)
|
#C0C0C0
|
|
RGB(200,200,200)
|
#C8C8C8
|
|
RGB(208,208,208)
|
#D0D0D0
|
|
RGB(216,216,216)
|
#D8D8D8
|
|
RGB(224,224,224)
|
#E0E0E0
|
|
RGB(232,232,232)
|
#E8E8E8
|
|
RGB(240,240,240)
|
#F0F0F0
|
|
RGB(248,248,248)
|
#F8F8F8
|
|
RGB(255,255,255)
|
#FFFFFF
|
HTML Color Names
Color Names Supported by All Browsers
The list below is a complete list of the color names supported by all major browsers.You can click on a color name (or a hex value) to view the color as the background-color along with different text colors.
Sorted by Names
Link: Same list sorted by values
Color Name
|
Color HEX
|
Color
|
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
HTML Color Values
Color Names Supported by All Browsers
The list below is a complete list of the color names supported by all major browsers.You can click on a color name (or a hex value) to view the color as the background-color along with different text colors.
Sorted by HEX Value
Link: Same list sorted by names
Color Name
|
Color HEX
|
Color
|
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
Introduction to CSS
What You Should Already Know
Before you continue you should have some basic understanding of the following:- HTML / XHTML
What is CSS?
- CSS stands for Cascading Style Sheets
- Styles define how to display HTML elements
- Styles are normally stored in Style Sheets
- Styles were added to HTML 4.0 to solve a problem
- External Style Sheets can save you a lot of work
- External Style Sheets are stored in CSS files
- Multiple style definitions will cascade into one
CSS Demo
With CSS, your HTML documents can be displayed using different output styles:Styles Solve a Common Problem
HTML tags were originally designed to define the content of a document. They were supposed to say "This is a header", "This is a paragraph", "This is a table", by using tags like <h1>, <p>, <table>, and so on. The layout of the document was supposed to be taken care of by the browser, without using any formatting tags.As the two major browsers - Netscape and Internet Explorer - continued to add new HTML tags and attributes (like the <font> tag and the color attribute) to the original HTML specification, it became more and more difficult to create Web sites where the content of HTML documents was clearly separated from the document's presentation layout.
To solve this problem, the World Wide Web Consortium (W3C) - the non profit, standard setting consortium, responsible for standardizing HTML - created STYLES in addition to HTML 4.0.
All major browsers support Cascading Style Sheets.
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 four has the highest priority:
- Browser default
- External style sheet
- Internal style sheet (inside the <head> tag)
- Inline style (inside an HTML element)
So, an inline style (inside an HTML element) has the
highest priority, which means that it will override a style declared inside the
<head> tag, in an external style sheet, or in a browser (a default
value).
Note: If the external style sheet link is placed
below the internal style sheet in HTML <head>, the external style sheet
will override the internal style sheet.
Style Sheets Can Save a Lot of Work
Styles sheets define HOW HTML
elements are to be displayed, just like the font tag and the color attribute in
HTML 3.2. Styles are normally saved in external .css files. External style
sheets enable you to change the appearance and layout of all the pages in your
Web, just by editing one single CSS document!
CSS is a breakthrough in Web
design because it allows developers to control the style and layout of multiple
Web pages all at once. As a Web developer you can define a style for each HTML
element and apply it to as many Web pages as you want. To make a global change,
simply change the style, and all elements in the Web are updated automatically.
Multiple Styles Will Cascade Into One
Style sheets allow style information to be specified in
many ways. Styles can be specified inside a single HTML element, inside the
<head> element of an HTML page, or in an external CSS file. Even multiple
external style sheets can be referenced inside a single HTML document.
CSS Syntax
The CSS syntax is made up of three parts: a selector, a property and a value:selector {property: value} |
body {color: black} |
p {font-family: "sans serif"} |
p {text-align:center;color:red} |
p { text-align: center; color: black; font-family: arial } |
Grouping
You can group selectors. Separate each selector with a comma. In the example below we have grouped all the header elements. All header elements will be displayed in green text color:h1,h2,h3,h4,h5,h6 { color: green } |
The class Selector
With the class selector you can define different styles for the same type of HTML element.Say that you would like to have two types of paragraphs in your document: one right-aligned paragraph, and one center-aligned paragraph. Here is how you can do it with styles:
p.right {text-align: right} p.center {text-align: center} |
<p class="right"> This paragraph will be right-aligned. </p> <p class="center"> This paragraph will be center-aligned. </p> |
<p class="center bold"> This is a paragraph. </p> |
You can also omit the tag name in the selector to define a style that will be used by all HTML elements that have a certain class. In the example below, all HTML elements with class="center" will be center-aligned:
.center {text-align: center} |
<h1 class="center"> This heading will be center-aligned </h1> <p class="center"> This paragraph will also be center-aligned. </p> |
Do NOT start a class name
with a number! It will not work in Mozilla/Firefox.
|
Add Styles to Elements with Particular Attributes
You can also apply styles to HTML elements with particular attributes.The style rule below will match all input elements that have a type attribute with a value of "text":
input[type="text"] {background-color: blue} |
The id Selector
You can also define styles for HTML elements with the id selector. The id selector is defined as a #.The style rule below will match the element that has an id attribute with a value of "green":
#green {color: green} |
p#para1 { text-align: center; color: red } |
Do NOT start an ID name
with a number! It will not work in Mozilla/Firefox.
|
CSS Comments
Comments are used to explain your code, and may help you when you edit the source code at a later date. A comment will be ignored by browsers. A CSS comment begins with "/*", and ends with "*/", like this:/* This is a comment */ p { text-align: center; /* This is another comment */ color: black; font-family: arial } |
How to Insert a Style Sheet
When a browser reads a style sheet, it will format the document according to it. There are three ways of inserting a style sheet:External Style Sheet
An external style sheet is ideal when the style is applied to many pages. With an external style sheet, you can change the look of an entire Web site by changing one file. Each page must link to the style sheet using the <link> tag. The <link> tag goes inside the head section:<head> <link rel="stylesheet" type="text/css" href="mystyle.css" /> </head> |
An external style sheet can be written in any text editor. The file should not contain any html tags. Your style sheet should be saved with a .css extension. An example of a style sheet file is shown below:
hr {color: sienna} p {margin-left: 20px} body {background-image: url("images/back40.gif")} |
Do NOT leave spaces between the property value and the units! If
you use "margin-left: 20 px" instead of "margin-left:
20px" it will only work properly in IE6 but it will not work in
Mozilla/Firefox or Netscape. |
Internal Style Sheet
An internal style sheet should be used when a single document has a unique style. You define internal styles in the head section by using the <style> tag, like this:<head> <style type="text/css"> hr {color: sienna} p {margin-left: 20px} body {background-image: url("images/back40.gif")} </style> </head> |
Note: A browser normally ignores unknown tags. This means that an old browser that does not support styles, will ignore the <style> tag, but the content of the <style> tag will be displayed on the page. It is possible to prevent an old browser from displaying the content by hiding it in the HTML comment element:
<head> <style type="text/css"> <!-- hr {color: sienna} p {margin-left: 20px} body {background-image: url("images/back40.gif")} --> </style> </head> |
Inline Styles
An inline style loses many of
the advantages of style sheets by mixing content with presentation. Use this
method sparingly, such as when a style is to be applied to a single occurrence
of an element.
To use inline styles you use the
style attribute in the relevant tag. The style attribute can contain any CSS
property. The example shows how to change the color and the left margin of a
paragraph:
<p style="color: sienna; margin-left: 20px"> This is a paragraph </p> |
Multiple Style Sheets
If some properties have been set for the same selector in different style sheets, the values will be inherited from the more specific style sheet.For example, an external style sheet has these properties for the h3 selector:
h3 { color: red; text-align: left; font-size: 8pt } |
h3 { text-align: right; font-size: 20pt } |
color: red; text-align: right; font-size: 20pt |
CSS Background Properties
The CSS background properties allow you to control the background color of an element, set an image as the background, repeat a background image vertically or horizontally, and position an image on a page.Browser support: IE: Internet Explorer, F: Firefox, N: Netscape.
W3C: The number in the "W3C" column indicates in which CSS recommendation the property is defined (CSS1 or CSS2).
Example
<html>
<head>
<style type="text/css">
body {background-color: yellow}
h1 {background-color: #00ff00}
h2 {background-color: transparent}
p {background-color: rgb(250,0,255)}
</style>
</head>
<body>
<h1>This is header 1</h1>
<h2>This is header 2</h2>
<p>This is a paragraph</p>
</body>
</html>
Property
|
Description
|
Values
|
IE
|
F
|
N
|
W3C
|
A shorthand property for setting all background
properties in one declaration
|
background-color
background-image background-repeat background-attachment background-position |
4
|
1
|
6
|
1
|
|
Sets whether a background image is fixed or scrolls
with the rest of the page
|
scroll
fixed |
4
|
1
|
6
|
1
|
|
Sets the background color of an element
|
color-rgb
color-hex color-name transparent |
4
|
1
|
4
|
1
|
|
Sets an image as the background
|
url(URL)
none |
4
|
1
|
4
|
1
|
|
Sets the starting position of a background image
|
top left top center top right center left center center center right bottom left
bottom center bottom right x% y% xpos ypos
|
4
|
1
|
6
|
1
|
|
Sets if/how a background image will be repeated
|
repeat
repeat-x repeat-y no-repeat |
4
|
1
|
4
|
1
|
CSS Text Properties
The CSS text properties allow you to control the appearance of text. It is possible to change the color of a text, increase or decrease the space between characters in a text, align a text, decorate a text, indent the first line in a text, and more.Browser support: IE: Internet Explorer, F: Firefox, N: Netscape.
W3C: The number in the "W3C" column indicates in which CSS recommendation the property is defined (CSS1 or CSS2).
Example
<html>
<head>
<style type="text/css">
h1 {color: #00ff00}
h2 {color: #dda0dd}
p {color: rgb(0,0,255)}
</style>
</head>
<body>
<h1>This is header 1</h1>
<h2>This is header 2</h2>
<p>This is a paragraph</p>
</body>
</html>
Property
|
Description
|
Values
|
IE
|
F
|
N
|
W3C
|
Sets the color of a text
|
color
|
3
|
1
|
4
|
1
|
|
Sets the text direction
|
ltr
rtl |
6
|
1
|
6
|
2
|
|
Sets the distance between lines
|
normal
number length % |
4
|
1
|
4
|
1
|
|
Increase or decrease the space between characters
|
normal
length |
4
|
1
|
6
|
1
|
|
Aligns the text in an element
|
left
right center justify |
4
|
1
|
4
|
1
|
|
Adds decoration to text
|
none
underline overline line-through blink |
4
|
1
|
4
|
1
|
|
Indents the first line of text in an element
|
length
% |
4
|
1
|
4
|
1
|
|
text-shadow
|
|
none
color length |
|
|
|
|
Controls the letters in an element
|
none
capitalize uppercase lowercase |
4
|
1
|
4
|
1
|
|
unicode-bidi
|
|
normal
embed bidi-override |
5
|
|
|
2
|
Sets how white space inside an element is handled
|
normal
pre nowrap |
5
|
1
|
4
|
1
|
|
Increase or decrease the space between words
|
normal
length |
6
|
1
|
6
|
1
|
CSS Font Properties
The CSS font properties allow you to change the font
family, boldness, size, and the style of a text.
Note: In CSS1
fonts are identified by a font name. If a browser does not support the
specified font, it will use a default font.
Browser support:
IE: Internet Explorer, F: Firefox, N: Netscape.
W3C: The number in the "W3C" column indicates
in which CSS recommendation the property is defined (CSS1 or CSS2).
Example
<html>
<head>
<style type="text/css">
h3 {font-family: times}
p {font-family: courier}
p.sansserif {font-family: sans-serif}
</style>
</head>
<body>
<h3>This is header 3</h3>
<p>This is a paragraph</p>
<p class="sansserif">This is a
paragraph</p>
</body>
</html>
Property
|
Description
|
Values
|
IE
|
F
|
N
|
W3C
|
A shorthand property for setting all of the properties
for a font in one declaration
|
font-style
font-variant font-weight font-size/line-height font-family caption icon menu message-box small-caption status-bar |
4
|
1
|
4
|
1
|
|
A prioritized list of font family names and/or generic
family names for an element
|
family-name
generic-family |
3
|
1
|
4
|
1
|
|
Sets the size of a font
|
xx-small
x-small small medium large x-large xx-large smaller larger length % |
3
|
1
|
4
|
1
|
|
Specifies an aspect value for an element that will
preserve the x-height of the first-choice font
|
none
number |
-
|
-
|
-
|
2
|
|
Condenses or expands the current font-family
|
normal
wider narrower ultra-condensed extra-condensed condensed semi-condensed semi-expanded expanded extra-expanded ultra-expanded |
-
|
-
|
-
|
2
|
|
Sets the style of the font
|
normal
italic oblique |
4
|
1
|
4
|
1
|
|
Displays text in a small-caps font or a normal font
|
normal
small-caps |
4
|
1
|
6
|
1
|
|
Sets the weight of a font
|
normal
bold bolder lighter 100 200 300 400 500 600 700 800 900 |
4
|
1
|
4
|
1
|
CSS Border Properties
The CSS border properties allow you to specify the style and color of an element's border. In HTML we use tables to create borders around a text, but with the CSS border properties we can create borders with nice effects, and it can be applied to any element.Browser support: IE: Internet Explorer, F: Firefox, N: Netscape.
W3C: The number in the "W3C" column indicates in which CSS recommendation the property is defined (CSS1 or CSS2).
Example
<html>
<head>
<style type="text/css">
p
{
border: medium double rgb(250,0,255)
}
</style>
</head>
<body>
<p>Some text</p>
</body>
</html>
Property
|
Description
|
Values
|
IE
|
F
|
N
|
W3C
|
A shorthand property for setting all of the properties
for the four borders in one declaration
|
border-width
border-style border-color |
4
|
1
|
4
|
1
|
|
A shorthand property for setting all of the properties
for the bottom border in one declaration
|
border-bottom-width
border-style border-color |
4
|
1
|
6
|
1
|
|
Sets the color of the bottom border
|
border-color
|
4
|
1
|
6
|
2
|
|
Sets the style of the bottom border
|
border-style
|
4
|
1
|
6
|
2
|
|
Sets the width of the bottom border
|
thin
medium thick length |
4
|
1
|
4
|
1
|
|
Sets the color of the four borders, can have from one
to four colors
|
color
|
4
|
1
|
6
|
1
|
|
A shorthand property for setting all of the properties
for the left border in one declaration
|
border-left-width
border-style border-color |
4
|
1
|
6
|
1
|
|
Sets the color of the left border
|
border-color
|
4
|
1
|
6
|
2
|
|
Sets the style of the left border
|
border-style
|
4
|
1
|
6
|
2
|
|
Sets the width of the left border
|
thin
medium thick length |
4
|
1
|
4
|
1
|
|
A shorthand property for setting all of the properties
for the right border in one declaration
|
border-right-width
border-style border-color |
4
|
1
|
6
|
1
|
|
Sets the color of the right border
|
border-color
|
4
|
1
|
6
|
2
|
|
Sets the style of the right border
|
border-style
|
4
|
1
|
6
|
2
|
|
Sets the width of the right border
|
thin
medium thick length |
4
|
1
|
4
|
1
|
|
Sets the style of the four borders, can have from one
to four styles
|
none
hidden dotted dashed solid double groove ridge inset outset |
4
|
1
|
6
|
1
|
|
A shorthand property for setting all of the properties
for the top border in one declaration
|
border-top-width
border-style border-color |
4
|
1
|
6
|
1
|
|
Sets the color of the top border
|
border-color
|
4
|
1
|
6
|
2
|
|
Sets the style of the top border
|
border-style
|
4
|
1
|
6
|
2
|
|
Sets the width of the top border
|
thin
medium thick length |
4
|
1
|
4
|
1
|
|
A shorthand property for setting the width of the four
borders in one declaration, can have from one to four values
|
thin
medium thick length |
CSS Outline Properties
An outline is a line that is drawn around elements, outside the border edge, to make the element "stand out".The CSS outline properties sets the outlines around elements. You can specify the style, color, and width of the outline.
Note: Outlines do not take up space, and they do not have to be rectangular.
Browser support: IE: Internet Explorer, F: Firefox, N: Netscape.
W3C: The number in the "W3C" column indicates in which CSS recommendation the property is defined (CSS1 or CSS2).
Example
<html>
<head>
<style type="text/css">
p
{
border: red solid thin;
outline: green dotted thick
}
</style>
</head>
<body>
<p>Some text.</p>
</body>
</html>
Property
|
Description
|
Values
|
IE
|
F
|
N
|
W3C
|
A shorthand property for setting all the outline
properties in one declaration
|
outline-color
outline-style outline-width |
-
|
1.5
|
-
|
2
|
|
Sets the color of the outline around an element
|
color
invert |
-
|
1.5
|
-
|
2
|
|
Sets the style of the outline around an element
|
none
dotted dashed solid double groove ridge inset outset |
-
|
1.5
|
-
|
2
|
|
Sets the width of the outline around an element
|
thin
medium thick length |
-
|
1.5
|
-
|
CSS Margin Properties
The CSS margin properties define the space around
elements. It is possible to use negative values to overlap content. The top,
right, bottom, and left margin can be changed independently using separate
properties. A shorthand margin property can also be used
to change all of the margins at once.
Note: Netscape and IE give the body tag a default
margin of 8px. Opera does not! Instead, Opera applies a default padding of 8px,
so if one wants to adjust the margin for an entire page and have it display
correctly in Opera, the body padding must be set as well!
Browser support: IE: Internet Explorer, F:
Firefox, N: Netscape.
W3C: The number in the "W3C" column
indicates in which CSS recommendation the property is defined (CSS1 or CSS2).
Example
<html>
<head>
<style type="text/css">
p.margin {margin: 2cm 4cm 3cm 4cm}
</style>
</head>
<body>
<p>This is a paragraph with no specified
margins</p>
<p class="margin">This is a paragraph
with specified margins</p>
<p>This is a paragraph with no specified
margins</p>
</body>
</html>
Property
|
Description
|
Values
|
IE
|
F
|
N
|
W3C
|
A shorthand property for setting the margin properties in
one declaration
|
margin-top
margin-right margin-bottom margin-left |
4
|
1
|
4
|
1
|
|
margin-bottom
|
Sets the bottom margin of an element
|
auto
length % |
4
|
1
|
4
|
1
|
margin-left
|
Sets the left margin of an element
|
auto
length % |
3
|
1
|
4
|
1
|
margin-right
|
Sets the right margin of an element
|
auto
length % |
3
|
1
|
4
|
1
|
Sets the top margin of an element
|
auto
length % |
3
|
1
|
4
|
1
|
CSS Padding Properties
The CSS padding properties define the space between the
element border and the element content. Negative values are not allowed. The
top, right, bottom, and left padding can be changed independently using
separate properties. A shorthand padding property is also created to control
multiple sides at once.
Browser support: IE: Internet Explorer, F:
Firefox, N: Netscape.
W3C: The number in the "W3C" column
indicates in which CSS recommendation the property is defined (CSS1 or CSS2).
Example
<html>
<head>
<style type="text/css">
td.test1 {padding: 1.5cm}
td.test2 {padding: 0.5cm 2.5cm}
</style>
</head>
<body>
<table border="1">
<tr>
<td class="test1">
This is a tablecell with equal padding on each side.
</td>
</tr>
</table>
<br />
<table border="1">
<tr>
<td class="test2">
This tablecell has a top and bottom padding of 0.5cm and
a left and right padding of 2.5cm.
</td>
</tr>
</table>
</body>
</html>
Property
|
Description
|
Values
|
IE
|
F
|
N
|
W3C
|
A shorthand property for setting all of the padding
properties in one declaration
|
padding-top
padding-right padding-bottom padding-left |
4
|
1
|
4
|
1
|
|
padding-bottom |
Sets the bottom padding of an element
|
length
% |
4
|
1
|
4
|
1
|
padding-left |
Sets the left padding of an element
|
length
% |
4
|
1
|
4
|
1
|
padding-right |
Sets the right padding of an element
|
length
% |
4
|
1
|
4
|
1
|
Sets the top padding of an element
|
length
% |
4
|
1
|
4
|
1
|
CSS List Properties
The CSS list properties allow you to place the list-item
marker, change between different list-item markers, or set an image as the
list-item marker.
Browser support: IE: Internet Explorer, F:
Firefox, N: Netscape.
W3C: The number in the "W3C" column
indicates in which CSS recommendation the property is defined (CSS1 or CSS2).
Example
<html>
<head>
<style type="text/css">
ul.disc {list-style-type: disc}
ul.circle {list-style-type: circle}
ul.square {list-style-type: square}
ul.none {list-style-type: none}
</style>
</head>
<body>
<ul class="disc">
<li>Coffee</li>
<li>Tea</li>
<li>Coca Cola</li>
</ul>
<ul class="circle">
<li>Coffee</li>
<li>Tea</li>
<li>Coca Cola</li>
</ul>
<ul class="square">
<li>Coffee</li>
<li>Tea</li>
<li>Coca Cola</li>
</ul>
<ul class="none">
<li>Coffee</li>
<li>Tea</li>
<li>Coca Cola</li>
</ul>
</body>
</html>
Property
|
Description
|
Values
|
IE
|
F
|
N
|
W3C
|
A shorthand property for setting all of the properties for
a list in one declaration
|
list-style-type
list-style-position list-style-image |
4
|
1
|
6
|
1
|
|
Sets an image as the list-item marker
|
none
url |
4
|
1
|
6
|
1
|
|
Sets where the list-item marker is placed in the list
|
inside
outside |
4
|
1
|
6
|
1
|
|
Sets the type of the list-item marker
|
none
disc circle square decimal decimal-leading-zero lower-roman upper-roman lower-alpha upper-alpha lower-greek lower-latin upper-latin hebrew armenian georgian cjk-ideographic hiragana katakana hiragana-iroha katakana-iroha |
4
|
1
|
4
|
1
|
|
marker-offset
|
|
auto
length |
|
1
|
7
|
2
|
CSS Table Properties
The CSS table properties allow you to set the layout of a
table.
Browser support: IE: Internet Explorer, M: Mac IE
only, F: Firefox, N: Netscape.
W3C: The number in the "W3C" column
indicates in which CSS recommendation the property is defined (CSS1 or CSS2).
Example
<html>
<head>
<style type="text/css">
table.one
{
table-layout: automatic
}
table.two
{
table-layout: fixed
}
</style>
</head>
<body>
<table class="one" border="1"
width="100%">
<tr>
<td
width="20%">1000000000000000000000000000</td>
<td width="40%">10000000</td>
<td width="40%">100</td>
</tr>
</table>
<br />
Property
|
Description
|
Values
|
IE
|
F
|
N
|
W3C
|
Sets whether the table borders are collapsed into a single
border or detached as in standard HTML
|
collapse
separate |
5
|
1
|
7
|
2
|
|
Sets the distance that separates cell borders (only for
the "separated borders" model)
|
length length
|
5M
|
1
|
6
|
2
|
|
Sets the position of the table caption
|
top
bottom left right |
5M
|
1
|
6
|
2
|
|
Sets whether or not to show empty cells in a table (only
for the "separated borders" model)
|
show
hide |
5M
|
1
|
6
|
2
|
|
Sets the algorithm used to display the table cells, rows,
and columns
|
auto
fixed |
5
|
1
|
6
|
2
|
No comments:
Post a Comment