Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to style my unordered list like a table?

Tags:

html

css

I have ONLY one <UL> and under that we have group of <LI>

 <ul>
   <li>1<li>
   <li>2<li>

   <li>3</li>
   <li>4<li>

 </ul>

now I wanted to show them as TABLE, please help me with CSS, how can we show as a TABLE for above UL/LI in below table format, 2 LI set in one TR (two TD) and so on....

enter image description here

like image 922
user584018 Avatar asked Dec 11 '12 17:12

user584018


2 Answers

Well, here's one possible solution:

ul {
    width: 450px;           /* change it to whatever you like */
    position: relative;

    /* these should be probably already set up by `reset.css` */ 
    list-style-type: none;
    margin: 0;
    padding: 0;
}

ul:before, ul:after {
    text-align: center;
    display: block;
    border: 1px solid black;
    border-bottom: 0;
    width: 48%;
}

ul:before {
    content: 'col1';
    border-right: 0;    
}

ul:after {
    content: 'col2';
    position: absolute;
    top: 0;
    left: 48%;
    margin-left: 1px;    
}

li {
    text-align: right;
    width: 48%;
    float: left;
    border: 1px solid black;
    margin-bottom: -1px;
}

li:nth-child(even) {
    margin-left: -1px;
}

It works (JSFiddle; tested in Chrome, Firefox and Opera; nth-child(even) selector obviously fails in IE8, so you have to emulate it with class or other means; but otherwise it's still solid), but I admit I feel guilty about this. )

P.S. If you want to add padding to the "cell" contents, don't forget to change their widths as well, like here:

li {
    width: 47%;
    padding-right: 1%;
}
like image 60
raina77ow Avatar answered Oct 07 '22 21:10

raina77ow


It's a really late answer, but I think this is a common topic. Here's a codepen I made.

Obviously it's just a starting point. It also has some example of how to add styles like bg or borders. If the 'cells' contain some arbitrary content, you'll have to adjust dimensions, for example. I use this kind of code for thumbnails galleries, for example, where you don't have to worry about borders or bgs and it's quite elementary code (the example is for a 2x3 table, see codepen):

ul{
  list-style:none;
}

ul li{
  float:left;
  padding:10px;
  border-bottom:1px solid #000;
  border-right:1px solid #000;
}

ul li:nth-child(3n){
  background-color:#888;
}

ul li:nth-child(3n+1){
  clear:both;
  border-left:1px solid #000;
  background-color:#ccc;
}

ul li:nth-child(-n+3){
  border-top:1px solid #000;
}

Hope it helps.

like image 40
Luca Reghellin Avatar answered Oct 07 '22 20:10

Luca Reghellin