如何创建具有自动宽度的表格div?

时间:2016-08-03 21:00:21

标签: html css

我试图将DIV HEAD创建为一个表格,但是当我添加另一个头部时,它会向下显示而不是向右显示。

换句话说,我希望表格宽六列,但它只有3,并且行开始堆叠。

这是一个现场演示:



    .rTable {    
     display: block;
     width: 100%;
     font-size: 10px; 
    } 
    
    .rTableHeading, .rTableBody, .rTableFoot, .rTableRow{ 
     clear: both; 
    } 
    
    .rTableHead, .rTableFoot{    
      background-color: #DDD;    
      font-weight: bold;
    }
    
    .rTableCell, .rTableHead {     
      border: 1px solid #999999;
      float: left; 
      height: 17px; 
      overflow: hidden;
      padding: 3px 1.8%; 
      width: 28%;
    } 

   <div class="rTable"> 
      <div class="rTableRow"> 
        <div class="rTableHead">HEAD 1</div> 
        <div class="rTableHead">HEAD 2</div> 
        <div class="rTableHead">HEAD 3</div>  
        <div class="rTableHead">HEAD 4</div> 
        <div class="rTableHead">HEAD 5</div> 
        <div class="rTableHead">HEAD 6</div> 
      </div> 
  
      <div class="rTableRow" style="page-break-before:always;">
        <div class="rTableCell">row 1.1</div>  
        <div class="rTableCell">row 1.2</div>  
        <div class="rTableCell">row 1.3</div>  
        <div class="rTableCell">row 1.4</div>  
        <div class="rTableCell">row 1.5</div>  
        <div class="rTableCell">row 1.6</div>  
      </div> 
  
      <div class="rTableRow" style="page-break-before:always;">
        <div class="rTableCell">row 2.1</div>  
        <div class="rTableCell">row 2.2</div>  
        <div class="rTableCell">row 2.3</div>  
        <div class="rTableCell">row 2.4</div>  
        <div class="rTableCell">row 2.5</div>  
        <div class="rTableCell">row 2.6</div>  
      </div> 

      <div class="rTableRow" style="page-break-before:always;">
        <div class="rTableCell">row 3.1</div>  
        <div class="rTableCell">row 3.2</div>  
        <div class="rTableCell">row 3.3</div>  
        <div class="rTableCell">row 3.4</div>  
        <div class="rTableCell">row 3.5</div>  
        <div class="rTableCell">row 3.6</div>  
      </div>  

    </div>
&#13;
&#13;
&#13;

4 个答案:

答案 0 :(得分:2)

select ip, sqrt(variance(time)) as stdev from logs group by ip

答案 1 :(得分:1)

您的rTableCellrTableHead在CSS中设为width: 28%;。六个并排是168%。

如果你想要适应六个人,你需要做更像16%的事情。

答案 2 :(得分:1)

这是因为你的细胞宽28%。这远远超过100%这是最大值。 10%适用于例如:JSFiddle

优于绝对百分比的计算方法如calc(100% / 6)

请问为什么要按div而不是<table>创建表格?会容易得多。

答案 3 :(得分:1)

一种简单的方法是使用width: calc(100% / 6);(使一列成为父宽度的第6列)和box-sizing: border-box;(忽略填充元素宽度):

&#13;
&#13;
.rTable {    
     display: block;
     width: 100%;
     font-size: 10px; 
    } 
    
    .rTableHeading, .rTableBody, .rTableFoot, .rTableRow{ 
     clear: both; 
    } 
    
    .rTableHead, .rTableFoot{    
      background-color: #DDD;    
      font-weight: bold;
    }
    
    .rTableCell, .rTableHead {     
      border: 1px solid #999999;
      float: left; 
      height: 17px; 
      overflow: hidden;
      padding: 3px 1.8%; 
      width: calc(100% / 6); 
      box-sizing: border-box;
    }
&#13;
<div class="rTable"> 
      <div class="rTableRow"> 
        <div class="rTableHead">HEAD 1</div> 
        <div class="rTableHead">HEAD 2</div> 
        <div class="rTableHead">HEAD 3</div>  
        <div class="rTableHead">HEAD 4</div> 
        <div class="rTableHead">HEAD 5</div> 
        <div class="rTableHead">HEAD 6</div> 
      </div> 
  
      <div class="rTableRow" style="page-break-before:always;">
        <div class="rTableCell">row 1.1</div>  
        <div class="rTableCell">row 1.2</div>  
        <div class="rTableCell">row 1.3</div>  
        <div class="rTableCell">row 1.4</div>  
        <div class="rTableCell">row 1.5</div>  
        <div class="rTableCell">row 1.6</div>  
      </div> 
  
      <div class="rTableRow" style="page-break-before:always;">
        <div class="rTableCell">row 2.1</div>  
        <div class="rTableCell">row 2.2</div>  
        <div class="rTableCell">row 2.3</div>  
        <div class="rTableCell">row 2.4</div>  
        <div class="rTableCell">row 2.5</div>  
        <div class="rTableCell">row 2.6</div>  
      </div> 

      <div class="rTableRow" style="page-break-before:always;">
        <div class="rTableCell">row 3.1</div>  
        <div class="rTableCell">row 3.2</div>  
        <div class="rTableCell">row 3.3</div>  
        <div class="rTableCell">row 3.4</div>  
        <div class="rTableCell">row 3.5</div>  
        <div class="rTableCell">row 3.6</div>  
      </div>  

    </div>
&#13;
&#13;
&#13;