0

I'm creating a table with divs. I'm able to create it, but there is this instance where in there is a row where I need to do a merge and center.

Here is my code.

.divTable {
  display: table;
  width: 100%;
}

.divTableRow {
  display: table-row;
}

.divTableCell {
  border: 1px solid #999999;
  display: table-cell;
  padding: 3px 10px;
}

.divTableBody {
  display: table-row-group;
}
<div class="divTable">
  <div class="divTableBody">
    <div class="divTableRow">
      <div class="divTableCell">$2</div>
      <div class="divTableCell">1</div>
    </div>
    <div class="divTableRow">
      <div class="divTableCell">&nbsp;6</div>
    </div>
  </div>
</div>

Here I want 6 to be in center. How can I achieve this?

basically like below table

<table border="1px solid black">
  <tr>
    <td>$2</td>
    <td>1</td>
  </tr>
  <tr>
    <td colspan="2" align="center">6</td>
  </tr>
</table>
user3872094
  • 3,269
  • 8
  • 33
  • 71

3 Answers3

1

If this cell is going to be at the bottom then you can go for table-caption like this

<div class="divTable">
  <div class="divTableBody">
    <div class="divTableRow">
      <div class="divTableCell">$2</div>
      <div class="divTableCell">1</div>
    </div>
  </div>
  <div class="divTableCaption">
    6
  </div>
</div>

css

.divTable {
  display: table;
  width: 100%;
}
.divTableRow {
  display: table-row;
}
.divTableCell {
  border: 1px solid #999999;
  display: table-cell;
  padding: 3px 10px;
}
.divTableBody {
  display: table-row-group;
}
.divTableCaption { /* <== Only added this */
  display: table-caption;
  text-align: center;
  border: 1px solid #999999;
  caption-side: bottom;
}
0

There is no colspan in CSS. (html-colspan-in-css)

A solution

.tr {
  width: 100%;
}

.tr:after {
  content: " ";
  display: table;
  clear: both;
}

.td {
  border: 1px solid;
  float: left;
  box-sizing: border-box;
  padding: 3px 10px;
}

.col-6 {
  width: 50%;
}

.col-12 {
  width: 100%;
}

.text-center {
  text-align: center;
}
<div class="table">
  <div class="tbody">
    <div class="tr">
      <div class="td col-6">$2</div>
      <div class="td col-6">1</div>
    </div>
    <div class="tr">
      <div class="td col-12 text-center">6</div>
    </div>
  </div>
</div>

Bootstraps' grid layout provides that.

Bootstrap solution

.bordered>div[class^=col-] {
  border: 1px solid;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<div class="container">
  <div class="row bordered">
    <div class="col-xs-6">A</div>
    <div class="col-xs-6">B</div>
  </div>
  <div class="row bordered">
    <div class="col-xs-12 text-center">C</div>
  </div>
</div>
-1

Try this

.divTableRow{
width:100%;
display:flex;
justify-content:center;
}
DPS
  • 943
  • 11
  • 22