One of the tricky parts of HTML is getting things into the right positions beside each other. Floating left or right is great for single items. But sometimes there are more parts to position or the exact position is really important or you want to overlap objects, which cannot be done with a table.
Problem: How to put things side by side on the page (and make sure they stay there)?
Solution 1- Old HTML Method: Put them in a table.
Unlike a data table, the parts of a positioning table do not have any logical relationship to each other. It's just a trick (a hack) to keep things in position on the page. You still have the issue with table cells resizing. These are reasons why this method is no longer recommended. (But it is still widely used!)
Solution 2 - New CSS Method: Position parts with CSS positioning, combining DIV and floating and relative or absolute positioning.
You actually have a lot more control over the position of an object on your web page with CSS. It just takes a bit of practice. There are a few situations where a table is still a good choice. But, all major web sites currently use CSS positioning for arranging the parts of their web pages.
Using a table to get parts of the page side by side was the only reasonable method to use, back in the olden days, before CSS was widely understood by browsers.
This is an example of a hack, an awkward but effective work-around to accomplish a task. Web authors may resort to a hack to make a page display correctly in a particular browser version. They sometimes take advantage of the way the old browser is broken to make it do what they want. It might be a cool trick, but you need to avoid doing this whenever possible. When the browser is updated, the hack might not work as expected any more!
This is a long paragraph that has an image at the right. We want the image to have a caption below it and to have this text wrap on the left of the image and the caption. A table is an easy way to do this. | ![]() Win 7 monitor |
If you put the text in the cell on the left and the caption below the image in the cell on the right, then it will look like the text wraps around them both.
This example table has the border turned on so you can see where it is. You would usually want the border off for a positioning table on an actual page.
There are a couple of ways to use CSS to position something on your web page. DIV tags can be used to group one or more other elements together for positioning.
The FLOAT property works for any element, not just images. You can float left or right to make text wrap around whatever you floated. The element must have a width set!
The example below uses two DIV tags. One DIV tag contains the image and its caption. It has a lavender background and is floated right.
The outside DIV tag (which has a border) holds the paragraph and the floating DIV with the image and caption. Again, the border and background color are there only to help you see where each DIV area is.
This is a long paragraph that has an image at the right. We want the image to have a caption below it and have this text wrap on the left of the image and the caption.
This method is more complex but offers more precise control and more options for effects than FLOAT does. Web authors can wander far, far away from the flexibility of HTML by using CSS to create exact placements. They may not consider the effect on various size screens. CSS positioned objects may not wrap as the window size changes.
Using CSS positioning requires that you set the type of position and one or two of the properties LEFT, RIGHT, BOTTOM, TOP. LEFT and TOP are the most common choices. If you pick a position type but do not set a position, you may not see any change at all. Widths and heights for the container and the elements being positioned are usually needed, too.
Distances are most commonly given in pixels or percentages, though there are some other choices. Negative numbers are allowed! This is useful when you want parts to overlap.
This is a long paragraph that has an image at the right and we want the image to have a caption below it and have this text wrap on the left of the image plus the caption.
The border and background color are set so you can see where each DIV lies on the page. The margin-left property scoots the DIV over a bit on this lesson page.
DIV Height: The outside DIV needed a HEIGHT as well as a WIDTH. Otherwise, there will be empty
space below the contents, where the parts would have been if we had not positioned them. Not good!
More examples of
various CSS POSITION values
(Opens in a new window/tab. The window
may need to be resized to see text covered by the rectangle for absolute
positioning.)
Disclaimer: The old version of these lessons uses tables for positioning, which is no longer recommended. When I started writing, CSS positioning was not handled the same way in different browsers. In particular, Netscape was quite popular then and used an entirely different method than other browsers used. So I opted to use the method that worked in all browsers - positioning tables. My new layout for these lessons is based mostly on CSS.
This is another case of Do as I say (and do now), not as I did!