generate html inline css style for a table in javascript

index.tsx
// Create a table element
const tableElement = document.createElement("table");

// Set inline styles using Javascript DOM
tableElement.style.border = "1px solid black";
tableElement.style.width = "100%";

// Create the table rows and cells
for(let i = 0; i < 3; i++) {
    const rowElement = tableElement.insertRow();
    for(let j = 0; j < 3; j++) {
        const cellElement = rowElement.insertCell();
        cellElement.innerText = `Row ${i} Column ${j}`;
        cellElement.style.border = "1px solid black";
        cellElement.style.padding = "10px";
    }
}

// Add the table to the HTML document
document.body.appendChild(tableElement);
647 chars
21 lines

This code creates a HTML table element using Javascript DOM and sets inline styles for the table and its cells using Javascript style property. The table element variable can then be added to the HTML document using appendChild method.

related categories

gistlibby LogSnag