Home > Articles > Mobile Application Development & Programming

HTML for Responsive Web Design

  • Print
  • + Share This
Jennifer Kyrnin teaches you the basics of HTML so that you can put together a decent website and make it responsive, in this chapter from Responsive Web Design in 24 Hours, Sams Teach Yourself.
This chapter is from the book

Responsive web design doesn’t add any new HTML tags or attributes: You simply write your HTML so that it is well-formed, valid, and semantic.

The best responsive sites are sites that also use progressive enhancement, as you learned in Hour 4, “Progressive Enhancement.” These sites keep the HTML, CSS, and JavaScript separate so that the pages are easier to maintain and load more quickly.

In this hour you will learn the basics of HTML so that you can put together a decent website and make it responsive. However, there is more to HTML than you can learn in just one hour, so if you don’t know HTML at all, you should consider a book like Sams Teach Yourself HTML and CSS in 24 Hours by Julie C. Meloni.

Using HTML5

HTML5 is the most recent version of HTML and provides the most assistance to web designers who want to use progressive enhancement and RWD. While you can build RWD sites using other versions of HTML, it’s best to stay as up-to-date as possible. This book uses HTML5 code samples.

Tags Every Page Should Contain

There are several HTML tags that every web page should contain. These tags may not be required for valid HTML, but they provide information about the page to the browser to make them easier to use. Every web page should contain these tags:

  • <!doctype>
  • <html>
  • <head>
  • <meta charset>
  • <title>
  • <body>

If your web page contains these elements, it contains the minimum HTML required to start building a responsive page. Listing 5.1 provides a standard template you can use to start any web page. Note that the tags listed above have both starting and ending tags as well as some attributes.

LISTING 5.1 A Basic HTML Template

<!doctype html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Untitled Document</title>
  </head>
  <body>
</body>
</html>

The first line in the code is the doctype. The doctype <!doctype html> tells the browser that this is HTML5.

Then you place the opening <html> tag. Be sure to surround the entire document with this element by including the </html> tag at the very end. This tells the browser that this is an HTML document, and also it tells the browser where it should expect to see HTML tags.

Valid HTML keeps all the content inside the <html> and </html> tags, but if content slips out, the browser will still parse it. Some content management systems append content to the top or bottom of the HTML, without placing it inside these tags. This content will still be parsed and displayed by browsers, but it could cause problems. Keep your content inside the <html> and </html> tags, and you’ll be safe.

The <head> and </head> tags contain details about the web page that may not be visible to the browser—things like metadata, the title, and any links to CSS and JavaScript. The basic template in Listing 5.1 includes a <meta charset> tag and a <title> tag.

The <meta charset> tag is an important one for keeping your web pages secure. It should always be the very first tag in the head of your HTML documents. It tells the browser what character set the page uses. If you don’t define the character set, or if you define it later in the document, you open up your site to hackers who can manipulate the character set and inject malicious code into the site.

Once you’ve defined the head information of your document, all you have left is the <body> element. This is where all the visible content goes. It contains what most people consider the meat of the web page. Anything that you type inside the <body> and </body> tags will show in the browser window.

Basic Tags for Web Content

While the tags already listed in this lesson are all you need to create a website, the site would be very plain and hard to read if you used only these tags. While there are dozens of HTML tags you can use (I list the new HTML5 elements at http://www.html5in24hours.com/2012/04/new-html5-elements/), there are only a few you need to know about to start creating a decent web page:

  • <h1>, <h2>, and <h3>
  • <p> and <br>
  • <a>
  • <strong> and <em>
  • <img>, <audio>, and <video>

Most web pages start with a headline, and in HTML you use the headline tags <h1>, <h2>, and <h3> to define them. They are numbered, and you should use them in order, with <h1> first through <h6> last. Most sites don’t go more than two or three levels deep, so you really only need to know <h2> and <h3>.

Once you have a headline, you can start adding content in paragraphs by using the <p> tag. Browsers typically display a paragraph as a block of text separated by some space. But if you want to drop down just one line of text, you use the <br> tag. This is a singleton tag and does not require a closing tag. You will sometimes see it written as <br/>, with the additional slash. This is a remnant from XHTML. You don’t need the closing slash, and most browsers recognize the tag either way.

You can put these tags together to create a web page. Figure 5.1 shows how a simple web page would look, and Listing 5.2 is the HTML for creating that web page. As you can see, the HTML is still very plain. But because content is the most important part of your website, you need to make sure it is visible and correct first.

Figure 5-1

FIGURE 5.1 A simple web page with content.

The <a> tag provides a link to another document. You define a link by using the href="" attribute. Then any text or image that is inside the <a> element will be clickable in the browser and will take the customer to the new location.

The <strong> and <em> tags let you provide some emphasis to the text in your paragraphs. In most browsers the <strong> tag makes the text bold and <em> makes the text italic. But you will be able to define how you want those to display in your style sheets. You’ll learn more about that in Hour 6, “Basic CSS.”

The last three tags let you add multimedia into your web pages. The <img> tag adds an image to the page, using the src="" attribute to define the location of the image on your web server. You can add sound files by using the <audio> tag and videos by using the <video> tag. I cover these in more detail in Hour 16, “Videos and Other Media in RWD.”

LISTING 5.2 Adding Some Content to the Template

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Dandylions</title>
</head>
<body>
<h1>Dandylions</h1>
<h2>Not Your Mother's Weed</h2>
<p><strong><em>Taraxacum</em></strong> <em>/te.jpg'rækse.jpgkum/</em> is a large
genus of flowering plants in the family Asteraceae. They are native to
Eurasia and North and South America, and two species, T. officinale and
T. erythrospermum, are found as weeds worldwide. Both species are edible
in their entirety. The common name <strong>dandelion</strong>
(<em>/'dændila |. e.jpgn/</em> dan-di-ly-e.jpgn, from French dent-de-lion, meaning
"lion's tooth") is given to members of the genus, and like other members
of the Asteraceae family, they have very small flowers collected together
into a composite flower head. Each single flower in a head is called a
floret. Many <em>Taraxacum</em> species produce seeds asexually by
apomixis, where the seeds are produced without pollination, resulting in
offspring that are genetically identical to the parent plant.</p>
<img src="images/dandy.jpg" width="400" height="300" alt=""/>
<p>Text from
<a href="http://en.wikipedia.org/wiki/Dandelion">Wikipedia</a></p>
</body>
</html>

HTML for Layout

You should also consider learning about some of the tags that are typically used for layout and style. There are two in particular that you should know:

  • <div>
  • <span>

These HTML tags provide no semantic meaning to your content and should be used to add hooks for CSS and JavaScript.

The <div> element acts like a container element, and you can place large blocks of text in it. It is called a block element, and most browsers add a line both before and after, similar to a paragraph.

Most people use <div> to create layout sections and then style those sections with CSS. Listing 5.3 shows how you might add some sections to your HTML so that you can style them later.

LISTING 5.3 HTML with Some Divisions

<!doctype html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Dandylions</title>
  </head>
  <body>
    <div id="main">
    <h1>Dandylions</h1>
    <div id="article">
      <h2>Not Your Mother's Weed</h2>
      <p><strong><em>Taraxacum</em></strong> <em>/te.jpg'rækse.jpgkum/</em> is a
      large genus of flowering plants in the family Asteraceae. They are
      native to Eurasia and North and South America, and two species, T.
      officinale and T. erythrospermum, are found as weeds worldwide.
      Both species are edible in their entirety. The common name
      <strong>dandelion</strong> (<em>/'dændila |.e.jpgn/</em> dan-di-ly-e.jpgn,
      from French dent-de-lion, meaning "lion's tooth") is given to
      members of the genus, and like other members of the Asteraceae
      family, they have very small flowers collected together into a
      composite flower head. Each single flower in a head is called a
      floret. Many <em>Taraxacum</em> species produce seeds asexually by
      apomixis, where the seeds are produced without pollination,
      resulting in offspring that are genetically identical to the parent
      plant.</p>
    </div>
    <div id="sidebar">
      <img src="images/dandy.jpg" width="400" height="300" alt=""/>
    </div>
    <div id="footer">
      <p>Text from
      <a href="http://en.wikipedia.org/wiki/Dandelion">Wikipedia</a></p>
    </div>
    </div>
  </body>
</html>

The <span> element works inside paragraphs to select small blocks of content. It is called an inline element. It does not add line breaks, and it allows you to mark up individual words in the content. Listing 5.4 shows a block of text with some words called out with the <span> tag.

LISTING 5.4 A Block of Text with Highlighted Words

<!doctype html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Dandylions</title>
  </head>
  <body>
    <div id="main">
    <h1>Dandylions</h1>
    <div id="article">
      <h2>Not Your Mother's Weed</h2>
      <p><span class="pronounce"><strong><em>Taraxacum</em></strong>
      <em>/te.jpg'rækse.jpgkum/</em></span> is a large genus of flowering plants
      in the family Asteraceae. They are native to Eurasia and North and
      South America, and two species, T. officinale and T.
      erythrospermum, are found as weeds worldwide. Both species are
      edible in their entirety. The common name
      <span class="pronounce"><strong>dandelion</strong>
      <em>/'dændila |.e.jpgn/</em></span> (dan-di-ly-e.jpgn, from French
      dent-de-lion, meaning "lion's tooth") is given to members of the
      genus, and like other members of the Asteraceae family, they have
      very small flowers collected together into a composite flower head.
      Each single flower in a head is called a floret. Many
      <em>Taraxacum</em> species produce seeds asexually by apomixis,
      where the seeds are produced without pollination, resulting in
      offspring that are genetically identical to the parent plant.</p>
    </div>
    <div id="sidebar">
      <img src="images/dandy.jpg" width="400" height="300" alt=""/>
    </div>
    <div id="footer">
      <p>Text from
      <a href="http://en.wikipedia.org/wiki/Dandelion">Wikipedia</a></p>
    </div>
    </div>
</body>
</html>

Some Useful Attributes

Nearly every tag in HTML has attributes. These are keywords that are defined within a tag and give the browser more information about that tag. You have already used attributes with the <img src=""> tag and the <a href=""> tag. But there are a couple more attributes that you should know about:

  • id
  • class

You can add these attributes to any HTML tag in your document to provide additional information about that element.

You use the id attribute to give an element a unique name. The id must be unique to the page it is on. In other words, there can be only one. But you can give every single tag on your page a unique id.

You use id to identify an element. You can then link to that element by using the pound sign (#) in your URL, followed by the id value you assign. For example, if you had an element <div id="main">, you could then write a link to that element by typing <a href="#main">link to main</a>.

You can also use the id attribute as a hook for styles and scripts. Because it must be unique on the page, you know that when you attach a style to that id, you will affect only one element. This attribute also makes the style rule more specific, which means it’s more likely to be applied. You will learn more about CSS specificity in Hour 6.

Like the id attribute, the class attribute allows you to apply styles and scripts to an element. But it does not have to be unique on the page. This means you can apply a class to multiple elements on the page, and any style rules that are written to that class will be applied to all the elements. For instance, say you want some of your <h1> headlines to be red, but others should remain the default color. You could give the red headlines a special class that you would style as red in your CSS: <h1 class="highlight">.

One of the nicest things about using classes is that you aren’t limited to just one. You can include multiple class names on any element to add styles to the element or hook up with your scripts. To add a second class to an element, simply separate the classes with a space, like this: <h1 class="highlight fancy">.

  • + Share This
  • 🔖 Save To Your Account