How to style my unordered list like a table?

后端 未结 3 434
余生分开走
余生分开走 2021-01-05 04:19

I have ONLY one

    and under that we have group of
  •  
    • 1
    • 2
3条回答
  •  心在旅途
    2021-01-05 04:47

    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%;
    }
    

提交回复
热议问题