How to use divs instead of tables

前端 未结 5 1300
被撕碎了的回忆
被撕碎了的回忆 2021-02-04 04:25

I am trying to create the following table layout, but I want to use DIV instead of TABLE:

------------------
|       |        |
| CELL1 |  CELL2 |
|       |              


        
相关标签:
5条回答
  • 2021-02-04 04:30

    You need inline-block and float: here's the jsFiddle

    .list-row {
        background:#f4f4f4;
        display:inline-block;
        border:2px solid red;}
    
    .list-left {
        width:auto;
        padding:10px;
        float:left;
        border: 2px solid blue;}
    
    .list-right {
        padding:10px;
        float:right;
        border:2px solid green;}
    

    Also, since you're not using relative or absolute positioning, you don't need to specify top and left.

    0 讨论(0)
  • 2021-02-04 04:34
    <style>
    .grid-container {
       display: grid;
    } 
    </style>
    <div class="grid-container">
       <div class="grid-item">1</div>
       <div class="grid-item">2</div>
       <div class="grid-item">3</div>
       <div class="grid-item">4</div>
       <div class="grid-item">5</div>
       <div class="grid-item">6</div>
       <div class="grid-item">7</div>
       <div class="grid-item">8</div>
       <div class="grid-item">9</div>
    </div> 
    

    use display grid for using div instead of table

    0 讨论(0)
  • 2021-02-04 04:43

    Try the following: (working jsFiddle)

    HTML:

    <div class="container">
        <div class="cell" id="cell1"></div>
        <div class="cell" id="cell2"></div>
        <div class="cell" id="cell3"></div>
    </div>
    

    CSS:

    .container{overflow:hidden; width:100%;}
    .cell{width:50%; float:right;}
    #cell1{float:left;}
    

    Your approach (which places the divs in rows) is not a good choice in this case.. mine separates them by columns.

    0 讨论(0)
  • 2021-02-04 04:47

    You could use display:inline-block instead of float. Just set widths of about 50% (adjust depending on padding, margins and borders) for the left and right containers and make them inline-block.

    Here is my jsFiddle

    0 讨论(0)
  • 2021-02-04 04:47

    rowspan is not avalaible for display:table,
    but you can still use it to get close to what you are looking for.
    http://codepen.io/anon/pen/kqDab
    display:inline-table used for the show , so they stand aside each others. you can turn it back to display:table.

    Options i see here is to set an height to parent container to have height:XX% avalaible for direct childs element (if it is: float, inline-block, table ...) . Other option is vertical-align middle for the cell if display:table-cell;.

    You HTML with the same CSS of first demo : http://codepen.io/anon/pen/dvlsG


    edit display:flex is also a good option nowdays :http://codepen.io/anon/pen/aBjqXJ

    0 讨论(0)
提交回复
热议问题