Hypertext Markup Language Martin Kruli Martin Neask by
Hypertext Markup Language Martin Kruliš, Martin Nečaský by Martin Kruliš (v 1. 1) 29. 10. 2015 1
What is HTML? � HTML = Hypertext Markup Language ◦ World Wide Web's markup language ◦ Language for describing the structure of Web pages � Web Page ◦ Semi-structured document �Form of structured data with non-formal structure model �Basically a plain text interleaved by markers (tags) that impose some form of internal structure ◦ Structure denoted in a form of HTML markup by Martin Kruliš (v 1. 1) 29. 10. 2015 2
What is HTML? <!DOCTYPE html> <html lang="en"> <head> <title>Simple web page</title> </head> <body> <h 1>Simple web page</h 1> <p>This is a <em>web page</em> with <a href="http: //whatwg. org/html">HTML 5</a> markup. </p> </body> </html> by Martin Kruliš (v 1. 1) 29. 10. 2015 3
HTML Syntax � HTML Document ◦ Is structured as a tree �HTML syntax is in fact an infix serialization of the tree �Browsers represent the tree in Document Object Model (DOM), which can be manipulated with Java. Script ◦ Various types of nodes �Elements, text, attributes, comments, … by Martin Kruliš (v 1. 1) 29. 10. 2015 4
HTML Syntax � DOM Document Tree Example <html> <body> <h 1>DOM Example</h 1> <p> Document Object Model is an API … h 1 </p> <img src="url" alt="text">. . . </body> DOM Example </html> html body … p img src alt Document Object Model … by Martin Kruliš (v 1. 1) 29. 10. 2015 5
HTML Syntax � HTML Element ◦ Represents a fragment of a web page �Gives semantic meaning to its content ◦ Opening and closing tags work as parentheses for the content (i. e. , what falls into the element) �Content-less elements may omit closing tag �Elements may not cross-overlap ◦ Attributes �Name-value pairs specified in opening tag �Values are optional and they are optionally enclosed in single or double quotes (recommended) �Quotes are mandatory if the value contains ", ', <, or > by Martin Kruliš (v 1. 1) 29. 10. 2015 6
HTML Syntax � Comments ◦ Enclosed in <!-- and --> ◦ Not displayed when page is rendered � Entities ◦ ◦ ◦ &entity-name; Provide a way to encode special characters <, >, &, " - < , > , & , " - non-braking space Numerically represented characters ü - “ü” by Martin Kruliš (v 1. 1) 29. 10. 2015 7
Evolution of HTML www. whatwg. org 1995 HTML 2. 0 1997 HTML 3. 2 XML 1. 0 2001 2000 1999 HTML 4. 01 2014 HTML 5 www. w 3. org XHTML 1. 0 XHTML 1. 1 XHTML 2 by Martin Kruliš (v 1. 1) 29. 10. 2015 2010 8
Evolution of HTML � What. WG ◦ http: //whatwg. org/html ◦ "Living Standard" (see Last Updated) � W 3 C ◦ http: //www. w 3. org/TR/html 5/ ◦ Formal standardization process �Stages of W 3 C a document (draft, CR, R) by Martin Kruliš (v 1. 1) 29. 10. 2015 9
Web of Documents � Current World Wide Web is sometimes referred to as Web of Documents ◦ HTML as a format for representing documents published on the Web ◦ URLs as unique global identifiers of documents ◦ HTTP for localization and accessing documents by their URLs ◦ Hyperlinks between documents to link related documents on the Web by Martin Kruliš (v 1. 1) 29. 10. 2015 10
Web of Documents http: //. . . Web Page http: //example. com/page. html http: //. . . Web Page http: //. . . Web Page by Martin Kruliš (v 1. 1) 29. 10. 2015 11
Hyperlinks � Hyperlink Elements ◦ Links to external resource, which are exposed to user of the current document as means to browse <a href="http: //www. google. com">Google</a> �Attribute href specifies URL of linked resource �Content is visible to user (text or any inline elements) ◦ Identifying fragment of a web page �<p id="Para 2">. . . </p> �Is then referenced by fragment part of an URL �<a href="#Para 2">. . . </a> �<a href="http: //www. page. com#Para 2">. . . </a> by Martin Kruliš (v 1. 1) 29. 10. 2015 12
HTML Body � Markup in HTML(5) Body ◦ Text level semantics elements ◦ ◦ ◦ �Denote parts of the text in a HTML document with a specific semantics Sectioning content Grouping content Tables, forms External sources (images) Hyperlinks … by Martin Kruliš (v 1. 1) 29. 10. 2015 13
Text Level Semantics Element Description em Represents stress emphasis of its content. The level of stress is given by the level of nesting of particular em elements strong Represents strong importance of its content. small Represents a side comment. s Represents no longer relevant or accurate content. cite Represents a title of a work (book, article, game, software, song, opera, . . . ). abbr Represents an abbreviation or acronym, optionally with its expansion in title attribute. data Represents its content enriched with its machine readable notation in value attribute. time Represents its content which is a determination of time with machine readable notation in datetime attribute. i Represents its content in a manner indicating different quality of text b Represents its content to which attention is being drawn br Represents a line break by Martin Kruliš (v 1. 1) 29. 10. 2015 14
Sectioning Content � Defining ◦ ◦ Internal Structure of Document Body Content of document is divided into sections Sections are divided to subsections section element – a generic section article element - self-contained main section �Independently distributable and reusable �E. g. , a blog post or a newspaper article ◦ aside element – separate section �Marginal content �E. g. , a did-you-know aside box ◦ nav element – section containing navigation links by Martin Kruliš (v 1. 1) 29. 10. 2015 15
Sectioning Content <article> <p>This article summarizes technologies. . . </p> <nav> <a href="#html">HTML</a><a href="#css">CSS</a> </nav> <section id="html"> <p>We will start with HTML. </p> <section><p>First, we will go to history. </p></section> <section><p>Then, we will deal with actual 5. 0. </p></section> <aside> <p>Did you know that SGML is a predecessor of HTML? </p> </aside> <section id="css"><p>CSS is the second technology. </p></section> <nav> <div><a href=". . . ">Home</a><a href=". . . ">Contact</a></div> </nav> </article> <article>Another article</article> by Martin Kruliš (v 1. 1) 29. 10. 2015 16
Headers and Footers � Headers and Footers ◦ Within a section or for the whole document ◦ header element �Distinguishes header of the nearest section (hierarchically) �Intended (but not required) to contain heading elements (h 1 – h 6) ◦ footer element �Distinguishes footer of the nearest section (hierarchically) �Usually contains copyrights, author info, etc. by Martin Kruliš (v 1. 1) 29. 10. 2015 17
Sectioning Content <article> <header> <h 1>NSWI 117 – Summary of technologies</h 1> </header> <section> <header> <h 1>HTML</h 1> </header> <p>We will start with HTML. </p> </section> <footer> <nav> <div><a href=". . . ">Home</a> <a href=". . . ">Contact</a></div> </nav> </footer> </article> by Martin Kruliš (v 1. 1) 29. 10. 2015 18
Headings � Heading Elements h 1 – h 6 ◦ Before HTML 5 �Six levels of importance (rank) �<h 1> most important (highest), <h 6> least important ◦ Headings in HTML 5 �Combined with sectioning <section>, <article>, … �Attempt to keep some backwards compatibility �Quite difficult to realize though �Each section has its own heading hierarchy �First heading element in a section is the main heading of that section (no matter its rank) by Martin Kruliš (v 1. 1) 29. 10. 2015 19
Section Headings in HTML 5 � Outline Algorithm ◦ First heading (any of <h 1>…<h 6>) in the section is the heading of the section ◦ Subsequent headings create new implicit sections �Headings of the same or higher rank implicitly close current section and start another one �Headings of lesser ranks open implicit subsections �Implicit subsections are closed when ancestor explicit section is closed by Martin Kruliš (v 1. 1) 29. 10. 2015 20
Section Headings Example <body> <h 1>NSWI 142 -. . . </h 1> <p>At this page, . . . </p> <h 2>HTML</h 2> <p>About HTML. . . </p> Sections Structure <h 3>HTML History</h 3> • NSWI 142 - … <h 3>HTML Today</h 3> • HTML <h 2>CSS</h 2> • HTML History • HTML Today <p>About CSS. . . </p> • CSS </body> by Martin Kruliš (v 1. 1) 29. 10. 2015 21
Section Headings Example <body> <h 1>NSWI 142 -. . . </h 1> <p>At this page, . . . </p> <section> <h 2>HTML</h 2> <p>About HTML. . . </p> <section> <h 3>HTML History</h 3> </section> <h 3>HTML Today</h 3> </section> <section> <h 2>CSS</h 2> <p>About CSS. . . </p> </section> </body> Sections Structure • NSWI 142 - … • HTML History • HTML Today • CSS by Martin Kruliš (v 1. 1) 29. 10. 2015 22
Section Headings Example <body> <h 1>NSWI 142 -. . . </h 1> <p>At this page, . . . </p> <section> <h 1>HTML</h 1> <p>About HTML. . . </p> <section> <h 1>HTML History</h 1> </section> <h 6>HTML Today</h 6> </section> <section> <h 1>CSS</h 1> <p>About CSS. . . </p> </section> </body> Sections Structure • NSWI 142 - … • HTML History • HTML Today • CSS by Martin Kruliš (v 1. 1) 29. 10. 2015 23
Section Headings in HTML 5 � Outline Algorithm ◦ Is not currently implemented in graphical browsers! � How to Fix The Problem ◦ Use single heading in each section �Use <h 1> everywhere �Express importance by <section> nesting �Or use appropriate heading based on nesting level �E. g. , <h 3> in 2 nd level (<section>…) ◦ Additional help from CSS may be required by Martin Kruliš (v 1. 1) 29. 10. 2015 24
Grouping Content Element Description p Represents a paragraph pre Represents a block of preformatted text div Element with no special meaning (generic container) main Represents a block with a dominant content ul Represents an unordered list ol Represents an ordered list li Represents a list item This is not a complete list! by Martin Kruliš (v 1. 1) 29. 10. 2015 25
List Example <ul> <li>First item</li> <li>Second item: <ol> <li>HI</li> <li>HELLO</li> <li>GOOD MORNING</li> </ol> </li> <li>Third item</li> </ul> • First item • Second item: 1. HI 2. HELLO 3. GOOD MORNING • Third item by Martin Kruliš (v 1. 1) 29. 10. 2015 26
Tables Element Description table thead table head tbody table body tfoot table footer tr table row th table head cell td table data cell The <thead>, <tbody> and <tfoot> table division may be omitted for simple tables by Martin Kruliš (v 1. 1) 29. 10. 2015 27
Table Example <table> <thead> <tr> <th>Name</th><th>Email</th><th>Address</th> </tr> </thead> <tbody> <tr> <td>Joe White</td> <td>joe@white. abc</td> <td>Lloyd Ave, Boston</td> </tr> <tr> Name Email <td>Bill Black</td> <td>bill@black. def</td> Joe White joe@white. abc <td>---</td> </tr> </tbody> Bill Black bill@black. def </table> by Martin Kruliš (v 1. 1) Address Lloyd Ave, Boston --- 29. 10. 2015 28
Irregular Tables � Spanning Table Cells ◦ Virtually grouping content of multiple logical cells ◦ Attribute colspan of element td and th �Spans selected cell over several following columns �Specifies the number of columns taken by the cell ◦ Attribute rowspan of element td and th �Spans selected cell over several following rows �Specifies the number of rows taken by the cell by Martin Kruliš (v 1. 1) 29. 10. 2015 29
Irregular Tables <table> <tr> <td colspan="2">Adults</td> <td>2</td> </tr> <tr><td>Adult 1</td><td>34</td><td rowspan="2">2</td></tr> <tr><td>Adult 2</td><td>32</td></tr> <td colspan="2">Children</td> <td>3</td> </tr> Adults <tr><td>Child 1</td><td>4</td><td>1</td></tr> Adult 1 <tr><td>Child 2</td><td>8</td><td rowspan="2">2</td></tr> <tr><td>Child 3</td><td>12</td></tr> Adult 2 </table> Children 2 34 32 3 Child 1 4 Child 2 8 Child 3 12 by Martin Kruliš (v 1. 1) 2 29. 10. 2015 1 2 30
Forms � HTML Form ◦ Component of web page composed of UI controls ◦ User can interact with form controls �Adding text, selecting options, clicking on buttons, … �Data provided by user can be processed by a script �By Javascript in the browser or by a server script � Element <form> ◦ Attribute method – HTTP method used for transfer �post – data are transferred in HTTP request body �get – data are encoded in URL (query part) ◦ Attribute action – URL where the data are sent to by Martin Kruliš (v 1. 1) 29. 10. 2015 31
Forms � Form Controls ◦ input �Basic input controls �Various types based on input attributes ◦ textarea �Input for longer (multi row) texts ◦ select �Selection list with options ◦ button �Submit or reset button by Martin Kruliš (v 1. 1) 29. 10. 2015 32
Forms Example <form method="get" action="http: //www. example. org/newcustomer. php"> Name: <input name="name". . . > Phone: <input name="phone". . . > Preferred delivery time: <input name="time". . . > Comments: <textarea name="comments"></textarea> <button type="submit">Submit Order</button> </form> Submit button works as a link to an assembled URL: http: //www. example. org/newcustomer. php ? name=John+Smith&phone=555 -1234& time=5 pm&comments=unleashed+dog+at+house by Martin Kruliš (v 1. 1) 29. 10. 2015 33
Forms � Element <input> - basic form input field ◦ Attribute name specifies identifier of the field �For script which processes the data on the server ◦ Attribute type specifies type of the input �text – input is one line of text �+ attribute maxlength – maximal text length �password – same as text, but obscures input with * �radio – exclusive choice (radio buttons) from set of fields with the same name �+ attribute value – value send to server (when selected) �+ attribute checked="checked" – default choice �checkbox – multiple choice, similar logic as radio by Martin Kruliš (v 1. 1) 29. 10. 2015 34
Element input Example <form method="post" action="http: //www. example. org/script. php"> <p> Name: <input type="text" name="full. Name" maxlength="5"> Password: <input type="password" name="password"> </p> <p>Age: 0 -18: <input type="radio" name="age" value="0"> 19 -65: <input type="radio" name="age" value="19"> 66 -*: <input type="radio" name="age" value="65"> </p> <p>Product: <input type="checkbox" name="product 1" value="BMW">BMW <input type="checkbox" name="product 2" value="AUD">Audi <input type="checkbox" name="product 3" value="POR">Porsche </p> </form> by Martin Kruliš (v 1. 1) 29. 10. 2015 35
Forms � Element <input> ◦ Attribute type specifies type of the input �submit – submission button �+ attribute value contains displayed button value �reset – reset button �+ attribute value contains displayed button value �hidden – submitted value hidden to user �Fixed value, user cannot change it �file – file submission �Browser allows selecting file on local disk, which is then uploaded to the server by Martin Kruliš (v 1. 1) 29. 10. 2015 36
Element input Example <form method="post" action="http: //www. example. org/script. php"> <p> Name: <input type="text" name="full. Name" maxlength="5"> Password: <input type="password" name="password"> </p> <input type="hidden" name="request. Code" value="H 38 a. JJJ"> <input type="submit" value="Submit Form"> <input type="reset" value="Reset Form"> </form> by Martin Kruliš (v 1. 1) 29. 10. 2015 37
Forms � Element <input> ◦ The control element might be further specified ◦ maxlength attribute �Maximum number of characters allowed in a text field ◦ size attribute �Number of characters visible in a text field ◦ value attribute �Specifies default/selected field value ◦ disabled="disabled" attribute �Specifies that field is disabled when form loads by Martin Kruliš (v 1. 1) 29. 10. 2015 38
Element input Example <form. . . > <input name="full_name" maxlength="32" size="16" value="Martin Nečaský" disabled title="First name followed by family name. "> <button type="submit">Submit</button> </form> by Martin Kruliš (v 1. 1) 29. 10. 2015 39
Forms � Element <select> ◦ List of options user selects from ◦ Sub-element <option> �Content is shown to the user �Attribute value – submitted value �Attribute selected="selected" – default value ◦ Attribute multiple="multiple" �Allows user to select more values ◦ Attribute size �Number of options (rows) displayed at once �If size=1, the list is shown as drop-down-list box by Martin Kruliš (v 1. 1) 29. 10. 2015 40
Element select Example <form. . . > <p>Product: <select name="product" size="4" multiple="multiple"> <option value="BMW">BMW</option> <option value="AUD" selected="selected">Audi</option> <option value="MER">Mercedes</option> <option value="SKO">Skoda</option> <option value="CHE">Chevrolet</option> <option value="TOY">Toyota</option> <option value="FOR">Ford</option> </select> </p> </form> by Martin Kruliš (v 1. 1) 29. 10. 2015 41
Forms � Additional Form Elements ◦ Making the form structured and better readable ◦ <fieldset> element �Groups logically related fields ◦ <legend> element �Caption for a <fieldset> element ◦ <label> element �Caption for a control element (e. g. , <input> element) ◦ title attribute �Balloon tip hint for a field �(This is attribute may be used with any HTML element) by Martin Kruliš (v 1. 1) 29. 10. 2015 42
Form Elements Example <form. . . > <p><label>Name: <input. . . ></label></p> <fieldset> <legend>Contact information</legend> <p><label>Phone: <input. . . ></label></p> <p><label>Email: <input. . . ></label></p> </fieldset> <legend>Timing</legend> <p> <label for="time">Preferred delivery time: </label> <input id="time" title="Time in hh: mm format. ". . . > </p> </fieldset> <p><label>Comments: <textarea></label></p> <button>Submit Order</button> </form> by Martin Kruliš (v 1. 1) 29. 10. 2015 43
Forms in HTML 5 � Element ◦ ◦ ◦ ◦ ◦ <input> New HTML 5 Types search - search query field tel - phone number field url - absolute URL (e. g. , page address) field email - email address field date, time, datetime - date/time field number – number (entered as text) field range - number (entered by slide bar) field color – color selection field Unsupported types are treated as text inputs by Martin Kruliš (v 1. 1) 29. 10. 2015 44
Forms in HTML 5 � HTML 5 <input> Attributes ◦ autocomplete attribute �Turns automatic completion of a field on/off ◦ autofocus attribute �Turns the focus to the field when the page loads ◦ pattern attribute �Regular expression for field value validation ◦ placeholder attribute �Hint for the user to help with filling the field ◦ required attribute �Field value is mandatory form submission by Martin Kruliš (v 1. 1) 29. 10. 2015 45
Element select Example <form. . . > <input name="phone" autocomplete="off" autofocus="autofocus" pattern="[0 -9]{9}" placeholder="Fill in your 9 -digit phone number" required="required"> <button>Submit</button> </form> by Martin Kruliš (v 1. 1) 29. 10. 2015 46
Submitting Forms � When Form Is Submitted … ◦ Data are encoded into HTTP request made for selected URL using given method (GET/POST) �In case of GET request, the data are encoded to URL ◦ enctype attribute of element <form> specifies the format of the encoded data (for POST requests) �application/x-www-form-urlencoded �multipart/form-data �text/plain by Martin Kruliš (v 1. 1) 29. 10. 2015 47
Submitting Forms Example <form method="post" action="? " enctype=". . . "> Full Name: <input name="fullname" type="text"> Gender: <input name="gender" type="radio" value="M"> male, <input name="gender" type="radio" value="F"> female <input name="student" type="checkbox" value="1"> student <input type="hidden" name="tag" value="#a& Hx 9%"> <button type="submit">Submit</button> </form> by Martin Kruliš (v 1. 1) 29. 10. 2015 48
Submitting Forms Example � application/x-www-form-urlencoded fullname=Martin+Kruli%C 5%A 1&gender=M&studen t=1&tag=%23 a%26 Hx 9%25 � text/plain fullname=Martin Kruliš gender=M student=1 tag=#a&Hx 9% by Martin Kruliš (v 1. 1) 29. 10. 2015 49
Submitting Forms Example � multipart/form-data --------------- 88242493723271 Content-Disposition: form-data; name="fullname" Martin Kruliš ---------------88242493723271 Content-Disposition: form-data; name="gender" M ---------------88242493723271 Content-Disposition: form-data; name="student" 1 ---------------88242493723271 Content-Disposition: form-data; name="tag" #a&Hx 9% ---------------88242493723271 -- by Martin Kruliš (v 1. 1) 29. 10. 2015 50
Images � Element <img> ◦ Include image in the document contents ◦ Attribute src – the URL of an image to be loaded ◦ Attributes width and height - image size in pixels �It is a good practice to let the browser know the size before the image is loaded (or if it fails) ◦ Attribute alt – alternative textual representation �Briefly describes, what the image shows �Used, when the image loading fails ◦ Attribute title – same as in form controls by Martin Kruliš (v 1. 1) 29. 10. 2015 51
Document Metadata � Additional Information about The Document ◦ Inside element <head> ◦ <title> - document title or name �Identify document for users (browser window caption) ◦ <base> - URL base for resolving relative addresses ◦ <link> - links document to other resources �Cascading stylesheet files, for instance ◦ <style> - embeds style information inside document �Contents is written in CSS stylesheet syntax ◦ <meta> - additional metadata and HTTP supplements �Description, keywords, document author, … by Martin Kruliš (v 1. 1) 29. 10. 2015 52
Document Metadata <html> <head> <title>Technologies for …</title> <base href="http: //www. ksi. mff. cuni. cz/index. html"> <link rel="stylesheet" href="default. css"> <style type="text/css"> body { font-size: 12 pt; } </style> <meta name="keywords" content="html, example"> <meta http-equiv="content-type" content="text/html; charset=utf-8"> </head> </html> In HTML 5, this particular construction can be shorten to <meta charset="utf-8"> by Martin Kruliš (v 1. 1) 29. 10. 2015 53
More on Links � HTML 5 Definition of Link ◦ Link represents relationship of particular type between current document and other web resource ◦ Syntactically defined by elements <link> and <a> ◦ Two kinds of links (according to HTML 5 spec. ) �Links to external resources �Augment/further specify current document �Hyperlinks �Exposed to the user to navigate between resources �The kind depends on the element used and on the relationship type (attribute rel) by Martin Kruliš (v 1. 1) 29. 10. 2015 54
More on Links � Link Specification ◦ href attribute �URL of resource linked by relationship ◦ rel attribute �Type of relationship ◦ media attribute �Media linked resource applies to �E. g. , print, screen, all ◦ type attribute �MIME type of linked resource �text/html, application/xhtml+xml, text/css, application/pdf by Martin Kruliš (v 1. 1) 29. 10. 2015 55
More on Links Link type link a Description alternate H H Alternate representation of current document author H H Author of document icon ER - Icon representing current document stylesheet ER - Stylesheet for current document licence H H Copyright license covering current document first H H First document of a series current document is part of last H H Last document of a series current document is part of next H H Next document in a series current document is part of by Martin Kruliš (v 1. 1) 29. 10. 2015 56
More on Links <html> <head> <title>NSWI 142 – Materials</title> <link rel="stylesheet" href="default. css" type="text/css" media="screen"> <link rel="stylesheet" href="default-print. css" type="text/css" media="print"> <link rel="alternate" href="materials. pdf" type="application/pdf" media="print"> </head> <body> <footer>Author: <a href="http: //www. ksi. mff. cuni. cz/~krulis" rel="author"> Martin Kruliš</a> </footer> </body> </html> by Martin Kruliš (v 1. 1) 29. 10. 2015 57
HTML Markup – 4. 01 vs. 5 � Differences ◦ No sense to go through particular differences in this lecture ◦ See http: //www. w 3. org/TR/html 5 -diff by Martin Kruliš (v 1. 1) 29. 10. 2015 58
Discussion by Martin Kruliš (v 1. 1) 29. 10. 2015 59
- Slides: 59