Jacob Frye Jacob Frye - 4 months ago 18
CSS Question

How can I create a table div with auto width?

I'm trying to create DIV HEAD as a table, but when I add another head it shows down and not to the right.

In other words, I want the table to be six columns wide, but it is only 3, and the rows start to stack.

Here is a live demo:



.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>




Answer

An easy way is to use width: calc(100% / 6); (make one column one 6th of the parent width) and box-sizing: border-box; (ignore padding for element width):

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

Comments