Sunteți pe pagina 1din 33

What is HTML?

HTML is a language for describing web pages.


HTML stands for Hyper Text Markup Language HTML is a markup language A markup language is a set of markup tags The tags describe document content HTML documents contain HTML tags and plain text HTML documents are also called web pages

HTML Tags
HTML markup tags are usually called HTML tags

HTML tags are keywords (tag names) 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 The end tag is written like the start tag, with a forward slash before the tag name Start and end tags are also called opening tags and closing tags

HTML Element Syntax


An HTML element starts with a start tag / opening tag An HTML element ends with an end tag / closing tag The element content is everything between the start and the end tag Some HTML elements have empty content Empty elements are closed in the start tag Most HTML elements can have attributes

Nested HTML Elements


Most HTML elements can be nested (can contain other HTML elements). HTML documents consist of nested HTML elements.

Don't Forget the End Tag


Some HTML elements might display correctly even if you forget the end tag: <p>This is a paragraph <p>This is a paragraph The example above works in most browsers, because the closing tag is considered optional. Never rely on this. Many HTML elements will produce unexpected results and/or errors if you forget the end tag .

Empty HTML Elements


HTML elements with no content are called empty elements. <br> is an empty element without a closing tag (the <br> tag defines a line break). Tip: In XHTML, all elements must be closed. Adding a slash inside the start tag, like <br />, is the proper way of closing empty elements in XHTML (and XML).

HTML Tip: Use Lowercase Tags


HTML tags are not case sensitive: <P> means the same as <p>. Many web sites
use uppercase HTML tags. W3Schools use lowercase tags because the World Wide Web Consortium (W3C) recommends lowercase in HTML 4, and demands lowercase tags in XHTML

HTML Versions
Since the early days of the web, there have been many versions of HTML: Version HTML HTML+ HTML 2.0 HTML 3.2 HTML 4.01 XHTML 1.0 HTML5 XHTML5 Year 1991 1993 1995 1997 1999 2000 2012 2013

Web Browsers
The purpose of a web browser (such as Google Chrome, Internet Explorer, Firefox, Safari) is to read HTML documents and display them as web pages. The browser does not display the HTML tags, but uses the tags to interpret the content of the page:

HTML Editors
HTML can be edited by using a professional HTML editor like:

Adobe Dreamweaver Visual Studio Microsoft Expression Web CoffeeCup HTML Editor

However, for learning HTML we can also use a simple text editor like Notepad (PC) or TextEdit (Mac). Using a simple text editor is a good way to learn HTML. Follow the 4 steps below to create your first web page with Notepad.

Step 1: Start Notepad


To start Notepad go to: Start All Programs

Accessories Notepad

Step 2: Edit Your HTML with Notepad


Type your HTML code into your Notepad:

Step 3: Save Your HTML


Select Save as.. in Notepad's file menu. When you save an HTML file, you can use either the .htm or the .html file extension. There is no difference, it is entirely up to you. Save the file in a folder that is easy to remember, like w3schools.

Step 4: Run the HTML in Your Browser


Start your web browser and open your html file from the File, Open menu, or just browse the folder and double-click your HTML file. The result should look much like this:

The <!DOCTYPE> Declaration


The <!DOCTYPE> declaration helps the browser to display a web page correctly. The DOCTYPE declaration defines the document type. The <!DOCTYPE> declaration must be the very first thing in your HTML document, before the <html> tag. The <!DOCTYPE> declaration is not an HTML tag; it is an instruction to the web browser about what version of HTML the page is written in. In HTML 4.01, the <!DOCTYPE> declaration refers to a DTD, because HTML 4.01 was based on SGML. The DTD specifies the rules for the markup language, so that the browsers render the content correctly. HTML5 is not based on SGML, and therefore does not require a reference to a DTD.

Tip: Always add the <!DOCTYPE> declaration to your HTML documents, so that the browser knows what type of document to expect. There are many different documents on the web, and a browser can only display an HTML page 100% correctly if it knows the HTML type and version used.

Common Declarations
HTML5 <!DOCTYPE html> HTML 4.01 Transitional <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> HTML 4.01 Strict

This DTD contains all HTML elements and attributes, but does NOT INCLUDE presentational or deprecated elements (like font). Framesets are not allowed.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> HTML 4.01 Frameset

This DTD is equal to HTML 4.01 Transitional, but allows the use of frameset content.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">

XHTML 1.0 Transitional <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> XHTML 1.0 Frameset This DTD is equal to XHTML 1.0 Transitional, but allows the use of frameset content. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd"> XHTML 1.0 Strict This DTD contains all HTML elements and attributes, but does NOT INCLUDE presentational or deprecated elements (like font). Framesets are not allowed. The markup must also be written as wellformed XML. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> XHTML 1.1

This DTD is equal to XHTML 1.0 Strict, but allows you to add modules (for example to provide ruby support for East-Asian languages).
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

Differences Between HTML 4.01 and HTML5


There are three different <!DOCTYPE> declarations in HTML 4.01. In HTML5 there is only one: <!DOCTYPE html>

HTML Tags
Tag <html> <body> <h1> to <h6> <hr> <!--> Description Defines an HTML document Defines the document's body Defines HTML headings Defines a horizontal line Defines a comment

<br>

Inserts a single line break

Often <strong> renders as <b>, and <em> renders as <i>. However, there is a difference in the meaning of these tags: <b> or <i> defines bold or italic text only. <strong> or <em> means that you want the text to be rendered in a way that the user understands as "important". Today, all major browsers render strong as bold and em as italics. However, if a browser one day wants to make a text highlighted with the strong feature, it might be cursive for example and not bold!

HTML Headings
HTML headings are defined with the <h1> to <h6> tags.

Example
<h1>This is a heading</h1> <h2>This is a heading</h2> <h3>This is a heading</h3> <h4> This is a heading </h4> <h5> This is a heading </h5> <h6> This is a heading </h6>

HTML Paragraphs
HTML paragraphs are defined with the <p> tag.

HTML Body
The <body> element defines the body of the HTML document.

HTML Links(hyperlinks)
HTML links are defined with the <a> tag. A hyperlink (or link) is a word, group of words, or image that you can click on to jump to another document. The most important attribute of the <a> element is the href attribute, which indicates the links destination. When you move the cursor over a link in a Web page, the arrow will turn into a little hand.

By default, links will appear as follows in all browsers:


An unvisited link is underlined and blue A visited link is underlined and purple An active link is underlined and red

The target attribute specifies where to open the linked document. The example below will open the linked document in a new browser window or a new tab:
<a href="http://www.w3schools.com/" target="_blank">Visit W3Schools!</a> The example below will open the linked document in the current window:

<a href="http://www.w3schools.com/" target="_self">Visit W3Schools!</a>


The example below will open the linked document in the frame that is superior to the frame the hyperlink is in:

<a href="http://www.w3schools.com/" target="_parent">Visit W3Schools!</a>


The example below will cancel all the frames and loads the page in the full browser window:

<a href="http://www.w3schools.com/" target="_top">Visit W3Schools!</a>

Example
<a href="http://www.w3schools.com">This is a link</a>

HTML Images
HTML images are defined with the <img> tag.

Example
<img src="w3schools.jpg" width="104" height="142">

HTML Text Formatting Tags


Tag <b> <em> <i> Description Defines bold text Defines emphasized text Defines a part of text in an alternate voice or mood

<small> <strong> <sub> <sup> <ins> <del>

Defines smaller text Defines important text Defines subscripted text Defines superscripted text Defines inserted text Defines deleted text

HTML "Computer Output" Tags


Tag <code> <kbd> <samp> <var> <pre> Description Defines computer code text Defines keyboard text Defines sample computer code Defines a variable Defines preformatted text

HTML Citations, Quotations, and Definition Tags


Tag <abbr> <address> <bdo> <blockquote> <q> <cite> <dfn> Description Defines an abbreviation or acronym Defines contact information for the author/owner of a document Defines the text direction Defines a section that is quoted from another source Defines an inline (short) quotation Defines the title of a work Defines a definition term

HTML Forms
HTML forms are used to pass data to a server. An HTML form can contain input elements like text fields, checkboxes, radio-buttons, submit buttons and more. A form can also contain select lists, textarea, fieldset, legend, and label elements. An HTML form is define by <form> </form> tag pair. An action attribute on the <form> tag gives the URL of the application that is to receive and process the forms data, i.e., where to send the inputs. A method attribute specifies how the browser will return the users inputs to the server. Example:

<form method="get" action="http://www.widgets.com/cgi-bin/update"> ... </form>

GET vs POST
method="get" Appends name/value pairs for inputs to the URL. Visible to the user. Normally used for small amounts of data. method="post" Embeds name/value pairs for inputs in the HTTP Request message. Not visible to the user. OK for larger amounts of data. Either method is OK for the web app on the server.

HTML Forms - The Input Element


The most important form element is the <input> element. The <input> element is used to select user information. An <input> element can vary in many ways, depending on the type attribute. An <input> element can be of type text field, checkbox, password, radio button, submit button, and more. The most common input types are described below. The <form> tag is used to create an HTML form.

Text Fields
<input type="text"> defines a one-line input field that a user can enter text into: <form> First name: <input type="text" name="firstname"><br> Last name: <input type="text" name="lastname"> </form>
Note: The form itself is not visible. Also note that the default width of a text field is 20 characters.

Password Field
<input type="password"> defines a password field:

<form> Password: <input type="password" name="pwd"> </form>


Note: The characters in a password field are masked (shown as asterisks or circles).

Radio Buttons
<input type="radio"> defines a radio button. Radio buttons let a user select ONLY 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> How the HTML code above looks in a browser: Male Female

Checkboxes
<input type="checkbox"> defines a checkbox. Checkboxes let a user select ZERO or MORE options of a limited number of choices. <form> <input type="checkbox" name="vehicle" value="Bike">I have a bike<br> <input type="checkbox" name="vehicle" value="Car">I have a car </form> How the HTML code above looks in a browser: I have a bike I have a car

Submit Button
<input type="submit"> defines a submit button. A submit button is used to send form data to a server. The data is sent to the page specified in the form's action attribute. The file defined in the action attribute usually does something with the received input:

<form name="input" action="html_form_action.asp" method="get"> Username: <input type="text" name="user"> <input type="submit" value="Submit"> </form> How the HTML code above looks in a browser: Username: 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_action.asp". The page will show you the received input.

HTML Form Tags


New : New tags in HTML5. Tag <form> <input> <textarea> <label> <fieldset> <legend> <select> <optgroup> <option> <button> <datalist>New <keygen>New <output>New Description Defines an HTML form for user input Defines an input control Defines a multiline input control (text area) Defines a label for an <input> element Groups related elements in a form Defines a caption for a <fieldset> element Defines a drop-down list Defines a group of related options in a drop-down list Defines an option in a drop-down list Defines a clickable button Specifies a list of pre-defined options for input controls Defines a key-pair generator field (for forms) Defines the result of a calculation

Example of a drop down list: <!DOCTYPE html> <html> <body> <form action=""> <select name="cars"> <option value="volvo">Volvo</option> <option value="saab">Saab</option> <option value="fiat">Fiat</option> <option value="audi">Audi</option> </select>

</form> </body> </html> Example of a radio button: <!DOCTYPE html> <html> <body> <form action=""> <input type="radio" name="gender" value="male">Male<br> <input type="radio" name="gender" value="female">Female </form> <p><b>Note:</b> When a user clicks on a radio-button, it becomes checked, and all other radiobuttons with equal name become unchecked.</p> </body> </html> <!DOCTYPE html> <html> <body>

Submit button: <form name="input" action="html_form_action.asp" method="get"> First name: <input type="text" name="FirstName" value="Mickey"><br> Last name: <input type="text" name="LastName" value="Mouse"><br> <input type="submit" value="Submit"> </form> <p>If you click the "Submit" button, the form-data will be sent to a page called "html_form_action.asp".</p> </body> </html> file:///C:/Documents%20and%20Settings/owner/My%20Documents/my %20html/html_form_action.htm? FirstName=Mickey&LastName=Mouse&pwd=hello&cars=volvo&gender=male if method=post

file:///C:/Documents%20and%20Settings/owner/My%20Documents/my %20html/html_form_action.htm

span and div signify no specific meaning besides the generic grouping of content,

and are therefore more appropriate for creating organization or stylistic additions without signifying superfluous meaning.
div

is a block element, span is inline.

There is one difference between div and span. In standard HTML, a div is a block-level element whereas a span is an inline element. The div block visually isolates a section of a document on the page, in the same way as a paragraph. The span element contains a piece of information inline with the surrounding text. In practice, even this feature can be changed by the use of Cascading Style Sheets (CSS). This means that to use them semantically, divs should be used to wrap sections of a document, while spans should be used to wrap small portions of text, images, etc.
span

and div elements are used purely to imply a logical grouping of enclosed elements.

There are three main reasons to use span and div tags with class or id attributes:
Styling with CSS See also: Cascading Style Sheets and Separation of presentation and content

Perhaps the most common use of span and div elements is to carry class or id attributes in conjunction with CSS to apply layout, typographic, color, and other presentation attributes to parts of the content. CSS does not just apply to visual styling: when spoken out loud by a voice browser, CSS styling can affect speech-rate, stress, richness and even position within a stereophonic image. For these reasons, and for compatibility with the concepts of the semantic web, discussed below, attributes attached to elements within any HTML should describe their semantic purpose, rather than merely their intended display properties in one particular medium. For example, the HTML in <span class="red small">password too short</span> denotes no more meaning than any other text, whereas <em class="warning">password too short</em> uses an em element to signify that the text requires emphasis, and uses a more appropriate class name. Perhaps when spoken they should be given extra stress, and a small reduction in speech-rate. The second example is semantically correct markup, rather than merely presentational.
Semantic clarity

This kind of grouping and labeling of parts of the page content might be introduced purely to make the page more semantically meaningful in general terms. It is impossible to say how and in what ways the World Wide Web will develop in years and decades to come. Web pages designed today may still be in use when information systems that we cannot yet imagine are trawling, processing, and classifying the web. Even today's search engines such as Google and others use proprietary information processing algorithms of considerable complexity.

For some years, the World Wide Web Consortium (W3C) has been running a major Semantic Web project designed to make the whole web increasingly useful and meaningful to today's and the future's information systems. The microformats movement is an attempt to build an idea of semantic classes. For example, microformats-aware software might automatically find an element like <span class="tel">123-456-7890</span> and allow for automatic dialing of the telephone number.
Access from code

Once the HTML or XHTML markup is delivered to a page-visitor's client browser, there is a chance that client-side code will need to navigate the internal structure (or Document Object Model) of the web page. The most common reason for this is that the page is delivered with client-side JavaScript that will produce on-going dynamic behavior after the page is rendered. For example, if rolling the mouse over a 'Buy now' link is meant to make the price, elsewhere on the page, become emphasized, JavaScript code can do this, but JavaScript needs to identify the price element, wherever it is in the markup, in order to affect it. The following markup would suffice: <div id="price">$45.99</div>. Another example is the Ajax programming technique, where, for example, clicking a hypertext link may cause JavaScript code to retrieve the text for a new price quotation to display in place of the current one within the page, without re-loading the whole page. When the new text arrives back from the server, the JavaScript must identify the exact region on the page to replace with the new information. Less common, but just as important examples of code gaining access to final web pages, and having to use span and div elements' class or id attributes to navigate within the page include the use of automatic testing tools. On dynamically generated HTML, this may include the use of automatic page testing tools such as HttpUnit, a member of the xUnit family, and load or stress testing tools such as Apache JMeter when applied to form-driven web sites.

Overuse
The judicious use of div and span is a vital part of HTML and XHTML markup. However, they are sometimes overused.
<div>This a large main division, with <span>a small bit</span> of spanned text!</div>

Note that it is illegal to place a block level element within an inline element, so:
<div>Some <span>text that <div>I want</div> to mark</span> up</div>

...is illegal.

<div id="header"> <div id="userbar"> Hi there, <span class="username">Chris Marasti-Georg</span> | <a href="/edit-profile.html">Profile</a> | <a href="http://www.bowlsk.com/_ah/logout?...">Sign out</a> </div> <h1><a href="/">Bowl<span class="sk">SK</span></a></h1> </div>

Hi there, Chris Marasti-Georg | Profile | Sign out

BowlSK

Styling the HTML Pages:


The <style> element and the style= attribute The style= attribute can be used on most tags defines features for a single HTML element, e.g. <p style=text-align: center>Center me.</p> The <style> element: <style type=text/css> ... </style> <style> tag always goes in the <head> section defines style information for the whole HTML page requires the type=text/css attribute more details to come in the description of CSS to link to a separate CSS style sheet, use instead: <link rel=stylesheet type=text/css href=string> Identifying and Grouping elements (e.g. for CSS) <div>...</div> division or section groups and identifies one or more block-elements usually causes a line break before and after <span>...</span> groups and identifies in-line elements (e.g. words) no visual change by itself (no line break) used to apply styles to parts of a text line, e.g. This <span style=color: red>red</span> apple.

Styles/Sections
<style type="text/css"> h1 {color:red;} p {color:blue;} </style> <div>A block-level section in a document</div> <span>An inline section in a document</span>

CSS(Cascading Style Sheets)


There are three ways of providing styling information for the Web browsers. 1.Linking style sheet 2.Internal style sheet 3.Inline style sheet Benefits: Authors and Web site managers may share style sheets across a number of documents (and sites). Authors may change the style sheet without requiring modifications to the document.

CSS Syntax:

This syntax has two parts, the selector and the declaration. Selector: Specifies the target of styling. Declaration: Specifies the property and value. Declaration is contained between {" ... "}. Declaration end with a semicolon. p{ color: red; }

Selectors are specify the target of styling. Selectors may range from simple element names to rich contextual representations. Kinds of selector: Type selector Class selector ID selector Grouping A type selector is the name of HTML Tag. Example: [index.html] <p>This is a paragraph</p> <p>This is a paragraph</p> <p>This is a paragraph</p> [style.css] p{ color: red; font-size: 12px; } Class selector is used for one or more elements. It is described the value of class attribute of HTML document with ".(period)" [index.html] <p class="red">This is a paragraph</p> <p class="blue">This is a paragraph</p> <p class="red">This is a paragraph</p> [style.css] p{ font-size: 12px; } .red{ color: red; } .blue{ color: blue; }

ID selector is used for unique element. It is described the value of ID attribute of HTML document with "#" [index.html] <p class="red">This is a paragraph</p> <p class="blue">This is a paragraph</p> <p class="red" id="small">This is a paragraph</p> [style.css] p{ font-size: 12px; } .red{ color: red; } .blue{ color: blue; } #small{ font-size: 9px; } Grouping: A comma-separated list of selectors represents the union of all elements selected by each of the individual selectors in the list [index.html] <h1>This is a heading</h1> <h2>This is a heading</h2> <h3>This is a heading</h3> <h4>This is a heading</h4> [style.css] h1, h2, h3, h4{ color: red; font-size: 12px; }

CSS: Selector examples


P { color: black; } /* Element Selector */ P, H1, H2 { color: black; } /* Grouping Selector */ * { color: black; } /* Universal Selector */ P.urgent, .Error { color: black; } /* Class Selector */ #Menu { color: black; } /* ID Selector */ *[title], A[href][title] { color: black; } /* Attribute Selector */ A[title="home page"] { color: black; } /* Exact Attribute Selector */ A[title~="foo"] { color: black; } /* Partial Attribute Selector */ *[lang|="en"] { color: black; } /* Particular Attribute Selector */ UL LI UL { color: black; } /* Descendant Selector */ P > STRONG { color: black; } /* Child Selector */ H1 + P { color: black; } /* Adjacent Sibling Selector */ A:hover { color: black; } /* Pseudo-Class Selector */ P:first-letter { font-size: 200%; } /* Pseudo-Element Selector */

CSS: Common declaration properties


background background-attachment background-color background-repeat border bottom color cursor display float font font-family font-size font-style font-weight height left letter-spacing line-height list-style-image list-style-position list-style-type margin overflow padding position right text-align text-decoration text-indent text-transform top vertical-align visibility white-space width word-spacing word-wrap z-index You can separate style sheets from HTML documents. Style sheet files are imported to HTML documents by <link>. [example.html] <head>

<link rel="stylesheet" type="text/css" href="example.css"> </head> [example.css] p{ color: red; foto-size: 120%; }

You can put style sheet rules in the head of the document by <style>. [example.html] <head> <style> p { color: red; font-size:120%; } </style> </head> <body> <p>This is a paragraph</p> </body>

The start tags can contain style sheet rules directly in HTML documents by the style attribute. [example.html] <p style="color: red; font-size:120%; "> This is a paragraph</p>

The HTML <script> Tag


The <script> tag is used to define a client-side script, such as a JavaScript. The <script> element either contains scripting statements or it points to an external script file through the src attribute. The required type attribute specifies the MIME type of the script. Common uses for JavaScript are image manipulation, form validation, and dynamic changes of content. The script below writes Hello World! to the HTML output:

Example
<script> document.write("Hello World!") </script>

The HTML <noscript> Tag


The <noscript> tag is used to provide an alternate content for users that have disabled scripts in their browser or have a browser that doesnt support client-side scripting. The <noscript> element can contain all the elements that you can find inside the <body> element of a normal HTML page. The content inside the <noscript> element will only be displayed if scripts are not supported, or are disabled in the users browser:

Example
<script> document.write("Hello World!") </script> <noscript>Sorry, your browser does not support JavaScript!</noscript>

JavaScript can write directly into the HTML output stream:


document.write("<p>This is a paragraph</p>");

JavaScript can react to events:


<button type="button" onclick="myFunction()">Click Me!</button>

JavaScript can manipulate HTML styles:


document.getElementById("demo").style.color="#ff0000";

Sound in HTML
We can add sound to an HTML page very much like we add images. Add to image_example.html: <embed src="hello.wav" autostart="true" loop="false"> </embed>

HTML Entities
Some characters are reserved in HTML. It is not possible to use the less than (<) or greater than (>) signs in your text, because the browser will mix them with tags. To actually display reserved characters, we must use character entities in the HTML source code. A character entity looks like this:

HTML Useful Character Entities


Note: Entity names are case sensitive! Result Description Entity Name Entity Number non-breaking space &nbsp; &#160; < less than &lt; &#60; > greater than &gt; &#62; & ampersand &amp; &#38; cent &cent; &#162; pound &pound; &#163; yen &yen; &#165; euro &euro; &#8364; section &sect; &#167; copyright &copy; &#169; registered trademark &reg; &#174; trademark &trade; &#8482;

What Is XHTML?

XHTML stands for EXtensible HyperText Markup Language XHTML is almost identical to HTML 4.01 XHTML is a stricter and cleaner version of HTML 4.01 XHTML is HTML defined as an XML application XHTML is supported by all major browsers.

Why XHTML?
Many pages on the internet contain "bad" HTML. The following HTML code will work fine if you view it in a browser (even if it does NOT follow the HTML rules):
<html> <head> <title>This is bad HTML</title> <body> <h1>Bad HTML <p>This is a paragraph </body>

XML is a markup language where documents must be marked up correctly and "well-formed". If you want to study XML, please read our XML tutorial. Today's market consists of different browser technologies. Some browsers run on computers, and some browsers run on mobile phones or other small devices. Smaller devices often lack the resources or power to interpret a "bad" markup language. Therefore - by combining the strengths of HTML and XML, XHTML was developed. XHTML is HTML redesigned as XML.

The Most Important Differences from HTML:


Document Structure XHTML DOCTYPE is mandatory The XML namespace attribute in <html> is mandatory

<html>, <head>, <title>, and <body> is mandatory

XHTML Elements XHTML elements must be properly nested XHTML elements must always be closed

XHTML elements must be in lowercase XHTML documents must have one root element

XHTML Attributes Attribute names must be in lower case Attribute values must be quoted

Attribute minimization is forbidden

<!DOCTYPE ....> Is Mandatory


An XHTML document must have an XHTML DOCTYPE declaration. A complete list of all the XHTML Doctypes is found in our HTML Tags Reference. The <html>, <head>, <title>, and <body> elements must also be present, and the xmlns attribute in <html>, must specify the xml namespace for the document. The example below shows an XHTML document with a minimum of required tags:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Title of document</title> </head> <body> ...... </body> </html>

XHTML Elements Must Be Properly Nested


In HTML, some elements can be improperly nested within each other, like this:
<b><i>This text is bold and italic</b></i>

In XHTML, all elements must be properly nested within each other, like this:
<b><i>This text is bold and italic</i></b>

XHTML Elements Must Always Be Closed


This is wrong:
<p>This is a paragraph <p>This is another paragraph

This is correct:
<p>This is a paragraph</p> <p>This is another paragraph</p>

Empty Elements Must Also Be Closed


This is wrong:
A break: <br> A horizontal rule: <hr> An image: <img src="happy.gif" alt="Happy face">

This is correct:
A break: <br /> A horizontal rule: <hr /> An image: <img src="happy.gif" alt="Happy face" />

XHTML Elements Must Be In Lower Case


This is wrong:
<BODY> <P>This is a paragraph</P> </BODY>

This is correct:
<body> <p>This is a paragraph</p> </body>

Attribute Names Must Be In Lower Case


This is wrong:
<table WIDTH="100%">

This is correct:
<table width="100%">

Attribute Values Must Be Quoted


This is wrong:
<table width=100%>

This is correct:
<table width="100%">

Attribute Minimization Is Forbidden

This is wrong:
<input checked> <input readonly> <input disabled> <option selected>

This is correct:
<input checked="checked"> <input readonly="readonly"> <input disabled="disabled"> <option selected="selected">

How to Convert from HTML to XHTML


1. Add an XHTML <!DOCTYPE> to the first line of every page 2. Add an xmlns attribute to the html element of every page 3. Change all element names to lowercase 4. Close all empty elements 5. Change all attribute names to lowercase 6. Quote all attribute values

********************************

S-ar putea să vă placă și