HTML page layout without tables

Tables are pretty effective as a page layout tool but they may be undesirable under certain circumstances. In my case, blog entries that used table based layouts (e.g. a two column article with a pic on the left and text on the right) would misrender in the blog entries listing page as part of their html tags would be cut off by the blog's backend. I needed something that would render correctly even if the closing tags were missing. Enter CSS based layout.

There are various techniques to do CSS page layout but the one I've found works the best is the "float" style based one. Here is code that makes for a nice two column layout.

<style>
	#colLeft {
		float:left;
		width:320px;
                padding:10px;
                margin:5px;
                border:solid green 1px;
        }

	#colRight {
                padding:10px;
                margin:5px;
                border:solid green 1px;
        }

        #colContainer {
                height:540px;
                padding:10px;
                margin:5px;
                border:solid red 1px;
        }
</style>

<div id="colContainer">

<span id="colLeft">
     <img align="bottom" src="/files/gal_oly-hoops.jpg">
</span>
<span id="colRight">
     some text here
</span>

</div>

It is pretty simple really: two spans within a container div with the span on the left having a style of "float:left". Also note that the left span's width style as well as the container's height are set to specfic numeric px values. This is done here in order to tailor the layout around the image's specific size; however, for two text only columns we could have used a percentages as needed. The rest of the used styles are not crucial and may be changed to whatever you prefer.

Here is what the code renders to when placed inside a blue-border parent div:





some text here

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Side-by-side DIVs

In case you simply want to position two DIVs side by side without using CSS positioning (e.g. position:absolute), a very simple way to do it is to override DIV's default style of display:block Here's the results of just that:

All you need is two DIVs, each one containing one photo and these two styles: display:inline;float:none; Here's the html:

<div style="display:inline;float:none;">

    <a href="/files/profile2-8x6_1.jpg" title="" rel="thumbnail"><img border="0" src="/files/profile2_28.png" width="485" height="473" /></a>

</div>

 

<div style="display:inline;float:none;">

    <a href="/files/joselita-8x6.jpg" title="" rel="thumbnail"><img border="0" src="/files/joselita_11.png" width="524" height="479" /></a>

</div>

Post new comment

The content of this field is kept private and will not be shown publicly.

Share

  submit to reddit