Home > Articles > Mobile Application Development & Programming

HTML for Responsive Web Design

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">.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020