So I am creating a table in html using javascript. However, anytime I create the table, the first column is much larger than the second column, while the second column is extremely compressed.
I am trying to get the first column to be compressed and the second column to be the larger one, but for some reason it doesn't want to work.
let rankings2 = [];
function generateTableHead(table, data) {
let thead = table.createTHead();
let row = thead.insertRow();
for (let key of data) {
let th = document.createElement("th");
let text = document.createTextNode(key);
th.appendChild(text);
row.appendChild(th);
th.style.textAlign='center';
}
}
function generateTable(table, data){
for (let element of data) {
let row = table.insertRow();
for (key in element) {
let cell = row.insertCell();
let text = document.createTextNode(element[key]);
cell.appendChild(text);
cell.style.textAlign='center';
}
}
}
let HotSeat = [{pos: "1", name:"Ben Shapiro's Wife"},
{pos: "2", name: "Illegal Bookies"},
{pos: "3", name: "76ers"},
{pos: "4", name: "The Post Office",},
{pos: "5", name: "New Orleans"}];
let table2 = document.getElementById("HotSeatTable");
let data2 = Object.keys(HotSeat[0]);
generateTableHead(table2,data2);
generateTable(table2,HotSeat);
window.history.forward(1);
#HotSeat {
font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
border-collapse: collapse;
margin: 0 auto;
width: 50%;
box-shadow: 0 4px 6px 0 hsla(0, 0%, 0%, 0.2);
}
#HotSeat td,
#HotSeat th {
border: 1px solid #ddd;
text-align: left;
width: 100%;
padding: 20px;
}
#HotSeat tr:nth-child(even) {
background-color: #f2f2f2;
}
#HotSeat tr:hover {
background-color: #ddd;
}
#HotSeat th {
padding-top: 12px;
padding-bottom: 12px;
text-align: left;
width: left;
background-color: red;
color: white;
}
<div id="HotSeat">
<table id="HotSeatTable"></table>
</div>
here is what the table looks like when I dont have width: 100% in td,th
Remove width off the td selector. This is causing your columns to look skewed.
Then add width: 100% to get the table to fit the width of the container.
Additionally, I am not sure what you are trying to do with width: left property, so I would remove it. Same with the text-align: left in your style sheet, you set text-align: center dynamically in your JS, rendering left align useless unless you really want them left aligned.
#HotSeat {
border-collapse: collapse;
margin: 0 auto;
width: 50%;
}
#HotSeatTable {
width: 100%;
text-align: center;
}
#HotSeat td,
#HotSeat th {
border: 1px solid #ddd;
padding: 20px;
}
#HotSeat th {
padding-top: 12px;
padding-bottom: 12px;
background-color: red;
color: white;
}
<div id="HotSeat">
<table id="HotSeatTable">
<table id="HotSeatTable">
<tr>
<th>Pos</th>
<th>Name</th>
</tr>
<tr>
<td>1</td>
<td>Ben Shapiro's Wife</td>
</tr>
<tr>
<td>2</td>
<td>Illegal Bookies</td>
</tr>
<tr>
<td>3</td>
<td>76ers</td>
</tr>
<tr>
<td>4</td>
<td>The Post Office</td>
</tr>
<tr>
<td>5</td>
<td>New Orleans</td>
</tr>
</table>
</div>
Related
I am working on my website and I have most of the design worked out, shown in the first image. I am trying to make the header row have no space (more accurately, make it look that way by having the image span across the entire row with no spaces.), but still have the elements themselves have space in between them.
Image showing a joined header, but separate body elements:
I am aware of the border-spacing css style, but it has to be applied to the table element, which means it will apply to headers and body elements. This with with a border-spacing set
Image showing what happens to the images when the border-spacing is set on the table:
I did attempt to find an answer before posting and usually I find the answer fairly quickly, but this one seems to be a rare request. I prefer to avoid hacks if possible, but I will use them if its the only way. Also, if possible, I'd like it to be cross-browser capable. (changes to the solution are ok of course, just something that I can make work will all of them.)
I guess code is helpful to show. Here is the html:
<table id="users">
<caption>Point Totals</caption>
<thead>
<tr>
<th>Name</th>
<th>Points</th>
</tr>
</thead>
<tbody>
<tr>
<td>Xogue</td>
<td>64433</td>
</tr>
<tr>
<td>Jesse</td>
<td>104040</td>
</tr>
<tr>
<td>Nanokarp</td>
<td>280</td>
</tr>
<tr>
<td>Brandon</td>
<td>70</td>
</tr>
</tbody>
</body>
and the css:
#users {
border-spacing: 6px;
width: 444px;
float: left;
margin: 20px 90px;
}
#users caption {
background-image: url("_images/points_label.png");
background-size: 200px 35px;
background-position: center center;
background-repeat: no-repeat;
height: 31px;
font-size: 20px;
padding-top: 8px;
}
#users thead {
background-image: url("_images/point_tr_head_back.png");
background-repeat: no-repeat;
}
#users th {
text-align: left;
padding: 9px 30px;
font-size: 20px;
}
#users td {
border: 2px solid #226fdb;
border-radius: 25px;
font-size: 20px;
padding: 18px 5px 2px 25px;
line-height: 15px;
background: #FFFFFFEE;
}
Note: Some of the styles used are likely unnecessary. I've been toying with it for a while and haven't cleaned it up yet.
SOLVED: further down if you would like to see the example. but put simply, wrap the content in a different element (like a span) and move all styles to the new element.
I think you could get there fairly easily with some margins and padding if you can wrap the cell contents in a span (or whatever).
table {
text-align: left;
border-spacing: 0;
}
thead tr {
box-shadow: inset 0 0 12px rgba(0,0,0,0.5);
border-radius: 24px;
}
th {
padding: 6px 1em;
}
th:first-child {
border-radius: 24px 0 0 24px;
}
th:last-child {
border-radius: 0 24px 24px 0;
}
tbody td > span {
display: block;
border: 2px solid blue;
background: aliceblue;
border-radius: 24px;
margin: 6px 6px 0 0;
padding: 0.25em 1em;
}
<table>
<thead>
<tr>
<th>Name</th>
<th>Points</th>
</thead>
<tbody>
<tr>
<td><span>Xogue</span></td>
<td><span>262677</span></td>
</tr>
<tr>
<td><span>Jesse</span></td>
<td><span>17632</span></td>
</tr>
<tr>
<td><span>Nanokarp</span></td>
<td><span>12344</span></td>
</tr>
</tbody>
</table>
One option is to add padding to your CSS for td only and keep rest of the attributes same among td and th as below, also if you can share the used code snippet someone can help better:
td {
padding: 5px;
}
I want to change th, td values-based values ..suppose align numbers to right(except s.no) and text to left.
<button type="submit" class="btn btn-raised btn-primary mr-5"
(click)="productPrintSection('productSection')">
<i class="fa fa-check-square-o"></i> Print
</button>
my Html code is bellow and these data getting dynamically ...
<div id="invoice-items-details" class="pt-2">
<div class="row">
<div class="table-responsive col-sm-12">
<table class="table">
<thead>
<tr>
<th
*ngFor="let column of productColumns">
{{column.name}}
</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let list of productSource; let i=index">
<td
*ngFor="let column of productColumns">
{{ list[column['value']] }}
</td>
</tr>
</tbody>
</table>
</div>
</div>
my print section code is
productPrintSection = function (reportSection) {
let printContents, popupWin;
printContents = document.getElementById(reportSection).innerHTML;
popupWin = window.open('', '_blank', 'top=0,left=0,height=100%,width=auto');
popupWin.document.open();
popupWin.document.write(`
<html>
<head>
<style>
body { width: 99%;}
h1 {
text-align:center;
}
ul {
list-style: none;
padding: 0;
margin: 0;
}
label { font-weight: 400;
font-size: 13px;
padding: 2px;
margin-bottom: 5px;
}
table, td, th {
border: 1px solid silver;
}
table td {
text-align: center;
font-size: 13px;
}
table th {
font-size: 13px;
}
table {
border-collapse: collapse;
width: 98%;
}
th {
height: 26px;
}
</style>
</head>
<body onload="window.print();window.close()">${printContents}</body>
</html>`
);
popupWin.document.close();
}
my output screen
could u please suggest me how to show align of numbers to right(except s.no) and text to left.
well I could not find any css selector for selecting specific cells with numbers in them. That's why I've made a javascript function to check whether the cellmate can be converted to a number or not with !isNaN(col.innerText). Just call the styleTable function whenever you need it.
The following snippet contains an example based on the by you provided code.
var table = document.getElementById("products");
window.onload = function() {
styleTable(table)
};
function styleTable(table) {
//loop through the second row to check what kind of value they have
for (var i = 1, row; row = table.rows[i]; i++) {
//exclude the first column "S.no"
for (var j = 1, col; col = row.cells[j]; j++) {
//check if the column value is a number
if (!isNaN(col.innerText)) {
col.style.textAlign = "right";
}
}
}
}
body {
width: 99%;
}
h1 {
text-align: center;
}
ul {
list-style: none;
padding: 0;
margin: 0;
}
label {
font-weight: 400;
font-size: 13px;
padding: 2px;
margin-bottom: 5px;
}
table,
td,
th {
border: 1px solid silver;
}
table td {
text-align: center;
font-size: 13px;
}
table th {
font-size: 13px;
}
table {
border-collapse: collapse;
width: 98%;
}
th {
height: 26px;
}
<table id="products">
<tr>
<th>S.no</th>
<th>Product Name</th>
<th>Product code</th>
<th>Purity</th>
<th>Store</th>
<th>city</th>
<th>state</th>
<th>quantity</th>
<th>weight</th>
<th>total</th>
<th>total orders</th>
</tr>
<tr>
<td>1</td>
<td>xyzzy</td>
<td>1001010</td>
<td>54.00</td>
<td>Default</td>
<td>Hyderabad</td>
<td>dashdsaf</td>
<td>10</td>
<td>2135.5000</td>
<td>239280324.09</td>
<td>12</td>
</tr>
<tr>
<td>2</td>
<td>xyzzy</td>
<td>1001010</td>
<td>54.00</td>
<td>Default</td>
<td>Hyderabad</td>
<td>adsfsdf</td>
<td>0</td>
<td>2135.5000</td>
<td>239280324.09</td>
<td>13</td>
</tr>
</table>
Hope this helps!
Edit:
Provided a more dynamic way of styling each cell accordingly.
I have a table on my html page made by using angular but mainly HTMl & CSS (see the code snippets down below).
Because the tables are probably going to be rather large eventually, we wanted to make the table to be able to scroll. That itself, wasn't that big of a problem, and the table with only one value didn't cause any problems either.
After some fiddeling around the second table that has four values, did not want to work with me.
I have tried various lines of css that also have been used on the other table. It probably is something really simple that I just can't think of right now.
This is the table with only one value, and works just fine.
<table class="table table-hover table-striped">
<thead>
<tr>
<th>Locaties</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let location of locations | filterPipeDep: searchToken">
<td (click)="SpecificLocation(location.departmentId)">{{location.departmentName}}</td>
</tr>
</tbody>
</table>
table {
-webkit-border-radius: 8px !important;
-moz-border-radius: 8px !important;
border-radius: 8px !important;
background-color: rgba(245, 245, 245, 0.644);
color: black;
}
thead {
background-color: rgb(245, 245, 245);
color: black;
}
tbody {
display: block;
overflow-y: auto;
width: 100%;
height: 350px;
}
tr {
display: block;
width: 100%;
}
td, th{
text-align: center;
display: block;
}
The table that doesn't work (and I did see the missing td and th class, but display block on every td and th with multiple rows isn't really a solution)
<table class="table table-striped table-hover">
<thead>
<tr>
<th>E-mail</th>
<th>Voornaam</th>
<th>Achternaam</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let user of users | filterPipeUsers: searchToken">
<td (click)="SpecificUser(user.userId)">{{user.username}}</td>
<td (click)="SpecificUser(user.userId)">{{user.firstName}}</td>
<td (click)="SpecificUser(user.userId)">{{user.lastName}}</td>
<td><a (click)="Debug()"><img class="delete"
src="https://cdn4.iconfinder.com/data/icons/controls-add-on-flat/48/Contols_-_Add_On-35-512.png" /></a>
</td>
</tr>
</tbody>
</table>
table {
-webkit-border-radius: 8px !important;
-moz-border-radius: 8px !important;
border-radius: 8px !important;
background-color: rgba(245, 245, 245, 0.644);
color: black;
}
thead {
background-color: rgb(245, 245, 245);
color: black;
}
tbody {
display: block;
overflow-y: auto;
width: 100%;
height: 350px;
}
tr {
display: block;
width: 100%;
}
Like I said, it probably is something really simple I am just missing or overlooking. What I would like to see is the table being able to scroll down and up, with a fixed header, the text lined out and using 100% of the table with even widths.
*Note, this question has basically been overhauled from a previous version so as to be more precise. Thus some of the answers below do not completely the restructed question.
I have two sets of data which I need to display tabulated. As both sets of data need to have the column widths (but still be dynamic), I am using two <tbody>'s.
I am trying to set a heading for each of the tabulated data, in a way that the heading takes up the width of the entire <tbody>.
I have tried using table-caption, but it does not apply to the tbody, but the table itself. Meaning all captions look to go to the top of the table, regardless of where they are in the html.
To demonstrate what I am running into, see the following snippet:
table {
width: 100%;
border-collapse: collapse;
color: black;
margin-bottom: 2px;
}
tbody:before {
display: table-caption;
font-size: 1.25em;
padding: 16px;
background-color: #303030;
font-weight: bold;
color: white;
width: 100%;
}
#tbody1:before {
content: 'tbody1';
}
#tbody2:before {
content: 'tbody2';
}
th,
td {
padding: 4px 0px;
border: 1px solid black;
}
caption {
border: 1px dotted black;
}
<table>
<tbody id="tbody1">
<caption>Caption1</caption>
<tr>
<th>bob</th>
<th>dob</th>
</tr>
</tbody>
<tbody id="tbody2">
<caption>Caption2</caption>
<tr>
<th>dob</th>
<th>bob</th>
</tr>
</tbody>
</table>
My current attempt is to use :before. But as you can see, the :before does not take up the entire width of the tbody. Even with width: 100% it does not work.
Another way I realized it could be done is to have another row for each tbody, and set colspan to equal the amount of columns for that table. Like this:
table {
width: 100%;
border-collapse: collapse;
color: black;
margin-bottom: 2px;
}
th,
td {
padding: 4px 0px;
border: 1px solid black;
}
caption {
border: 1px dotted black;
}
<table>
<tbody id="tbody1">
<tr>
<th colspan="2">Title1</th>
</tr>
<tr>
<th>bob</th>
<th>dob</th>
</tr>
</tbody>
<tbody id="tbody2">
<tr>
<th colspan="2">Title2</th>
</tr>
<tr>
<th>dob</th>
<th>bob</th>
</tr>
</tbody>
</table>
However, the only problem there is that it does not become dynamic and requires you to know how many columns there will be ahead of time. Normally this would not be a problem but I am looking for a more dynamic solution in my case.
My question is: How does one add a caption to a tbody (not the table) in a way so that each caption relates to the applicable tbody and not the table
You just need to set the width to 100vw. This sets the width to 100% of the viewport width. For a more in-depth explanation of viewport width, see this article.
table {
width: 100%;
border-collapse: collapse;
color: black;
margin-bottom: 2px;
}
#tbody1:before, #tbody2:before {
display: table-caption;
font-size: 1.25em;
padding: 16px;
background-color: #303030;
font-weight: bold;
color: white;
width: 100vw;
}
#tbody1:before {
content: 'tbody1';
}
#tbody2:before {
content: 'tbody2';
}
th, td {
padding: 4px 0px;
border: 1px solid black;
}
<table>
<tbody id="tbody1">
<tr>
<th>bob</th>
</tr>
</tbody>
<tbody id="tbody2">
<tr>
<th>dob</th>
</tr>
</tbody>
</table>
I'm having css specificity issues.
I want the <td> style to be colored different if there are 'no bills', so I set the style with an if statement. If there are no bills, the style is style = "id=\"no-bills\"";. I got this to work with css. Now, if the user hovers over it, I want the background to go red - so I modified the css and added #bill-list #no-bills td:hover, which has td:hover to add some extra specificity when you hover over it. Unfortunately, this doesn't work (the background stays the same).
here's my css:
#bill-list
{
font-family: "Lucida Sans Unicode", "Lucida Grande", Sans-Serif;
font-size: 12px;
/*margin: 45px;*/
width: 100%;
text-align: left;
border-collapse: collapse;
}
#bill-list th
{
font-size: 13px;
font-weight: normal;
padding: 8px;
background: #b9c9fe;
border-top: 4px solid #aabcfe;
border-bottom: 1px solid #fff;
color: #039;
}
#bill-list td
{
padding: 8px;
background: #e8edff;
border-bottom: 1px solid #fff;
color: #669;
border-top: 1px solid transparent;
}
#bill-list tr:hover td
{
background: #d0dafd;
color: #339;
}
#bill-list #bill td
{
background: white;
}
#bill-list #no-bills
{
background: #FFCC99;
}
#bill-list #no-bills td:hover
{
color: orange;
background: red /*#FFCC66*/;
}
here's my code (snippet):
<table id="bill-list">
<thead>
<tr>
<% for (int i=0; i<vecHeader.size(); i++) { %>
<th><%=vecHeader.get(i) %></th>
<% } %>
</tr>
</thead>
<tbody>
<% int uniqueId = 0;
for (int i=0; i < vecValue.size(); i++) {
boolean hasBills = true;
String style = "";
if ( vecBills.get(i) == null || vecBills.get(i).size() == 0 ) {
hasBills = false;
style = "id=\"no-bills\"";
}
%>
<tr id="bill<%=i%>" onclick="javascript:showBillDetails(<%=i%>)">
<% for (int j=0; j < vecHeader.size(); j++) {
prop = (Properties)vecValue.get(i);
%>
<td <%=style%>><%=prop.getProperty((String)vecHeader.get(j), " ") %> </td>
<% } %>
</tr>
...
...
Anyone have any ideas?
You should post the HTML code, not the code that generates it, it's irrelevant here.
First problem: are you certain there can't be 2 no-bills id on the same page?
Then your problem: you're trying to style td:hover that is a descendant of #no-bills. The latter is the same td, not an ascendant! You should then style #bill-list #no-bills:hover, which appears to be a td being hovered.
try
#bill-list #no-bills:hover
instead of
#bill-list #no-bills td:hover
If there are multiple no-bills, you should use class over id.