Home > Articles > Mobile Application Development & Programming

  • Print
  • + Share This
This chapter is from the book

Clean Code

When you’re building HTML for RWD sites, you should always strive to keep it as clean and clear as possible. The technical term for this is well-formed. Well-formed HTML follows these guidelines:

  • There is a doctype at the top of the document.
  • Tags should nest correctly, inside to outside; for example, <b><i>text</i></b> is correct, and <b><i>text</b></i> is incorrect.
  • Attributes with spaces in their values should be quoted using single or double quotation marks.
  • Comments are not allowed inside tags.
  • You should escape special characters used in HTML, such as ampersand (&amp;), a less-than sign (&lt;), and a greater-than sign (&gt;).

Well-formed XHTML involves additional rules, such as always closing every tag and using a closing slash in singleton tags, including an XML declaration, and quoting all attributes. But if you are using HTML5, the rules are not as strict.

The other part of clean code is using only the elements and attributes you need and nothing more. Try to consider what your site needs and limit your HTML to only those elements.

Think about what the content is when you are writing HTML. A paragraph should be in the <p> element. For a list, you should use one of the list elements. You get the picture. In the next section, you will learn about some of the new HTML5 elements that help define your content right in the HTML, using semantics.

  • + Share This
  • 🔖 Save To Your Account