Search code examples
htmlcsscss-tables

Table grid using Div in HTML and CSS


I want to create a table grid using DIV (HTML & CSS only). I almost got into and still got some issues. I attached the sample image. I want the grid should be the same like this sample image. I attached the fiddle of what I created so far. Could you help somebody that what am doing and how can I improve to finish the table as same as the image?

HTML:

<div class="containerDiv">

  <div class="rowDivHeader">
    <div class="cellDivHeader">Recommendation</div>
    <div class="cellDivHeader">Typical savings</div>
    <div class="cellDivHeader">Improved SAP</div>
    <div class="cellDivHeader">Improved EI</div>
    <div class="cellDivHeader">Indicative cost</div>
    <div class="cellDivHeader">Include</div>
    <div class="cellDivHeader lastCell">Removal Reason</div>
  </div>

  <div class="rowDiv">
    <div class="cellDiv">Room-in-roof-insulation</div>
    <div class="cellDiv">93.0</div>
    <div class="cellDiv">F : 29</div>
    <div class="cellDiv">B : 89</div>
    <div class="cellDiv">£1,500 - £2,700</div>
    <div class="cellDiv">Checkbox</div>
    <div class="cellDiv lastCell">Textbox</div>
  </div>
</div>

CSS:

.containerDiv {
  border: 1px solid #3697f6;
  width: 100%;
}

.rowDivHeader {
  border: 1px solid #668db6;
  background-color: #336799;
  color: white;
  font-weight: bold;
}

.rowDiv {
  border: 1px solid #668db6;
  background-color: #cee6fe;
}

.cellDivHeader {
  border-right: 1px solid white;
  display: table-cell;
  width:12%;
  padding: 1px;
  text-align: center;
}

.cellDiv {
  border-right: 2px solid white;
  display: table-cell;
  width:10%;
  padding-right: 4px;
  text-align: center;
  border-bottom: none;
}

.lastCell {
  border-right: none;
}

sample image

Sample Table Grid Image


Solution

  • Add display:table-row to the row div i.e .rowDivHeader & .rowDiv

    & display:table to the main div .containerDiv

    .containerDiv {
      border: 1px solid #3697f6;
      width: 100%; display:table
    }    
    .rowDivHeader {
          border: 1px solid #668db6;
          background-color: #336799;
          color: white;
          font-weight: bold; display:table-row
        }
        .rowDiv {
          border: 1px solid #668db6;
          background-color: #cee6fe;
          display:table-row
        }
    

    DEMO