HTML

The Ultimate HTML Cheat Sheet For Beginners

Posted on Updated on

Below is a complete guide of HTML codes that you can copy and paste for use on your own blog or website. Although I like to insist on bloggers taking the time to learn how to write these codes and know what each part of them does, sometimes you need a code in a pinch!

 

Text Formatting

Headings

Defines an important heading in your text. You can use <h1> to <h6>, with the highest number resulting in the smallest font size.

<h1>Your important heading</h1>

Aligned Heading

Aligns your heading using a little bit of CSS. You can use “left”, “right”, or “justify” in place of “center” below:

<h1 style="text-align:center;">Your aligned heading</h1>

Paragraphs

Inserts a paragraph break. Defines each paragraph.

<p>Your paragraph here</p>

Aligned Paragraph

Aligns your paragraph using a little bit of CSS. You can use “left”, “right”, or “justify” in place of “center” below:

<p style="text-align:center">Your paragraph text is aligned</p>

Line Breaks

Line breaks are used instead of paragraphs, when you want to create a new line without starting a new paragraph.

The end of your sentence.<br />

Bold Text

Makes the weight of your font bold

<b>Your bold text</b>

Strong Text

Same look as bold text, but is semantic. Instead of it being simply a style, strong text shows how the text should be read or understood.

<strong>Your strong text</strong>

Italic Text

Makes your text italicized

<i>Your italic text</i>

Emphasized Text

Same look as Italic text in HTML, but is semantic. Specifies that the text should be emphasized when read.

<em>Your emphasized text</em>

Underlined Text

Underlines your text

<u>Your underlined text</u>

Strike-through

Places a line through your text to strike it out.

<s>Your text here</s>

Font Family

Changes the font of the text using a little CSS. You can change the font to any web safe font or Google web font:

<span style="font-family: Arial, Helvetica, sans-serif;">Your new font</span>

Font Size

Changes the size of the font using a little CSS. You can use px, em, or a percentage. Here is an example with px:

<span style="font-size:16px;">Your font in a new size</span>

Font Color

Changes the color of your font to any hex color value of your choice:

<span style="font-color:#030303;">Your new font color</span>

Highlighted Text

Highlights the text with a background color using a little CSS:

<span style="background-color:#C2F2CA">Your highlighted text</span>

Block Quotes

Useful when quoting someone or when you need a particular part of your text to stand out.

<blockquote>Your quoted text here</blockquote>

Links

Basic Text Link

Use to add a link to specific text or a word. Replace the http://www.yourlink.com with your own link:

<a href="http://www.yourlink.com">Your linked text</a>

Open Link in New Tab

Used to open the link in a new window or tab instead of in the same web page:

<a href="http://www.yourlink.com" target="_blank">Your linked text</a>

Link To An Email Address

Opens the user’s email program to quickly send an email to the address supplied. Replace the email address with your own:

<a href="mailto:you@youremailaddress.com">Your email address or link</a>

 Link To An Email Address With Subject Line

Useful if you want the email to have a specific subject when a user clicks your link. Use %20 in place of any spaces and replace the text with your own subject line:

<a href="mailto:you@youremailaddress.com?subject=Your%20Email%20Subject">Your email address or link</a>

Anchored (“Jump”) Link

For jumping to a particular part of a page with the click of a link. This happens in two parts. First, include the code below wherever you want the user to end up when they click the link, for example, at the top of the post. Name it something unique:

<a name="backtotop"></a>

Then, add the anchor to your link that the user will click on to jump to that section:

<a href="#backtotop">Back to top</a>

Images

Basic Image

Include an image in your post. Replace the image URL with your own URL. You will need to have this image uploaded somewhere online. Describing your image helps with SEO:

<img src="http://www.yoursite.com/yourimage.jpg" alt="describe this image"/>

Image Link

For adding a link to a certain image. Replace the image URL and link with your own:

<a href="http://www.yourlink.com"><img src="http://www.yoursite.com/yourimage.jpg" alt="describe this image"/></a>

Image Link Opens In New Window

<a href="http://www.yourlink.com" target="_blank"><img src="http://www.yoursite.com/yourimage.jpg" alt="describe this image"/></a>

Image Width and Height

You can change the width and height of the image if you need to, however it’s usually best to resize your image prior to adding it to your site. You can specify the width and height below for browser compatibility. Change the width and height values to those of your actual image:

<img src="http://www.yoursite.com/yourimage.jpg" alt="describe this image" width="450" height="600"/>

Align Image to Left or Right of Paragraph

If you want to place your image to the left or right of a paragraph, use the following code, replacing “left” with “right” if you like:

<img src="http://www.yoursite.com/yourimage.jpg" alt="describe this image" align="left"/>

Backgrounds

The following should be completed in your site’s main CSS file, or a dedicated CSS section of your website/blog editor. If you don’t have a CSS section or file, you can place these codes in between <style> and </style> tags in the <head> section of your website’s HTML, although it is recommended to have an external CSS file.

Page Background Color

Change the overall background of your website or blog with this code. Replace the color hex code with your own.

body {
background-color:#c3c3c3;
}

Repeating Background Image

For smaller backgrounds that you want tiled, or larger backgrounds that were made to be repeating, use this code and replace the image URL with your own. You will need to upload the image online first:

body {
background-image:url(http://www.yourwebsite.com/background-image.jpg); 
background-repeat:repeat;
}

Change the above red “repeat” if you want the image to only repeat vertically: repeat-y
Change the above red “repeat” if you want the image to only repeat horizontally: repeat-x

Non-Repeating Background Image

For background images that you want displayed only once (not repeated or tiled). Replace the image URL with your own.

body {
background-image:url(http://www.yourwebsite.com/background-image.jpg);
background-repeat:no-repeat;
}

Top Centered Non-Repeating Background Image

Center your background image on the page, at the top. Replace the image URL with your own.

body {
background-image:url(http://www.yourwebsite.com/background-image.jpg);
background-repeat:no-repeat;
background-position: top center;
}

Top Centered Vertical Repeating Background Image

Center your background image on the page, at the top. It will repeat vertically down the page. Replace the image URL with your own.

body {
background-image:url(http://www.yourwebsite.com/background-image.jpg);
background-repeat:repeat-y;
background-position: top center;
}

Lists

Ordered List

This will create a numbered list. Replace list elements with your own:

<ol>
<li>List item 1</li>
<li>List item 2</li>
<li>List item 3</li>
</ol>

Unordered List With Bullets

This will create a list with bullets instead of numbers. Replace list elements with your own:

<ul>
<li>List item 1</li>
<li>List item 2</li>
<li>List item 3</li>
</ul>

Unordered List With Different Bullet Types

You can change the bullet type of any unordered list to circle, square, or disc (default):

<ul>
<li style="list-style-type:square">List item 1</li>
</ul>

Unordered List with Custom Image Bullet

If you want to use your own bullet image instead of the default ones, you can do that as well with a little CSS. Perhaps a star or a heart or check mark? You will need to create a small enough image and upload it somewhere online:

<ul style="list-style-image:url('http://yourimageurl.com/yourbullet.jpg')">
 <li>List item 1</li>
 <li>List item 2</li>
 <li>List item 3</li>
 </ul>

Special HTML Characters

Copyright symbol ©

&copy;

Less Than Symbol <

&lt;

Greater Than Symbol >

&gt;

Ampersand &

&amp;

Trademark Symbol ™

&trade;

Non-breaking Space

&nbsp;

Quotation Mark ”

&quot;

Registered Trademark ®

&reg;

Heart ♥

&hearts;

Euro sign €

&euro;

Left Arrow ←

&larr;

Right Arrow →

&rarr;

Up Arrow ↑

&uarr;

Down Arrow ↓

&darr;

Spade ♠

&spades;

Club ♣

&clubs;

Diamond ♦

&diams;

 

Create your forms with CSS3 and HTML5 validation

Posted on Updated on

Let’s look at how to create a functional form which validates users’ data, client-side. With that done, we’ll cover prettying it up using CSS, including some CSS3!

Step 1: Conceptualization Functionality

First we want to conceptualize what our form is going to look like and how it is going to function. For this example, let’s create a simple contact form that asks for the following information from the user:

  • Name
  • Email
  • Website
  • Message

We want to make sure the user is entering the information correctly. To accomplish this, we will use HTML5’s new client-side validation techniques. What about users who don’t have HTML5 capabilities? You can simply use server-side validation, but that will be beyond the scope of this article.

Step 2: Conceptualization Form

Let’s get an idea of what we want our form to look like by creating a rough mockup.

As you can see, the following elements make up our form:

  • Form Title Required fields notification
  • Form labels
  • Form inputs Placeholder text
  • Form field hints
  • Submit Button

Now that we’ve specified which elements make up our form, we can create the HTML markup.

Step 3: HTML Starter Code

Let’s create our basic HTML markup from the form concept we created.

01

02

03

04

05

06

07

08

09

10

<!DOCTYPE html>

<html>

<head>

<meta charset=”utf-8″>

<title>HTML5 Contact Form</title>

<link rel=”stylesheet” media=”screen” href=”styles.css” >

</head>

<body>

</body>

</html>

Up to this point, our HTML file will still appear blank in the browser. This is simply starter code for an HTML5 page.

Step 4: HTML Form

Let’s create the HTML form (we’ll leave the action method blank for now, since server-side validation will not be covered in this tutorial):

1

2

<form class=”contact_form” action=”” method=”post” name=”contact_form”>

</form>

Step 5: HTML Form Elements

To keep our form content organized and structured, we’ll wrap our form elements (label, input, etc) in a list. So let’s start by creating the form header and our first input element:

01

02

03

04

05

06

07

08

09

10

<ul>

<li>

<h2>Contact Us</h2>

<span class=”required_notification”>* Denotes Required Field</span>

</li>

<li>

<label for=”name”>Name:</label>

<input type=”text” name=”name” />

</li>

</ul>

Form Hints

As seen in our mockup, we’re going to have formatting hints for the “email” and “website” fields. So we’ll add our hints under the input fields where necessary, and give them a class so we can style them later.

1

2

3

4

5

<li>

<label for=”email”>Email:</label>

<input type=”text” name=”email” />

<span class=”form_hint”>Proper format “name@something.com”</span>

</li>

The Remaining Input Elements

Let’s go ahead and create our remaining form elements, remembering to wrap each section in a list item.

01

02

03

04

05

06

07

08

09

10

11

12

<li>

<label for=”website”>Website:</label>

<input type=”text” name=”website” />

<span class=”form_hint”>Proper format “http://someaddress.com“</span>

</li>

<li>

<label for=”message”>Message:</label>

<textarea name=”message” cols=”40″ rows=”6″ >

</li>

<li>

<button class=”submit” type=”submit”>Submit Form</button>

</li>

Step 6: Adding the Placeholder Attribute

One of the first improvements HTML5 brings to web forms (one you’re probably already familiar with) is the ability to set the placeholder text. Placeholder text is displayed when the input field is either empty or not in focus.

Let’s add the placeholder attribute to our input elements. This will help the user understand what they should enter in each field.

1

2

3

<input type=”text” name=”name” placeholder=”John Doe” />

<input type=”text” name=”email” placeholder=”john_doe@example.com” />

<input type=”text” name=”website” placeholder=”http://johndoe.com/” required/>

Quick Tip: Style your placeholder Text

Here’s a quick tip, if you want to style your placeholder text, there are some browser prefixes to help you:

1

2

3

4

5

6

7

:-moz-placeholder {

color: blue;

}

 

::-webkit-input-placeholder {

color: blue;

}

Support for the placeholder attribute is pretty well established in modern browsers (except IE9, sorry). If you really need to have it supported across all browsers, there are some javascript solutions you could look into.

Step 7: Basic CSS

Let’s add some basic CSS to give our form some structure. I’ll walk you through the rules:

Remove :focus Style

Webkit automatically adds some styling to input elements when they are in focus. Since we’ll be adding our own styles, we want to override these defaults:

1 *:focus {outline: none;}

Typographic Styles

Let’s add some typographic styles to our form elements:

1

2

3

body {font: 14px/21px “Lucida Sans”, “Lucida Grande”, “Lucida Sans Unicode”, sans-serif;}

.contact_form h2, .contact_form label {font-family:Georgia, Times, “Times New Roman”, serif;}

.form_hint, .required_notification {font-size: 11px;}

List Styles

Let’s style our list elements to give our form some structure:

01

02

03

04

05

06

07

08

09

10

11

12

.contact_form ul {

width:750px;

list-style-type:none;

list-style-position:outside;

margin:0px;

padding:0px;

}

.contact_form li{

padding:12px;

border-bottom:1px solid #eee;

position:relative;

}

Also, let’s add a slight border to the top and bottom sections of the form. We can accomplish this by using the :first-child and :last-child selectors. These select, as the names imply, the first and last elements in the <ul> list.

This adds some useful visual sectioning to our form. Keep in mind that these CSS selectors are not supported in older browsers. Since this is not vital to key functionality, we’re rewarding our those who use current browsers.

1

2

3

.contact_form li:first-child, .contact_form li:last-child {

border-bottom:1px solid #777;

}

Form Header

Let’s style the header section of our form. This includes the heading tag and the notification that informs users that the asterisk (*) indicates required fields.

01

02

03

04

05

06

07

08

09

10

.contact_form h2 {

margin:0;

display: inline;

}

.required_notification {

color:#d45252;

margin:5px 0 0 0;

display:inline;

float:right;

}

Form Input Elements

Let’s style all of our core form elements, the ones used to collect user information.

01

02

03

04

05

06

07

08

09

10

11

12

13

14

.contact_form label {

width:150px;

margin-top: 3px;

display:inline-block;

float:left;

padding:3px;

}

.contact_form input {

height:20px;

width:220px;

padding:5px 8px;

}

.contact_form textarea {padding:8px; width:300px;}

.contact_form button {margin-left:156px;}

Now, let’s add some extra visual CSS styles. Some of these are CSS3 styles that reward users who use modern browsers.

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

.contact_form input, .contact_form textarea {

border:1px solid #aaa;

box-shadow: 0px 0px 3px #ccc, 0 10px 15px #eee inset;

border-radius:2px;

}

.contact_form input:focus, .contact_form textarea:focus {

background: #fff;

border:1px solid #555;

box-shadow: 0 0 3px #aaa;

}

/* Button Style */

button.submit {

background-color: #68b12f;

background: -webkit-gradient(linear, left top, left bottom, from(#68b12f), to(#50911e));

background: -webkit-linear-gradient(top, #68b12f, #50911e);

background: -moz-linear-gradient(top, #68b12f, #50911e);

background: -ms-linear-gradient(top, #68b12f, #50911e);

background: -o-linear-gradient(top, #68b12f, #50911e);

background: linear-gradient(top, #68b12f, #50911e);

border: 1px solid #509111;

border-bottom: 1px solid #5b992b;

border-radius: 3px;

-webkit-border-radius: 3px;

-moz-border-radius: 3px;

-ms-border-radius: 3px;

-o-border-radius: 3px;

box-shadow: inset 0 1px 0 0 #9fd574;

-webkit-box-shadow: 0 1px 0 0 #9fd574 inset ;

-moz-box-shadow: 0 1px 0 0 #9fd574 inset;

-ms-box-shadow: 0 1px 0 0 #9fd574 inset;

-o-box-shadow: 0 1px 0 0 #9fd574 inset;

color: white;

font-weight: bold;

padding: 6px 20px;

text-align: center;

text-shadow: 0 -1px 0 #396715;

}

button.submit:hover {

opacity:.85;

cursor: pointer;

}

button.submit:active {

border: 1px solid #20911e;

box-shadow: 0 0 10px 5px #356b0b inset;

-webkit-box-shadow:0 0 10px 5px #356b0b inset ;

-moz-box-shadow: 0 0 10px 5px #356b0b inset;

-ms-box-shadow: 0 0 10px 5px #356b0b inset;

-o-box-shadow: 0 0 10px 5px #356b0b inset;

 

}

Step 8: Add Some Interactivity with CSS3

Let’s add a little bit of interactivity. We’ll make the field that is currently selected expand by adding some padding.

1

2

3

.contact_form input:focus, .contact_form textarea:focus { /* add this to the already existing style */

padding-right:70px;

}

Now for browsers that support it, let’s make the expansion of the field a smooth transition using CSS3.

1

2

3

4

5

6

.contact_form input, .contact_form textarea { /* add this to the already existing style */

-moz-transition: padding .25s;

-webkit-transition: padding .25s;

-o-transition: padding .25s;

transition: padding .25s;

}

Step 9: The required Attribute in HTML5

Now it’s time for what we’ve all been waiting for: HTML5’s form handling tools.

Adding the required attribute to any input/textarea element will tell the browser that a value is required before the form can be submitted. Thus, a form cannot be submitted if a required field has not been filled out.

So, let’s go ahead and add the required attribute to all of our form elements (because we want them all to be filled out).

1

2

3

4

<input type=”text” name=”name” required />

<input type=”text” name=”email” required />

<input type=”text” name=”website” required />

<textarea name=”message” cols=”40″ rows=”6″ required ></textarea>

Step 10: Styling required Fields

You’ll probably notice that, visually speaking, nothing happened by adding therequired attribute. We are going to style required fields using CSS. For this example, we are going to add a red asterisk as a background image in each required field. To accomplish this, we will want to first add some padding on the right side of our input where the background image will be (this will prevent text overlap if the field entry is a long string):

1

2

3

.contact_form input, .contact_form textarea {

padding-right:30px;

}

Now we will use the CSS pseudo selector :required to target all the form elements with a required attribute. I made a simple 16×16 pixel red asterisk icon in photoshop that will serve as the visual indicator of a required field.

1

2

3

input:required, textarea:required {

background: #fff url(images/red_asterisk.png) no-repeat 98% center;

}

What happens upon submission?

Right now, different browsers will do different things when a form using HTML5 elements is submitted. When the form is submitted, most browsers will prevent the form from being submitted and will display a “hint” to the user, marking the first field that is required and has no value. Visual styling and support for these ‘bubble fields’ is quite broad. Hopefully these behaviors will become standardized in the future.

You can see current browser support for the required attribute at quirksmode.

Quick Tip:

You can actually style the bubble message somewhat in webkit using the following:

1

2

3

::-webkit-validation-bubble-message {

padding: 1em;

}

Step 11: Understanding New HTML5 typeAttributes and Client-Side Validation

HTML5 validation works according to the type attribute that is set within the form fields. For years HTML only supported a handful of type attributes, such astype=”text” but with HTML5 there are a over a dozen new input types includingemail and url which we are going to use in our form.

By combining our input type attributes with the new required attribute, the browser can now validate the form’s data client-side. If a user’s browser does not support the new type attributes, such as type=”email”, it will simply default to type=”text”. This is actually pretty amazing. Essentially you have backwards compatibility in all browsers on earth, hooray!

So what if the browser does actually support the new type attributes? For desktop browsers there is no visual difference (unless specified by custom CSS rules). Atype=”text” field looks the same as a type=”email” field. However, for mobile browsers, there is a difference when it comes to the user interface.

An Example: The iPhone

Apple’s iPhone detects the form types and dynamically changes the on-screen keyboard by providing context-aware characters. For example, all email addresses require the following symbols: “@” and “.” So the iPhone provides those characters when the input type is specified to email.

Step 12: Changing the type Attributes

We already have our form fields set to the default type=”text”. But now we want to change the type attribute on our email and website fields to their corresponding HTML5 type.

1

2

<input type=”email” name=”email” placeholder=”john_doe@example.com” required />

<input type=”url” name=”website” placeholder=”http://johndoe.com” required/>

Step 13: HTML5 Validation

As mentioned before, HTML5 validation is based on your type attributes and it is on by default. There is no specific markup required in order to activate form validation. If you wish to turn it off, you can use the novalidate attribute like this:

1

2

3

4

<form novalidate>

<– do not validate this form –>

<input type=”text” />

</form>

Name Field

Let’s look at our first field that asks the user for his/her name. As described eariler, we’ve added the type=”text” attribute and the required attribute. This informs the web browser that this field is mandatory and it should validate the field as simply text. So as long as the user enters at least one character in that field, it will validate.

Now we will create our own CSS to style field inputs that are considered valid and invalid by the browser. If you remember, we used :required in our CSS to style all input elements with a required attribute. Now, we can style our required fields that are either valid or invalid by adding :valid or :invalid to our CSS rules.

First, let’s style fields that are invalid. For this example, we only want to style the form as invalid when it is in focus. We’ll add a red border, red shadow, and red icon created in photoshop to indicate the invalid field.

1

2

3

4

5

.contact_form input:focus:invalid, .contact_form textarea:focus:invalid { /* when a field is considered invalid by the browser */

background: #fff url(images/invalid.png) no-repeat 98% center;

box-shadow: 0 0 5px #d45252;

border-color: #b03535

}

Now, let’s create the rules that indicate the field is valid. We’ll add a green border, green shadow, and greed checkmark icon made in photoshop. This will be applied to all valid fields whether they are in focus or not.

1

2

3

4

5

.contact_form input:required:valid, .contact_form textarea:required:valid { /* when a field is considered valid by the browser */

background: #fff url(images/valid.png) no-repeat 98% center;

box-shadow: 0 0 5px #5cd053;

border-color: #28921f;

}

Now when you focus on a form field, the red invalid styling is shown. As soon as a single character has been entered in the field, it is validated and green CSS styles are shown to indicate that fact.

Email and URL Fields

Our CSS styles and validation rules are already applied to the email field because we set the type and required attributes earlier.

Step 14: Introducing the HTML5 pattern Attribute

Using the type=”email” attribute as an example, it appears that most browsers validate that field as *@* (any character + the “@” symbol + any character). This is obviously not very limiting but it does prevent users from entering spaces or values that are entirely wrong.

In the example of the type=”url” attribute, it appears as though the minimum requirement for most browsers is any character followed by a colon. So, if you entered “h:” then the field would validate. This is not extremely helpful but it does prevent users from entering irrelevant information, such as their email or home address. Now, you could handle being more specific on with your input values in your server-side validation; however, we’re going to talk about how to do that in HTML5.

The pattern Attribute

The pattern attribute accepts a javascript regular expression. This expression is used, rather than the browser default, to validate the field’s value. So our HTML now looks like this:

1 <input type=”url” name=”website” placeholder=”http://johndoe.com” required pattern=”(http|https)://.+” />

Now our field will only accept values that start with “http://&#8221; or “https://&#8221; and one additional character. These regular expression patterns can be confusing at first, but once you take the time to learn them, your forms will be open to a whole new world.

Step 15: Form Field Hints (CSS)

Now let’s style our form hints that tell the user the format they should use when entering their information.

01

02

03

04

05

06

07

08

09

10

.form_hint {

background: #d45252;

border-radius: 3px 3px 3px 3px;

color: white;

margin-left:8px;

padding: 1px 6px;

z-index: 999; /* hints stay above all other elements */

position: absolute; /* allows proper formatting if hint is two lines */

display: none;

}

We set display:none because we are only going to show the hints when the user focuses on the input field. We also set our tooltips to default to our red invalid color, because they are always considered invalid until the proper information is entered in.

Using the ::before Selector

Now we want to add a little triangle to our hint boxes that help direct and guide the eye. This can be done using images, but in our case we are going to do it using pure CSS.

Because it is purely a presentational element that is not vital to the page’s functionality, we are going to add a small triangle that points left using the ::beforepseudo selector. We can do this by using one of the unicode geometric shapes.

Normally we would use the HTML Unicode format to display these in our HTML (as shown in the image above). However, because we will be using the ::before CSS selector, we have to use the triangle’s corresponding escaped unicode when using the content:”” rule. Then we just use positioning to get it where we want it.

1

2

3

4

5

6

7

.form_hint::before {

content: “\25C0”; /* left point triangle in escaped unicode */

color:#d45252;

position: absolute;

top:1px;

left:-6px;

}

Using the + Adjacent Selector

Finally, we are going to use the CSS adjacent selector to show and hide our form field hints. The adjacent selector (x + y) selects the element that is immediately preceded by the former element. Since our field hints come right after our input fields in our HTML, we can use this selector to show/hide the tooltips.

1

2

3

.contact_form input:focus + .form_hint {display: inline;}

.contact_form input:required:valid + .form_hint {background: #28921f;} /* change form hint color when valid */

.contact_form input:required:valid + .form_hint::before {color:#28921f;} /* change form hint arrow color when valid */

As you can see from the CSS, we also set the form hints to change colors along with the input’s border when a field is valid or invalid.

Step 16: Sit Back and Admire Your Beautiful HTML5 Form

Go ahead and take a look at your final product!

Conclusion

As you can see, the new HTML5 form features are pretty neat! Everything is backwards compatible so incorporating these new features into your website won’t break anything.

HTML5 validation is coming closer to replacing client-side validation in helping users properly fill out their online forms. However, HTML5 validation still does not replace server-side validation. For the time being, it’s best to use both methods when handling user-submitted information. Thanks for reading!

 

 

 

HTML5 new tags

Posted on

website-code-html5-optimized

HTML 5 is the new major version of HTML. HTML5 brings a host of new elements and attributes to allow developers to make their documents more easily understood by other systems (especially search engines). In addition, HTML 5 will also include fancy APIs for drawing graphics on screen, storing data offline, dragging and dropping, and a lot more. Here are new HTML5 tags that will make it easier for you to write your Web sites.

 

Doctype

In HTML 5, there is only one doctype. It is declared in the beginning of the page by <!DOCTYPE HTML>. It simply tells the browser that it’s dealing with an HTML-document.

 

<video> and <audio>

One of the biggest uses for Flash, Silverlight, and similar technologies is to get a multimedia item to play. With HTML5 supporting the new video and audio controls, those technologies are now relegated to being used for fallback status. The browser can now natively display the controls, and the content can be manipulated through JavaScript. Don’t let the codec confusion scare you away. You can specify multiple sources for content, so you can make sure that your multimedia will play regardless of what codecs the user’s browser supports.

 

<input> type attributes

The venerable <input> element now has a number of new values for the type attribute, and browsers do some pretty slick things depending on its value. For example, set type to “datetime” and browsers can show calendar/clock controls to pick the right time, a trick that used to require JavaScript. There is a wide variety of type attributes, and learning them (and the additional attributes that go with some of them) will eliminate the need for a lot of JavaScript work.

 

<canvas>

The <canvas> tag gives HTML a bitmapped surface to work with, much like what you would use with GDI+ or the .NET Image object. While <canvas> isn’t perfect (layers need to be replicated by using multiple canvas objects stacked on top of each other, for example), it is a great way to build charts and graphs, which have been a traditional weak spot in HTML, as well as custom graphics. And that is just a start!

 

<header> and <footer>

The <header> and <footer> tags are two of the new semantic tags available. These two tags do not get you anything above and beyond <div> for the actual display. But they will reap long-term rewards for your search engine efforts, since the search engines will be able to tell the difference between “content” and things that are important to the user but that aren’t the actual content.

 

<nav>

The nav-tag is used to contain navigational elements, such as the main navigation on a site or more specialized navigation like next/previous-links.

 

<article> and <section>

The <article> and <section> tags are two more semantic tags that will boost your search engine visibility. Articles can be composed of multiple sections, and a section can have multiple articles. Confusing? Not really. An article represents a full block of content, and a section is a piece of a bigger whole. For example, if you are looking at a blog, the front page might have a section for the listing of all the posts, and each post would be an article with a section for the actual post and another for comments.

<aside>

The aside tag is used to wrap around content related to the main content of the page that could still stand on it’s own and make sense.

 

 <output>

The new <output> tag is unique, in that it expects its content to be generated dynamically with JavaScript. It has a value attribute, which can be manipulated through the DOM with JavaScript to change what is displayed on the screen. This is much more convenient than the current ways of doing things.

 

<details>

It seems like every Web site needs to have an expanding/collapsing block of text. While this is easy enough to do with JavaScript or server-side code, the <details> tag makes it even easier. It does exactly what we’ve all been doing for years now: makes a simple block that expands and collapses the content when the header is clicked. The <details> tag does not have widespread support yet, but it will soon.

 

 <figure> and <figcaption>

<figure> is a container for content (typically images, but it can be anything), and <figcaption> (which gets put inside the <figure> tag) provides a caption or subtitle for the contents of the <figure> tag. For example, you could have four images representing charts of sales growth within a <figure> tag, and a <figcaption> with text like “Year-to-year sales growth, 1989 – 1993.” The images would be shown next to each other with the text running below all four.

 <progress>and <meter>

<progress> and <meter> are similar. You use <progress> for a task or a “measure how complete something is” scenario. It also has an indeterminate mode for something that has an unknown duration (like searching a database). The <meter> tag is for gauges and measurements of value (thermometers, quantity used, etc.). While they may look alike on the screen in many cases, they do have different semantic meanings.

 

<datalist>

The <datalist> tag acts like a combo box, where the system provides a pre-made list of suggestions, but users are free to type in their own input as well. There are tons of possible uses for this, such as a search box pre-populated with items based on the user’s history. This is another one of those things that currently requires a bunch of JavaScript (or JavaScript libraries) to handle but that can be done natively with HTML5.

 

Other tags?

What other new tags have you found especially useful? Share your HTML5 experiences with us.

HTML Attributes

Posted on Updated on

Attributes provide additional information about HTML elements.


HTML Attributes

  • HTML elements can have attributes
  • Attributes provide additional information about an element
  • Attributes are always specified in the start tag
  • Attributes come in name/value pairs like: name=”value”

The lang Attribute

The document language can be declared in the tag.

The language is declared in the lang attribute.

Declaring a language is important for accessibility applications (screen readers) and search engines:

Example

<!DOCTYPE html>
<html lang=“en-US”>
<body><h1>My First Heading</h1><p>My first paragraph.</p>

</body>
</html>

The first two letters specify the language (en). If there is a dialect, use two more letters (US).


The title Attribute

HTML paragraphs are defined with the

tag.

In this example, the

element has a title attribute. The value of the attribute is “About Learnforweb“:

Example

<p title=“About Learnforweb”>
Learnforweb is a web developer’s site.
It provides tutorials and useful tricks covering
many aspects of Front-end Development,
including HTML, CSS, Bootstrap, JavaScript etc.
</p>
Note When you move the mouse over the element, the title will be displayed as a tooltip.

The href Attribute

HTML links are defined with the tag. The link address is specified in the href attribute:

Example

<a href=learnforweb.wordpress.com>This is a link</a>

You will learn more about links and the tag later in this tutorial.


Size Attributes

HTML images are defined with the tag.

The filename of the source (src), and the size of the image (width and height) are all provided as attributes:

Example

<img src=“code.jpg” width=“104” height=“142”>

The image size is specified in pixels: width=”104″ means 104 screen pixels wide.

You will learn more about images and the tag later in this tutorial.


The alt Attribute

The alt attribute specifies an alternative text to be used, when an HTML element cannot be displayed.

The value of the attribute can be read by “screen readers”. This way, someone “listening” to the webpage, i.e. a blind person, can “hear” the element.

Example

<img src=“Code.jpg” alt=“Coding” width=“104” height=“142”>

We Suggest: Always Use Lowercase Attributes

The HTML5 standard does not require lower case attribute names.

The title attribute can be written with upper or lower case like Title and/or TITLE.

W3C recommends lowercase in HTML4, and demands lowercase for stricter document types like XHTML.

Note Lower case is the most common. Lower case is easier to type.
At Learnforweb we always use lower case attribute names.

We Suggest: Always Quote Attribute Values

The HTML5 standard does not require quotes around attribute values.

The href attribute, demonstrated above, can be written as:

W3C recommends quotes in HTML4, and demands quotes for stricter document types like XHTML.

Sometimes it is necessary to use quotes. This will not display correctly, because it contains a space:

Example

<p title=About W3Schools>
Note Using quotes are the most common. Omitting quotes can produce errors.

Single or Double Quotes?

Double style quotes are the most common in HTML, but single style can also be used.

In some situations, when the attribute value itself contains double quotes, it is necessary to use single quotes:

Example

<p title=‘John “ShotGun” Nelson’>

Or vice versa:

Example

<p title=“John ‘ShotGun’ Nelson”>

Chapter Summary

  • All HTML elements can have attributes
  • The HTML title attribute provides additional “tool-tip” information
  • The HTML href attribute provides address information for links
  • The HTML width and height attributes provide size information for images
  • The HTML alt attribute provides text for screen readers
  • At W3Schools we always use lowercase HTML attribute names
  • At W3Schools we always quote attributes with double quotes

HTML Attributes

Below is an alphabetical list of some attributes often used in HTML:

Attribute Description
alt Specifies an alternative text for an image
disabled Specifies that an input element should be disabled
href Specifies the URL (web address) for a link
id Specifies a unique id for an element
src Specifies the URL (web address) for an image
style Specifies an inline CSS style for an element
title Specifies extra information about an element (displayed as a tool tip)
value Specifies the value (text content) for an input element.

HTML Elements

Posted on

HTML documents are made up by HTML elements.


HTML Elements

HTML elements are written with a start tag, with an end tag, with the content in between:

<tagname>content</tagname>

The HTML element is everything from the start tag to the end tag:

<p>My first HTML paragraph.</p>
Start tag Element content End tag
<h1> My First Heading </h1>
<p> My first paragraph. </p>
<br>
Note Some HTML elements do not have an end tag.

Nested HTML Elements

HTML elements can be nested (elements can contain elements).

All HTML documents consist of nested HTML elements.

This example contains 4 HTML elements:

Example

<!DOCTYPE html>
<html>
<body>

<h1>My First Heading</h1>
<p>My first paragraph.</p>

</body>
</html>

HTML Example Explained

The <html> element defines the whole document.

It has a start tag <html> and an end tag </html>.

The element content is another HTML element (the <body> element).

<html>
<body>

<h1>My First Heading</h1>
<p>My first paragraph.</p>

</body>
</html>

The <body> element defines the document body.

It has a start tag <body> and an end tag </body>.

The element content is two other HTML elements (<h1> and <p>).

<body>

<h1>My First Heading</h1>
<p>My first paragraph.</p>

</body>

The <h1> element defines a heading.

It has a start tag <h1> and an end tag </h1>.

The element content is: My First Heading.

<h1>My First Heading</h1>

The <p> element defines a paragraph.

It has a start tag <p> and an end tag </p>.

The element content is: My first paragraph.

<p>My first paragraph.</p>

Don’t Forget the End Tag

Some HTML elements will display correctly, even if you forget the end tag:

Example

<html>
<body>

<p>This is a paragraph
<p>This is a paragraph

</body>
</html>

The example above works in all browsers, because the closing tag is considered optional.

Never rely on this. It might 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).

Empty elements can be “closed” in the opening tag like this: <br />.

HTML5 does not require empty elements to be closed. But if you want stricter validation, or you need to make your document readable by XML parsers, you should close all HTML elements.


HTML Tip: Use Lowercase Tags

HTML tags are not case sensitive: <P> means the same as <p>.

The HTML5 standard does not require lowercase tags, but W3C recommends lowercase in HTML4, and demandslowercase for stricter document types like XHTML.

Note At Learnforweb we always use lowercase tags.

HTML Basic Examples

Posted on Updated on

Don’t worry if these examples use tags you have not learned.

You will learn them in the next chapters.


HTML Documents

All HTML documents must start with a type declaration:.

The HTML document itself begins with and ends with .

The visible part of the HTML document is between and .

Example

<!DOCTYPE html>
<html>
<body><h1>My First Heading</h1><p>My first paragraph.</p></body>
</html>


HTML Headings

HTML headings are defined with the

to

tags:

Example

<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

tag:

Example

<p>This is a paragraph.</p>
<p>This is another paragraph.</p>

HTML Links

HTML links are defined with the tag:

Example

<a href=http://www.learnforweb.wordpress.com>This is a link</a>

The link address is specified in the href attribute.

Attributes are used to provide additional information about HTML elements.


HTML Images

HTML images are defined with the tag.

The source file (src), alternative text (alt), and size (width and height) are provided as attributes:

Example

<img src=“code.jpg” alt=“Coding” width=“104” height=“142”>
Note You will learn more about attributes in a later chapter.

HTML Editors

Posted on

Write HTML Using Notepad or TextEdit

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

  • Adobe Dreamweaver
  • Microsoft Expression Web
  • CoffeeCup HTML Editor

However, for learning HTML we recommend a text editor like Notepad (PC) or TextEdit (Mac).

We believe 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: Open Notepad

To open Notepad in Windows 7 or earlier:

Click Start (bottom left on your screen). Click All Programs. Click Accessories. Click Notepad.

To open Notepad in Windows 8 or later:

Open the Start Screen (the window symbol at the bottom left on your screen). Type Notepad.


Step 2: Write Some HTML

Write or copy some HTML into Notepad.

<!DOCTYPE html>
<html>
<body>

<h1>My First Heading</h1>

<p>My first paragraph.</p>

</body>
</html>

Notepad


Step 3: Save the HTML Page

Save the file on your computer.

Select File > Save as in the Notepad menu.

Name the file “index.htm” or any other name ending with htm.

UTF-8 is the preferred encoding for HTML files.

ANSI encoding covers US and Western European characters only.

View in Browser

Note You can use either .htm or .html as file extension. There is no difference, it is up to you.

Step 4: View HTML Page in Your Browser

Open the saved HTML file in your favorite browser. The result will look much like this:

View in Browser

Note To open a file in a browser, double click on the file, or right-click, and choose open with.