How to style my unordered list like a table?

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

I have ONLY one

    and under that we have group of
  •  
    • 1
    • 2
相关标签:
3条回答
  • 2021-01-05 04:32

    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.

    0 讨论(0)
  • 2021-01-05 04:34

    You cannot convert a single list (containing more than 2 items) into 2 columns via the display: table properties because you need some element to act as the table-row. Without an element acting as a table-row, all adjacent elements that are set to display: table-cell will be contained within an anonymous table-row element that cannot be modified or styled in any way.

    Your only option is to either change the markup (to use tables or lists of lists) or use a different approach to your CSS: either floats/inline-block on the lis or using the columns property on the ul.

    0 讨论(0)
  • 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%;
    }
    
    0 讨论(0)
提交回复
热议问题