[SOLVED] I’m having trouble centering a cell of an HTML table


This Content is from Stack Overflow. Question asked by Flávio Tiezzi

I’m generating an HTML table from an array of objects. But I need to center the last signature when it goes to the left.

My current code:

var result = [

                  {name: "John",
          jobPosition: "President"

                  {name: "Marc",
          jobPosition: "Director"

                    {name: "Paul",
          jobPosition: "Director"

                    {name: "Mary",
          jobPosition: "Director"

                    {name: "Carl",
          jobPosition: "Geral Secretary"

var table = '<table style=" height: 94px;" border="0" width="100%" cellspacing="0" cellpadding="0"><tbody>';
    result.forEach(function(item, index) {
        //par abro o tr
        if (index % 2 == 0) {
            table += '<tr style="height: 35px;">';
        table += '<td style="width: 50%; height: 150px; text-align: center; font-family: arial; font-size: 12pt;" valign="top"><p>_____________________________</p><p>' + item.name + '</p><p>' + item.jobPosition + '</p></td>';
        //impar ou ultimo item fecho tr
        if (index % 2 == 1 || index == (result.length - 1)) {
            table += '</tr>';
    table += '</tbody></table>';

In this example, Carl’s signature should be centered. I’ve tried a few things, but unfortunately nothing works. Does anyone have any suggestions?

Thanks in advance.


excuse me sir. now i found out your desire.

you should add colspan attribute to te last td if index % 2 == 1
and set the amount of this colspan to 2

This Question was asked in StackOverflow by Flávio Tiezzi and Answered by hata.IR It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?