4

I have a table. The table is generated by some server side code which I can't change but I can add some CSS to style.

<table>
<tbody>
<tr>
 <td>row 1 column 1</td>
 <td>row 1 column 2</td>

</tr>
    </tbody>
</table>

Is there a way of merging the two columns using CSS and not JQuery

  |row1 column1 |row 1 column 2 |
  |             |               |

get an output like

| row 1 column2  |
|                |  
user1339913
  • 1,017
  • 3
  • 15
  • 36
  • 1
    Can you show an example output with respect to the HTML used? – m4n0 Jun 16 '15 at 15:21
  • 1
    You might be looking for the colspan attribute? http://www.w3schools.com/tags/att_td_colspan.asp – Jay Jun 16 '15 at 15:23
  • This does not make much sense... Just write everything inside the same `td` – LcSalazar Jun 16 '15 at 15:23
  • maybe http://stackoverflow.com/questions/2403990/html-colspan-in-css – Dmitriy Jun 16 '15 at 15:25
  • as your example shows u wnat to show the 2 columns as one but without any style it is difficult to distinguish between columns. http://jsfiddle.net/q8mne8ts/ what do u want to do exactly – lakshya_arora Jun 16 '15 at 17:20

2 Answers2

5

This isn't possible within CSS, even when you say they're block level elements.

They are still two separate elements and HTML will render them as such.

As shown below:

td {
  border: 1px solid black;
}
.block * {
  display: block;
}
.border-collapse {
  border-collapse: collapse;
  margin: 0;
}
<table>
  <tbody>
    <tr>
      <td>row 1 column 1</td>
      <td>row 1 column 2</td>

    </tr>
  </tbody>
</table>

<table class="block">
  <tbody>
    <tr>
      <td>row 1 column 1</td>
      <td>row 1 column 2</td>

    </tr>
  </tbody>
</table>

<table class="border-collapse">
  <tbody>
    <tr>
      <td>row 1 column 1</td>
      <td>row 1 column 2</td>

    </tr>
  </tbody>
</table>

I believe you are going to have to try and change the html, use the colspan attribute:

<table>
  <tbody>
    <tr>
      <td colspan="3">Here I need a cell by all width</td>
    </tr>
    <tr>
      <td>TD 1</td>
      <td>TD 2</td>
      <td>TD 3</td>
    </tr>
  </tbody>
</table>

The colspan attribute defines the number of columns a cell should span.

There is a related attribute rowspan that achieves the same for rows.

One last dirty option is to always set one of the elements to display: none and the other width: 100%

table {
  width: 400px;
}
table tr td {
  border: 1px solid black;
}

table.td_hide tr td:first-child {
  display: none;
}

table.td_hide tr td:last-child {
  width: 100%;
}
<table>
  <tr>
    <td>Row One</td>
    <td>Row Two</td>
  </tr>
</table>

<table class="td_hide">
  <tr>
    <td>Row One</td>
    <td>Row Two</td>
  </tr>
</table>
Stewartside
  • 20,378
  • 12
  • 60
  • 81
  • The solution works for me if I set the colspan for the second column I.e. colspan:2 and set the display:none for the first column . Is there a way to add a way to set the colspan using CSS using the content attribute. – user1339913 Jun 17 '15 at 14:53
  • Sadly colspan is a html only element. I have come up with another alternative if you look at the bottom of my answer. – Stewartside Jun 17 '15 at 14:59
1

Try to display the elements as block elements like this:

table, tbody, tr, td {
    display: block;
}
Jackson
  • 3,476
  • 1
  • 19
  • 29