Section 1.2. Writing HTML for CSS


1.2. Writing HTML for CSS

If you're new to Web design, you may need some helpful hints to guide your forays into HTML (and to steer clear of well-intentioned, but out-of-date HTML techniques). And if you've been building Web pages for a while, then you may have picked up a few bad HTML-writing habits that you're better off forgetting . The rest of this chapter introduces you to some HTML writing habits that will make your mother proudand help you get the most out of CSS.

1.2.1. Think Structure

HTML adds meaning to text by logically dividing it and identifying the role that text plays on the page: for example, the <h1> tag's the most important introduction to a page's content. Other headers let you divide up the content into other, less important, but related sections. Just like the book you're holding, for example, a Web page should have a logical structure. Each chapter in this book has a title (think <h1>); and several sections (think <h2>), which in turn contain smaller subsections. Imagine how much harder it would be to read these pages if every word just ran together as one long paragraph.


Note: For a good resource on HTML/ XHTML check out HTML & XHTML: The Definitive Guide (O'Reilly) by Chuck Musciano and Bill Kennedy, or visit www.w3schools.com for online HTML and XHTML tutorials. For a quick list of all available HTML and XHTML tags, visit www.w3schools.com/tags/.

HTML provides many other tags besides headers for marking up (that's the M in HTML) content to identify its role on the page. Among the most popular are the <p> tag for paragraphs of text, and the <ul> tag for creating bulleted (non-numbered) lists. Lesser-known tags can indicate very specific types of content, like <abbr> for abbreviations and <code> for computer code.

When writing HTML for CSS, use a tag that comes close to matching the role the content plays in the page, not the way it looks (see Figure 1-2). For example, a bunch of links in a navigation bar isn't really a headline and it isn't a regular paragraph of text. It's most like a bulleted list of options, so the <ul> tag is a good choice. If you're saying "but items in a bulleted list are stacked vertically one on top of the other, and I want a horizontal navigation bar where each link sits next to the previous link," don't worry. With CSS magic you can convert a vertical list of links into a stylish horizontal navigation bar as described in Chapter 9.

1.2.2. Two New HTML Tags to Learn

HTML's motley assortment of tags doesn't cover the wide range of content you'll probably add to a page. Sure, <code> is great for marking up computer program code, but most folks would find a <recipe> tag handier. Too bad there isn't one. Fortunately, HTML provides two generic tags that let you better identify content, and, in the process, provide "handles" that let you attach CSS styles to different elements on a page.

Figure 1-2. Old School, New School. Before CSS, designers had to resort to the <font> tag and other extra HTML to achieve certain visual effects (top). You can achieve the same (and often better) look with a lot less HTML code (bottom.) In addition, by using CSS to format your Web pages, you're free to write HTML that follows the logical structure of the page's content.

GEM IN THE ROUGH
Simple HTML Is Search Engine Friendly

Once you take the mental leap of picturing HTML as the way to structure a document's content, and CSS as the tool for making that content look good, you'll discover additional benefits to writing lean, mean HTML. For one thing, you may boost your search engine ranking as determined by sites like Google, Yahoo, and MSN. That's because when search engines crawl the Web, indexing the content on Web sites, they must go through all the HTML on a page to discover the actual content. The old HTML-way of using special tags (like <font>) and lots of tables to design a page, gets in the way of a search engine's job. In fact, some search engines stop reading a page's HTML after a certain number of characters . When you use a lot of HTML just for design, the search engine may miss important content on the page, or even fail to rank it at all.

By contrast, simple, structured HTML is easy for a search engine to read and index. Using an <h1> tag to indicate the most important topic of the page (as opposed to just making the text big and bold) is smart strategy: Search engines give greater weight to the contents inside that tag while indexing the page.

To see Google's suggestions for building search-friendly Web sites, visit www.google.com/webmasters/guidelines.html.

You can also hear a podcast of an excellent speech on SEO (search engine optimization) from the 2005 Web Visions conference at http://www.webvisionsevent.com/podcasts/WV05_Alan_Knecht.mp3 and read an article by that speaker covering the same topic at: www.digital-web.com/articles/seo_and_your_web_site/.


The < div > tag and the < span > tag are like empty vessels that you fill with content. Since they have no inherent visual properties, you can use CSS to make them look any way you want. The <div> (for division ) tag indicates any discrete block of content, much like a paragraph or a headline. But you can also use it to surround any number of other elements, so you can insert a headline, a bunch of paragraphs, and a bulleted list inside a single <div> block. The <div> tag is a great way to subdivide a page into logical areas, like a banner, footer, sidebar, and so on. Using CSS, you can later position each area to create sophisticated page layouts (a topic that's covered in Part Three of this book).

The < span> tag is used for inline elements; that is, words or phrases that appear inside of a larger paragraph or heading. Treat it just like other inline HTML tags such as the <a> tag (for adding a link to some text in a paragraph) or the <strong> tag (for emphasizing a word in a paragraph). For example, you could use a <span> tag to indicate the name of a company, and then use CSS to highlight the name using a different font, color , and so on. Here's an example of those tags in action, complete with a sneak peek of a couple of attributes id and class frequently used to attach styles to parts of a page.

  <div id="footer">  <p>Copyright 2006,  <span class="bizName">  CosmoFarmer.com  </span><  /p> <p>Call customer service at 555-555-5501 for more information</p>  </div>  

This brief introduction isn't the last you'll see of these tags. They're used frequently in CSS-heavy Web pages, and in this book you'll learn how to use them in combination with CSS to gain creative control over your Web pages (Section 3.1).

1.2.3. HTML to Forget

CSS lets you write simpler HTML for one big reason: You can stop using a bunch of tags and attributes that only make a page better looking. The <font> tag is the most glaring example. Its sole purpose is to add a color, size and font to text. It doesn't do anything to make the structure of the page more understandable.

Here's a list of tags and attributes you can easily replace with CSS:

  • Ditch < font > for controlling the display of text. CSS does a much better job with text. (See Chapter 6 for text-formatting techniques.)

  • Stop using < b > and < i > to make text bold and italic. CSS can make any tag bold or italic, so you don't need these formatting-specific tags. However, if you want to really emphasize a word or phrase, then use the < strong > tag (browsers display <strong> text as bold anyway). For slightly less emphasis, use the < em > tag to emphasize text (browsers italicize content inside this tag).


    Note: To italicize a publication's title, the <cite> tag kills two birds with one stone. It puts the title in italics and tags it as a cited work for search engines' benefit. This one's a keeper.
  • Skip the < table > tag for page layout. Use it only to display tabular information like spreadsheets, schedules, and charts . As you'll see in Part 3 of this book, you can do all your layout with CSS for much less time and code than the table tag tango.

  • Eliminate the awkward < body > tag attributes that enhance only the presentation of the content: background, bgcolor , text, link, alink , and vlink set colors and images for the page, text, and links. CSS gets the job done better (see Chapter 7 and Chapter 8 for CSS equivalents of these attributes). Also trash the browser-specific attributes used to set margins for a page: leftmargin, topmargin, marginwidth, marginheight . CSS handles page margins easily (see Chapter 7).

  • Don't abuse the < br / > tag. If you grew up using the <br /> tag (<br> in HTML) to insert a line break without creating a new paragraph, then you're in for a treat. (Browsers automaticallyand sometimes infuriatinglyinsert a bit of space between paragraphs, including between headers and <p> tags. In the past, designers used elaborate workarounds to avoid paragraph spacing they didn't want, like replacing a single <p> tag with a bunch of line breaks and using a <font> tag to make the first line of the paragraph look like a headline.) Using CSS's margin controls you can easily set the amount of space you want to see between paragraphs, headers and other block-level elements (see Section 7.2).

As a general rule, adding attributes to tags that set colors, borders, background images, or alignmentincluding attributes that let you format a table's colors, backgrounds, and bordersis pure old-school HTML. So is using alignment properties to position images and center text in paragraphs and table cells . Instead, look to CSS to control text placement (see Section 6.4.1.2), borders (Section 7.3), backgrounds (page145), and image alignment (Section 8.2).

1.2.4. Tips to Guide Your Way

It's always good to have a map for getting the lay of the land. If you're still not sure how to use HTML to create well-structured Web pages, then here are a few tips to get you started:

  • Use only one < h1 > tag per page, and use it to identify the main topic of the page. Think of it as a chapter title: you only put one title per chapter. Using <h1> correctly has the added benefit of helping the page get properly indexed by search engines (see the box in Section 1.2.1).

  • Use headings to indicate the relative importance of text. Again, think outline. When two headings have equal importance in the topic of your page, use the same level header on both. If one is less important or a subtopic of the other, then use the next level header. For example, follow a <h2> with a <h3> tag (see Figure 1-4). In general, it's good to use headings in order and try not to skip heading numbers . For example, don't follow a <h2> tag with a <h5> tag.

    UP TO SPEED
    Validate Your Web Pages

    HTML follows certain rules: For example, the < html> tag wraps around the other tags on a page and the <title> tag needs to appear within the <head> tag. XHTML provides an even more strict set of rules to follow. It's easy to forget these rules, or simply make a typo. Incorrect (or invalid , as the geeks would say) HTML causes problems like making your page look different in different Web browsers. More importantly, you can't create valid CSS with invalid HTML. Fortunately, there are tools for checking whether the HTML in your Web pages is correctly written.

    The easiest way to checkthat is, validate your pages is on the W3C's Web site at http://validator.w3.org/ (see Figure 1-3).

    The W3C, or World Wide Web Consortium, is the organization responsible for determining the standards for many of the technologies and languages of the Web, including HTML, XHTML, and XML.

    If the W3C validator finds any errors in your page, it tells you what those errors are. If you use Firefox, you can download an extension that lets you validate a Web page directly in that browser, without having to visit the W3C site. It can even attempt to fix any problems it encounters. You can get the extension here: http://users.skynet.be/mgueury/mozilla/. A similar tool is available for the Safari browser as well: www.zappatic.net/safaritidy/.


    Figure 1-3. The W3C HTML validator located at http://validator.w3.org lets you quickly make sure the HTML in a page is sound. You can either point the validator to an already existing page on the Web, upload an HTML file from your computer, or just type or paste the HTML of a Web page into a form box and press the submit button to check it.
  • Use the < p > tag for, duh, paragraphs of text.

  • Use unordered lists when you've got a list of several related items, such as navigation links, headlines, or a set of tips like these.

    Figure 1-4. Use the headline tags (<h1>, <h2> and so on) like you would if you were outlining a school report: put them in order of importance, beginning with an <h1> tag, which should shout "Listen up! This is what this whole page is about."
  • Use numbered lists to indicate steps in a process, or define the order of a set of items. The tutorials in this book (see Section 6.6.1) are a good example, as is a list of rankings like "Top 10 Web sites popular with Monks."

  • To create a glossary of terms and their definitions or descriptions, use the < dl > (definition list) tag in conjunction with the < dt > (definition term ) and < dd > (definition description) tags. (For an example of how to use this combo visit www.w3schools.com/tags/tryit.asp?filename=tryhtml_list_definition .)

  • If you want to include a quotation like a snippet of text from another Web site, a movie review, or just some wise saying of your grandfather's, try the < blockquote > tag for long passages, or the < q > tag for one-liners.

  • Take advantage of obscure tags like the < cite > tag for referencing a book title, newspaper article or Web site, and the < address > tag to identify and supply contact information for the author of a page (great for a copyright notice).

  • As explained in full in Section 1.2.3, steer clear of any tag or attribute aimed just at changing the appearance of a text or image. CSS, as you'll see, can do it all.

  • When there just isn't an HTML tag that fits the bill, but you want to identify an element on a page or a bunch of elements on a page so that you can apply a distinctive look, use the < div > and < span > tags (see Section 1.2.1). You'll get more advice on how to use these in later chapters (for example, Section 3.1).

  • Remember to close tags. The opening <p> tag needs its partner in crime (the closing </p> tag), as do all other tags, except the few self-closers like <br /> and <img />.

  • Validate your pages with the W3C validator (see Figure 1-3 and the box in Section 1.2.4). Poorly written or typo-ridden HTML causes many weird browser errors.



CSS[c] The Missing Manual
Dreamweaver CS3: The Missing Manual
ISBN: 0596510438
EAN: 2147483647
Year: 2007
Pages: 154

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net