Mywebdunia's Blog

Create a table using CSS

Posted on: April 9, 2009


Few days back my colleague was developing a website using Div’s. It was very difficult for him to create a structure of table using div’s. So he asked me for look for some way to develop such a structure like a table. 

Here is how we can do it, its so simple and easy to understand. Down below are the html and css for the same

HTML code :

<html>

<head>

<title>Creating a table using CSS</title>

</head>

<body>

<h1>Test Rankings</h1>

<div class=”table”>

<ul>

<li class=”title”>Rank</li>

<li class=”even”>1</li>

<li class=”odd”>2</li>

<li class=”even”>3</li>

<li class=”odd”>4</li>

<li class=”even”>5</li>

</ul>

<ul>

<li class=”title”>Team</li>

<li class=”even”>Australia</li>

<li class=”odd”>South Africa</li>

<li class=”even”>India</li>

<li class=”odd”>Srilanka</li>

<li class=”even”>Pakistan</li>

</ul>

<ul>

<li class=”title”> Points</li>

<li class=”even”>5361</li>

<li class=”odd”>5360</li>

<li class=”even”>5498</li>

<li class=”odd”>3680</li>

<li class=”even”>2197</li>

</ul>

<ul>

<li class=”title”>Rating</li>

<li class=”even”>128</li>

<li class=”odd”>119</li>

<li class=”even”>117</li>

<li class=”odd”>108</li>

<li class=”even”>100</li>

</ul>

</div>

</body>

</html>

CSS code: 

<style type=”text/css”>

.table

{

background:#333;

font-family:Verdana, Arial, Helvetica, sans-serif;

font-size:12px;

}

.table ul

{

float:left;

margin:0;

padding:0;

border:1px solid #C9C9C9;

}

.table ul li

{

list-style:none;

padding:5px 10px;

}

.table ul li.title

{

font-weight:bold;

background:#333;

color:#fff;

}

.table ul li.even

{

background:#fff

}

.table ul li.odd

{

background:#D7EDFB

}

h1{

font-size:16px;

color:#227786;

}

</style>

16 Responses to "Create a table using CSS"

Why not just use a table tag?

Its an another option, instead of using table tr, td’s blah blah… just use div’s or ul’s and CSS..

Another option is to use floating divs in the very same manner you used the UL’s, and with divs you’re open to use all sorts of extra parameters. This tutorial isn’t very useful at all, in that it’s an easier way out of the TRUE div-table css method.

Ya floating div’s can also be used, but when you use div’s you need to specify the width and height, all these extra parameters are required. But with ul’s you dont.

Have you ever tested this code?

Ya I have tried it and written this blog.

its doesnt work with me, am i miss something?

already put css code in too

Just check with the double cotes(“).. replace all double cotes with the keyboard double cotes one and try and let me know if its working or not..

it works!!

its just because I forgot to include the CSS file

my bad T_T

i m not getting a table .

I have pasted the above code in notepad and saved it. When i open in IE an FF i am not getting the table

Can you please help me .

did u put in the CSS, if yes just check with the double cotes(“) there might be problem with this.

Just delete the double cotes and add it from ur keyboard in the same places.

If this also did not work tell me i will check it and try to sort it out.

When i type some very big string in one of the column, the other columns are displaying as a new row. This scenario is not going to happen always. But still if you could give me a suggestion to wrap the text when the entire table is fulfilling the width of the screen. (Just like Table tag, if we have some bigger txt, it will wrap automatically)

Thanks
Sathish

Well in that case then you can use word-wrap: break-word;. Try with this and see..

hi!, nice tips

how to add hover effect entire row?

This works great. . just what I was looking for. . .I plan to mix in a sprinkle of jQuery and this is perfect! Thanks!

Leave a comment

Categories