Search code examples
javascripthtmlcsshtml-tablealignment

HTML/CSS/Javascript - Align Text In Table


I am trying to align the text in my table. It currently looks like this:

I would like to align the tile (in bold) in the center. Also, I am trying to get the values on the right, moved to the left.

Currently I have:

Javascript:

var table = $('<table/>',
    {
        id: 'table',
        "class":'tables'

    });

// ************************ Row 1 ************************************
row1 = $('<tr></tr>');
var head = $('<th></th>').addClass('dealHead').text(value.dealTitle);
row1.append(head);
table.append(row1);
// ********************************************************************

// ************************ Row 2 ************************************
row2 = $('<tr></tr>');
var col1 = $('<td></td>').addClass('name').text("Name: ");
var col2 = $('<td></td>').addClass('nameValue').text(value.name);

row2.append(col1);
row2.append(col2);                  
table.append(row2);
// ********************************************************************

// ************************ Row 3 ************************************
row3 = $('<tr></tr>');
col1 = $('<td></td>').addClass('address').text("Address: ");
col2 = $('<td></td>').addClass('addressValue').text(value.address);

row3.append(col1);
row3.append(col2);                  
table.append(row3);

// ********************************************************************

table.appendTo('body')

CSS:

body
{
background-color:#5CD65C;
}

.dealHead
{
text-align:center;
}


.name
{
border-top:thick double #0F0F00;
border-left:thick double #0F0F00;
border-bottom:thick double #0F0F00;

}

.nameValue
{

border-top:thick double #0F0F00;
border-right:thick double #0F0F00;
border-bottom:thick double #0F0F00;
text-align:left;

}

.address
{
border-top:thick double #0F0F00;
border-left:thick double #0F0F00;
border-bottom:thick double #0F0F00;
}

.addressValue
{

border-top:thick double #0F0F00;
border-right:thick double #0F0F00;
border-bottom:thick double #0F0F00;
text-align:left;
}


.tables
{
width="400";
border-collapse: collapse;

}

HTML

Pretty much an empty <body>

Would anyone know what I am doing wrong here?


Solution

  • CSS

    .tables { width="400"; ...

    should be

    .tables { width: 400px; ...
    

    In order to have both cells the same with:

    .tables td { width: 50%; }
    

    Javascript

    var head = $('<th></th>').addClass('dealHead').text(value.dealTitle);

    This is only one cell, make it like two:

    var head = $('<th colspan="2"></th>').addClass('dealHead').text(value.dealTitle);
    


    jsFiddle