Click here to Skip to main content
Click here to Skip to main content

DIV TABLE

By , 17 Dec 2009
 

Introduction

This article demonstrates using div elements to effectively to build websites without using table elements on a page. Because there are a number of advantages to the div element most clients ask designers to develop websites using div elements rather than table elements. The code below is for beginners learning to use div elements to develop websites.

The following explains why to use DIV over TABLE elements.

Pros of Table Element: Most designers use table for a consistent look. Tables are also easy to maintain. Another advantage of table is that it is compatible with the most browsers.

Cons of Table Element: All this comes with a cost: Too many nested tables increase page size and download time. More table elements push important content down so search spiders are less likely to add content to search engines.

Pros of DIV Element: div with CSS we can achieve the same table based page structure and reduce the number of elements on the page, which allows the page to load faster. It also makes page more compatible with search engine spiders.

Cons of DIV Element: The major drawback of this is not all CSS elements are not browser compatible. Because of this we have to write some custom CSS to resolve issues.

Using the Code

I have created one simple application which shows how easily you can create you own pages with div elements only.

The following diagram demonstrates the layout of div elements.

image001.jpg

DivLayout.JPG

Here is a list of the style sheet classes I have created for the page layout. Let’s get into more detail.

  • divHeaderTable: Class assigned to header of the html page and its works as headertable
  • divHeaderRow: Class assigned to the div element which works as header row of table. As height of header image is fix to 105 row having height element value 105px.
  • divHeaderColumn: Class assigned to div element which works as header column of the table. As header part is divided in three part the width element having value 99%.
  • divTable: Class assigned to the div element woks as the table in container part of html document.
  • divRow: Class assigned to the div element woks as the row in container part of html document.
  • divColumn: Class assigned to the div element woks as the table in container part of html document. As container form element are 4 per row the width element having value 24%.
body
{
            background-color: LightBlue;
            font-family: Verdana;
            font-size: 13px;
}
.divHeaderTable
{
            width: 100%;
            padding-bottom:5px;
            display:block;
}
.divHeaderRow
{
            width: 100%; /* add extra that you want to for header column */
            display:block;
            height:105px;
}
.divHeaderColumn
{
            float: left;
            width: 33%;
            display:block;
}
.divTable
{
            width: 100%;
            display:block;
            padding-top:10px;
            padding-bottom:10px;
            padding-right:10px;
            padding-left:10px; 
}
.divRow
{
         width: 99%; 
         display:block;
         padding-bottom:5px;
}
.divColumn
{
         float: left;
         width: 24%;
         display:block;
}

Following is HTML code of the page which shows how the classes assigned to the elements.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Gmind Solusion-DivElement Layout</title>
    <link href="CSS/StyleSheet.css"  rel="Stylesheet"  type="text/css"   />
</head>
<body>
 
 <div style="width:99%; height:565px;">
    <div class="divHeaderTable">
        <div class="divHeaderRow">
            <div class="divHeaderColumn">
                <img alt="Offshore Outsourcing software development company India"
                    title="Offshore Outsourcing software development company India"
                    src="Images/gmind.png" width="100%" height="105" />
            </div>
            <div class="divHeaderColumn">
                 
            </div>
            <div class="divHeaderColumn">
                <div>
                    <img alt="Gmind" width="15" height="10" src="Images/smllindia.jpg" />
                    +91-9702989270
                </div>
                <div>
                    <img alt="Gmind" src="Images/smallusa.jpg" />
                    +91-9924839208
                </div>
                <div>
                </div>
                <div>
                </div>
            </div>
        </div>
    </div>
    
    <div id="menu" class="divHeaderTable" >
        <ul style="list-style-type:none;" >
            <li style="float:left"><a href="javascript:void(0);" >Home</a>   </li>
            <li style="float:left"><a href="javascript:void(0);" >About Us</a>   </li>
            <li style="float:left"><a href="javascript:void(0);" >Services</a>   </li>
            <li style="float:left"><a href="javascript:void(0);" >Technology</a>  </li>
            <li style="float:left"><a href="javascript:void(0);" >Outsourcing</a>  </li>
            <li style="float:left"><a href="javascript:void(0);" >Quality</a>  </li>
            <li style="float:left"><a href="javascript:void(0);" >Career</a>  </li>
            <li style="float:left"><a href="javascript:void(0);" >Blogs</a>  </li>
            <li style="float:left"><a href="javascript:void(0);" >Partner</a>  </li>
            <li><a href="javascript:void(0);" title="Contact Us">Contact Us</a>  </li>
        </ul>    
    </div>
    
    <div id="divHeader" class="divHeaderTable">
        <h4>User Registration</h4>
    </div>
    
    <div id="content" class="divTable">
       <div class="divRow">
        <div class="divColumn">
            <div>
                <label id="lblSolutation" > Solutation</label>
            </div>
            <div>
                <input type="text" id="txtSolutation" />
            </div>
        </div>
        <div class="divColumn">
            <div>
                <label id="lblFname" > First Name</label>
            </div>
            <div>
                <input type="text" id="Text1" />
            </div>
        </div>
        <div class="divColumn">
            <div>
                <label id="lblMname" > Middle Name</label>
            </div>
            <div>
                <input type="text" id="Text2" />
            </div>
        </div>
        <div class="divColumn">
            <div>
                <label id="lblLname" > Last Name</label>
            </div>
            <div>
                <input type="text" id="Text3" />
            </div>
        </div>
      </div>
      
       <div class="divRow">
       <div class="divColumn">
             <div>
                <label id="Label10" > Login Name</label>
            </div>
            <div>
                <input type="text" id="Text13" />
            </div>
        </div>
        <div class="divColumn">
             <div>
                <label id="Label9" > E-mail</label>
            </div>
            <div>
                <input type="text" id="Text12" />
            </div>
        </div>
        <div class="divColumn">
            <div>
                <label id="Label7" > Password</label>
            </div>
            <div>
                <input type="password" id="Text10" />
            </div>
        </div>
        <div class="divColumn">
            <div>
                <label id="Label8" > Confirm Password</label>
            </div>
            <div>
                <input type="password" id="Text11" />
            </div>
        </div>
      </div>
      
       <div class="divRow">
        <div class="divColumn" style="width:48%;">
            <div>
                <label id="Label1" > Address1</label>
            </div>
            <div>
                <textarea id="Text4" style="width:80%;" ></textarea>
            </div>
        </div>
        <div class="divColumn" style="width:48%;">
            <div>
                <label id="Label3" > Address2</label>
            </div>
            <div>
                <textarea id="Text6" style="width:80%;"> </textarea>
            </div>
        </div>
      </div>
      
       <div class="divRow">
        <div class="divColumn">
            <div>
                <label id="Label2" > State</label>
            </div>
            <div>
                <input type="text" id="Text5" />
            </div>
        </div>
        <div class="divColumn">
            <div>
                <label id="Label4" > City</label>
            </div>
            <div>
                <input type="text" id="Text7" />
            </div>
        </div>
        <div class="divColumn">
            <div>
                <label id="Label5" > Country</label>
            </div>
            <div>
                <input type="text" id="Text8" />
            </div>
        </div>
        <div class="divColumn">
            <div>
                <label id="Label6" > Pin Code</label>
            </div>
            <div>
                <input type="text" id="Text9" />
            </div>
        </div>
      </div>
      
      <br />
      <div class="divRow">
         <input type="submit" value="Submit" onclick="javascript:void(0);" />
          or   
        <a  href="javascript:void(0)">Cancel </a> 
      </div>
    </div>
 </div>   
 <div style="text-align:center; background-color:Green;">
  Contact us to discuss your requirements
 </div>
</body>
</html>

Points of Interest

This not the only structure you can create with div and CSS so you can achieve different layouts with this also.

So from the above example you can see the div layout with CSS is easy and powerful. Because of this web pages download faster, avoid using more tags, and create web pages that are more appealing to search engines.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Pranay Rana
Software Developer (Senior) GMind Solusion
India India
Member

Microsoft C# MVP

 
Hey, I am Pranay Rana, working as a ITA in MNC. Web development in Asp.Net with C# and MS sql server are the experience tools that I have had for the past 5.5 years now.
 
For me def. of programming is : Programming is something that you do once and that get used by multiple for many years
 

You can visit my blog

StackOverFlow - http://stackoverflow.com/users/314488/pranay
My CV :- http://careers.stackoverflow.com/pranayamr
 
Awards:



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionNicememberGajendra Yadav20 Sep '12 - 4:02 
I was looking for this type article...thanks
GeneralMy vote of 5membermanoj kumar choubey27 Mar '12 - 4:11 
Nice
GeneralMy vote of 3memberMember 43208449 Nov '11 - 20:43 
thanks for your effort.
QuestionGreat ArticelmemberJP_Rocks19 Oct '11 - 3:18 
Thumbs Up | :thumbsup:
***** Programme comme si dept soutien technique. est plein de tueurs en série et ils savent adresse de votre domicile. *****

QuestionDoes not work in IE9memberGarry Lowther30 Jul '11 - 4:54 
Just tested in Internet Explorer 9 and the footer does not align to bottom of page.
GeneralMy vote of 5memberRusselSSC27 Jan '11 - 12:13 
nice
I love css. it is very powerful and it makes developers so faster.
GeneralRe: My vote of 5memberPranay Rana17 Mar '11 - 19:55 
thanks for the vote @RusselSSC

Generalgood jobmemberArlen Navasartian19 Jan '10 - 21:28 
take my 5
 
-------
Arlen.N

GeneralRe: good jobmemberPranay Rana20 Dec '10 - 0:37 
its too late but thank you
For any question : http://pranayamr.blogspot.com/
 
vote my article :

Learn SQL to LINQ ( Visual Representation )


Calling WCF Services using jQuery

GeneralGood Articlememberthatraja15 Jan '10 - 18:31 
But you should list the differences between DIV vs Table. You got 4 from me. Smile | :)
GeneralRe: Good ArticlememberPranay Rana20 Dec '10 - 0:36 
thanks for this
For any question : http://pranayamr.blogspot.com/
 
vote my article :

Learn SQL to LINQ ( Visual Representation )


Calling WCF Services using jQuery

Generalmy vote of 4memberNightJammer26 Dec '09 - 4:42 
vote of 1 is way to hypocritical, as He did mention that this was for beginners. So far I have found this of use as I'm the type to try to stay away from tables unles must use.
 
Thank you for creating a good article to work with.
 
/////////////////
Groucho Marx
Those are my principals, if you don't like them… I have others.

GeneralRe: my vote of 4memberPranay Rana20 Dec '10 - 0:36 
thanks man
For any question : http://pranayamr.blogspot.com/
 
vote my article :

Learn SQL to LINQ ( Visual Representation )


Calling WCF Services using jQuery

GeneralThe Reason for DIV vs TABLEmember.hmcclungiii20 Dec '09 - 9:47 
The rule to remember always is that DIV's should be used for layout only, and tables for data only. I understand your attempt at DIV only markup, but it should be pointed out, especially in light of the other comments, that the following DIVS would be appropriate:
 
divHeaderTable
divHeaderRow
divHeaderColumn
divTable
divFooter
 
As they are specifically layout related elements of the page. However, the
 
divRow, and
divColumn
 
DIVs are not appropriate, because they are obviously used to display data, and TABLE tags could be used with the appropriate CSS to achieve the same result.
 
This would also be better for spider and increase page load time.
GeneralI know div Renders fastermemberAbhishek Sur18 Dec '09 - 12:14 
Well... Its true, most of the browsers renders div faster than Tables, but does it mean you will put so much extra html and css which reduces the simplicity of writing html...
 
I think I am better of using Table rather than this. Or rather use Ul/LI tables(CSS tables) when cells are of fixed size.. Smile | :)
 
Also most of the pages places Relative Controls. Your one using floating divs...
 
Abhishek Sur
Don't forget to click "Good Answer" if you like this Solution.
My Latest Articles-->

Simplify Code Using NDepend
Basics of Bing Search API using .NET
Microsoft Bing MAP using Javascript

GeneralMy vote of 1memberJames Luterek18 Dec '09 - 5:44 
Tables are not easier to maintain. Your pros and cons list is off base. Also you used divs to make a table, this is one instance where a table is appropriate.
GeneralRe: My vote of 1memberPranay Rana18 Dec '09 - 6:34 
sir it's not fully functional code it's just demo for the beginner people and i think this is best way to explain.
GeneralRe: My vote of 1memberJLuterek19 Dec '09 - 2:37 
I think if you rewrote your tutorial using examples like a two column or three column layout it would receive a better response.
 

GeneralRe: My vote of 1memberDr. Song Li19 Jun '11 - 14:08 
div vs. table is an argument that never ending. both will co-exist for a long time. I would not use a conclusion that is still so arguable to give people a "1" vote. It seems that we still should support "freedom in speech".
GeneralMy vote of 1memberSmirkinGherkin17 Dec '09 - 22:13 
Your cons for the table all apply to what you produce with the divs.
 
The disadvantages that you mention for tables boils down to it makes the page size larger. However, since you need to use div instead of td, tr or th (an extra two bytes over the opening and closing tag) and then include your classes (many more bytes) the HTML produced will be larger again.
 
A major disadvantage to tables is that some browsers - IE in particular will not display the table until all of its content has been parsed and the required space can be calculated. This means that when large, nested tables are used for an entire page that you have a noticeable lag before anything is shown on the page.
 
I appreciate that you have attempted to make a direct conversion from layout tables to a tableless design - but it kind of defeats the point. You are producing a lot of superfluous HTML which could be removed if the CSS was improved. Not as generic but would produce much smaller pages.
GeneralRe: My vote of 1memberPranay Rana17 Dec '09 - 22:25 
Thanks for the reply. its something added in my knowledge.
GeneralRe: My vote of 1memberMauro Leggieri18 Dec '09 - 2:27 
Also I never could make a div table to work in IE if using percentage widths.
 
It I do a div with a width of NNN%, always div size is adjusted to its contents.
 
Best regards,
Mauro.
GeneralThanks !memberRajesh Pillai17 Dec '09 - 22:06 
This is useful to me and very timely as I had a similar requirement for one of my project.
Thanks for sharing..
 
I highly recommend you have a look at the 1KB grid project at http://www.1kbgrid.com/.
 
Would love to see more examples of complex form layout with this approach...
 
Enjoy Life,
Rajesh Pillai
http://geekswithblogs.net/rajeshpillai
 


 

GeneralMy vote of 1memberSteven Berkovitz17 Dec '09 - 12:03 
Just because you can doesn't mean you should.
GeneralRe: My vote of 1memberPranay Rana17 Dec '09 - 18:04 
I just want to demostrate the use of div nothing else.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 17 Dec 2009
Article Copyright 2009 by Pranay Rana
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid