admin管理员组文章数量:1289540
I referred this generate-html-table-using-javascript webpage and created a table. It works fine. In the javascript function it places the items as left aligned. How do I access the element and align it to center?
Here is my javascript function
function addRow() {
var table = document.getElementById("myTableData");
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);
row.insertCell(0).innerHTML = '<input type="button" value = "Delete" style="width: 65px;" class="btn btn-danger" onClick="Javascript:deleteRow(this)">';
row.insertCell(1).innerHTML = '<input id="workloadDescription" style="width: 138px;" name="workloadDescription" type="text" value="Description"/>';
}
<table border="1" align="center" id="myTableData" span style="width:1580px;">
<thead>
<tr>
<td bgcolor="green" align="center" style="width: 75px;overflow: hidden;">
<font color="white">Operation</font>
</td>
<td bgcolor="green" align="center" style="width: 140px;overflow: hidden;">
<font color="white">Workload <br> Description
</font>
</td>
</thead>
</table>
The HTML looks like this
But I want the button to be aligned in the center.
I referred this generate-html-table-using-javascript webpage and created a table. It works fine. In the javascript function it places the items as left aligned. How do I access the element and align it to center?
Here is my javascript function
function addRow() {
var table = document.getElementById("myTableData");
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);
row.insertCell(0).innerHTML = '<input type="button" value = "Delete" style="width: 65px;" class="btn btn-danger" onClick="Javascript:deleteRow(this)">';
row.insertCell(1).innerHTML = '<input id="workloadDescription" style="width: 138px;" name="workloadDescription" type="text" value="Description"/>';
}
<table border="1" align="center" id="myTableData" span style="width:1580px;">
<thead>
<tr>
<td bgcolor="green" align="center" style="width: 75px;overflow: hidden;">
<font color="white">Operation</font>
</td>
<td bgcolor="green" align="center" style="width: 140px;overflow: hidden;">
<font color="white">Workload <br> Description
</font>
</td>
</thead>
</table>
The HTML looks like this
But I want the button to be aligned in the center.
Share Improve this question edited Mar 12, 2017 at 14:41 mplungjan 178k28 gold badges181 silver badges240 bronze badges asked Mar 12, 2017 at 14:31 juniorbansaljuniorbansal 1,2719 gold badges31 silver badges54 bronze badges 3- You forgot a 'c' in 'javasript:....' – JustARandomProgrammer Commented Mar 12, 2017 at 14:35
- Thank u. Corrected it in my code and in this post – juniorbansal Commented Mar 12, 2017 at 14:36
-
The label Javascript is not needed here
Javascript:deleteRow
– mplungjan Commented Mar 12, 2017 at 14:42
6 Answers
Reset to default 3change your code to below, add margin:auto; display:block;
to your button this will make button center
row.insertCell(0).innerHTML = '<input type="button" value = "Delete" style="width: 65px; margin:auto; display:block;" class="btn btn-danger" onClick="Javascript:deleteRow(this)">';
All you need to do is adding text-align:center;
to your table like so
<table border="1" align="center" id="myTableData" span style="width:1580px; text-align:center;"
Try to keep your styles separate from JavaScript.
With JavaScript you should add a new class to the button:
row.insertCell(0).innerHTML = '<input type="button" value = "Delete" style="width: 65px;" class="btn btn-danger btn-centered" onClick="Javascript:deleteRow(this)">';
In your CSS file, you style that button:
.btn-centered {
text-align: center;
}
Just use text-align: center
on <td>
as follow:
tbody > tr > td {
text-align: center;
}
If you want to center only the content of first <td>
, select the first child:
tbody > tr > td:first-child {
text-align: center;
}
JSFiddle
Before writing the CSS for this I remend you to wrap the td elements inside a div and the below CSS will do the rest for you.
#myTableData tr td div {text-align:center; }
This will center your div.
Div by itself is a blockelement. Therefor you need to define the style to the div how to behave.
Find Your Fiddle
https://jsfiddle/ey36s317/3/
To align itens you can use margin: auto;
and text-align:center;
https://jsfiddle/r84tcq5c/
本文标签: javascriptHow to align the lttdgt at center in a dynamic HTML tableStack Overflow
版权声明:本文标题:javascript - How to align the <td> at center in a dynamic HTML table - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741417470a2377600.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论