Sams Teach Yourself HTML 4 in 24 Hours
- Table of Contents
- Copyright
- About the Author
- Acknowledgments
- Tell Us What You Think!
- Put Your HTML Page Online Today
- I. Your First Web Page
- Hour 1. Understanding HTML and XML
- Hour 2. Create a Web Page Right Now
- Hour 3. Linking to Other Web Pages
- Hour 4. Publishing Your HTML Pages
- II. Web Page Text
- Hour 5. Text Alignment and Lists
- Hour 6. Text Formatting and Font Control
- Hour 7. Email Links and Links Within a Page
- Hour 8. Creating HTML Forms
- III. Web Page Graphics
- Hour 9. Creating Your Own Web Page Graphics
- Hour 10. Putting Graphics on a Web Page
- Hour 11. Custom Backgrounds and Colors
- Hour 12. Creating Animated Graphics
- IV. Web Page Design
- Hour 13. Page Design and Layout
- Hour 14. Graphical Links and Imagemaps
- Hour 15. Advanced Layout with Tables
- Hour 16. Using Style Sheets
- V. Dynamic Web Pages
- Hour 17. Embedding Multimedia in Web Pages
- Hour 18. Interactive Pages with Applets and ActiveX
- Hour 19. Web Page Scripting for Non-Programmers
- Hour 20. Setting Pages in Motion with Dynamic HTML
- VI. Building a Web Site
- Hour 21. Multipage Layout with Frames
- Hour 22. Organizing and Managing a Web Site
- Hour 23. Helping People Find Your Web Pages
- Hour 24. Planning for the Future of HTML
- VII. Appendixes
- A. Readers' Most Frequently Asked Questions
- B. HTML Learning Resources on the Internet
- C. Complete HTML 4 Quick Reference
- D. HTML Character Entities
Positioning and Layers
Here's the bad news: Neither the official HTML 4 standard nor the official cascading style sheets, level 1 (CSS1) standard gives you any direct way to control the exact position of the images and text on your Web pages. The closest thing you can do is adjust the margins of your page. For example, the following HTML would position the first image on the page exactly 50 pixels to the left and 40 pixels down from the top-left corner of the browser window. It would then indent the text exactly 60 pixels from the left edge of the browser window.
<body> <span style="margin-left: 50px; margin-top: 40px"> <img src="pretty.gif" /></span> <span style="margin-left: 60px"> This is a pretty picture.</span>
A lot of Web page designers have prayed to the HTML gods for the power to be more direct about where they want an image to be. Overlapping layers of images and text are high on a lot of wish lists, too. Unfortunately, there is no officially sanctioned way to say, "Put this image at pixel position 100,50 and this text at 200,52."
The good news is that the new cascading style sheets, level 2 (CSS2) standard includes positioning, and both Netscape Navigator and Microsoft Internet Explorer already started conforming to part of that standard as of version 4. It's a simple, elegant extension to the CSS1 style sheet standard, so you'll find it quite easy to use.
The following line of HTML shows how to position an image with the top-left corner exactly 50 pixels from the left and 40 pixels down from the top edge of the browser window. Although I use a <span> tag here, you can include a similar style attribute in almost any HTML tag to position some text precisely where you want it.
<span style="position: absolute; left: 50px; top: 40px"> <img src="pretty.gif" /></span>
Along with left: and top: positioning, you can also optionally include width: and height: for text blocks.
Figure 16.6 is a simple HTML document with style-based positioning included. You can see the resulting Web page, as displayed by Microsoft Internet Explorer 5, in Figure 16.7. The page would look exactly the same in Microsoft Internet Explorer 4 and Netscape Navigator version 4 or later.
Figure 16.6 This HTML page uses inline styles to precisely position images and text.
Figure 16.7 Microsoft Internet Explorer 5 displays the HTML from Figure 16.6.
Notice that elements are drawn onto the page in the order that they occur in the HTML document. For example, the white streak appears to be layered on top of the logo, but is underneath the body text, because of the order they were drawn.
Although exact positioning control is a great idea, always remember that anyone using Netscape Navigator version 3 or earlier, Microsoft Internet Explorer version 3 or earlier, or almost any other Web browser will not see any of your fancy positioning. The HTML from Figure 16.6, for example, looks like Figure 16.8 when displayed in Netscape Navigator 3.
Figure 16.8 The HTML from Figure 16.6 doesn't look very artistic in Netscape Navigator 3 because none of the positioning shows up.
Also remember that changes in font or window size can easily destroy the appearance of your carefully positioned elements. As with all style sheet stuff, I strongly recommend that you use style-based positioning for fine tuning only—at least until most of your intended audience has moved up to at least version 4 browsers.
Summary | Next Section

Account Sign In
View your cart