Is there a way to align columns in HTML table? - html

I'm trying to create a basic form, including a table, at the beginning of an Angular project, but I can't get the columns to align.
I've tried all manner of permutations with the styling for HTML <table> elements <table>, <tr>,,,width, 'float' etc., and have tried using vertical bars`.
My code borrow heavily from a W3Schools page, https://www.w3schools.com/html/tryit.asp?filename=tryhtml_table_intro, so it's strange that it doesn't align (not only don't the columns align, but the alternate row coloring also doesn't work).
<style>
table {
border-collapse: collapse;
width: 5%;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
width: 50%;
}
tr {
width: 100%;
}
tr:nth-child(even) {
background-color: #522a2a;
}
</style>
<form style="margin-left: 2%; margin-right: 2%">
<h3>Form</h3>
<table>
<tr>
<th>Category</th>
<th>Input</th>
</tr>
<div *ngFor="let key of memberKeys">
<tr>
<td>{{key}}</td>
<td><input type="text" [(ngModel)]="member[key]" [ngModelOptions]="{standalone: true}"/></td>
</tr>
</div>
</table>
<button type="submit">Submit</button>
</form>
Not sure why the columns don't align, and the rows don't alternate background color. Many thanks for some advice!

There is a div where the table expects a valid child element such as tr:
<table>
<tr>[...]</tr>
<div *ngFor="let key of memberKeys">
Try moving the div to another location so that the table rows are all siblings.

Related

How did the browser decide on the widths of these table columns?

I have the following table:
I'm a little unhappy with it, because the width of the second column forces the text to wrap into 3 lines. Wouldn't it have made more sense for the renderer to give the first column a little less width, and wrap both elements onto two lines? Like this:
Is there some way I've set my table up wrongly?
(I don't want to set an explicit column width because I'm not sure what other content I might want to put in there later. I'd like to work with the browser's default behaviour as much as possible).
Thank you!
<table class="sequence">
<tbody>
<tr>
<td>Rhythm</td>
<td>Msec</td>
</tr>
<tr>
<td>8xxooxxoo 6xxxxxx 8xxooxxoo 6xxxxxx</td>
<td>168ms average<br>(5 tapbacks)</td>
</tr>
</tbody>
</table>
and css
table.sequence {
margin-left:auto;
margin-right:auto;
width: 320px;
}
table.sequence th, table.sequence td {
border: 1px solid black;
border-collapse: collapse;
padding-left: 5px;
padding-right: 5px;
padding-top: 2px;
padding-bottom: 2px;
vertical-align: middle;
word-wrap: normal;
}

How to vertically align Text in the middle with an Image in the same HTML Table Cell

Here's the Output screenshot
I'm struggling to vertically align text with an image within the same HTML cell that they share. I'm sharing a simpler version of the test html file I created, but in the past I've tried everything from CSS and nested tables but somehow couldn't get it to work. Appreciate the help.
CSS:
<style>
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 25%;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: #dddddd;
}
</style>
Html:
<table>
<tbody>
<tr>
<th>Col-1</th>
<th>Col-2</th>
</tr>
<tr>
<td><img src="Star.png" style="width:60px"> Static Text</td>
<td>Static Text</td>
</tr>
</tbody>
</table>
It's very simple. Use vertical-align: middle; on the image. It will push the text to the center as well.
Example
Also if I were you I wouldn't use inline CSS. It can give problems in future, use instead an external CSS file

input field width is not what I set it to be, misaligned. Additional border in input

http://jsfiddle.net/HnnHf/1/
Trying to understand what I do wrong. Plain table, I want input boxes to fill cells evenly. On first row you see 2 inputs and second row has one input spanned across cells.
Their right sides don't match. Why? When I run inspector it shows additional pixels?
Part of my HTML:
<div style="width: 1000px; margin-left: auto; margin-right: auto; padding-left: 20px; padding-top: 10px;">
<table>
<tr>
<td style="width: 80px;"><label>From </label></td>
<td style="width: 120px;">
<input type="text" class="fill-space" />
</td>
<td style="width: 80px;"><label>To </label></td>
<td style="width: 120px;">
<input type="text" class="fill-space" />
</td>
<td style="width: 80px;"><label>Sort by </label></td>
<td></td>
</tr>
<tr>
<td colspan="6"> </td>
</tr>
<tr>
<td></td>
<td colspan="3">
<input type="text" class="search" />
</td>
<td></td>
<td>
Refresh button
</td>
</tr>
</table>
</div>
Style:
td label {
width: 100%;
color: #F1F1F1;
text-align: right;
vertical-align: central;
}
input.fill-space {
width: 100%;
}
input.search {
width: 100%;
background-image: url("/images/Search.png");
background-position: right center;
background-repeat: no-repeat;
}
</style>
My live site misalignment:
Also, why do I get this another border inside input if I set background?
Working fiddle: http://jsfiddle.net/ghUEw/
Default padding and margins for table elements differ in different browsers.
So you'd better use a CSS reset on table elements.
table * {
margin: 0;
padding: 0;
}
Then, comes the border-collapse property. It determines whether the table borders are collapsed into a single border or rendered individually, let's say for neighboring table cells. You need to set it as following to make them collapsed since you have different number of cells per table row.
table {
border-collapse: collapse;
}
Then, you need to set the borders of the inputs in your table if you want them look the same.
table input {
border: 1px solid #aaa;
}
If you don't want any borders to appear, replace it with border: none;
Then, in your CSS, for the labels to appear the way you want, you can apply float:right; (also corrected vertical-align: middle;)
td label {
width: 100%;
color: #F1F1F1;
text-align: right;
vertical-align: middle;
float:right;
}

CSS limit border to table cell TD content

Hello all I'm just trying to have my border around my table cell right around the text...not stretched the length of the entire table. Its the section with the border around it
CSS:
table.content_table {
width: 100%;
margin: 0px;
border-collapse: collapse;
}
table.content_table > tbody > tr > td.results {
border: 2px solid;
background-color: #eeeecc;
font-size: 8pt;
font-weight: bold;
PADDING: 0px;
}
HTML:
<table class="content_table">
<br/><br/>
<h1>Planned Vs Actual Productions Drilldown</h1>
<tr>
<td class="results">
Number of results returned: ${fn:length(beans)}
</td>
</tr>
give the text a simple span or any other block element like div p ... span with inline-block is also a block element which can have a border.
table.content_table {
width: 100%;
margin: 0px;
border-collapse: collapse;
}
.border {
border: 2px solid;
background-color: #eeeecc;
font-size: 8pt;
font-weight: bold;
PADDING: 0px;
display: inline-block;
}
Any Element inside a table needs to be in TD so that is is valid html... put another tr > td into your table like this
<table class="content_table">
<tr>
<td>
<h1>Planned Vs Actual Productions Drilldown</h1>
</td>
</tr>
<tr>
<td class="results">
<span class="border">Number of results returned: ${fn:length(beans)}</span>
</td>
</tr>
</table>
The answer lies in the fact that you have table width as 100%. Without any of styling at the TD level, the TD is automatically going to take the most width it can.
The bigger question though, is why you are using a table at all. This is a single column of data, no need for a table here, just use div's.
I had a similar problem with a WordPress theme. The "collapse" wasn't entirely working on the first column, because my theme's style.css "reset" had set the table width to 100%. At least for me, the "auto" width solved the problem.
<style>
table#donations { border-collapse: collapse; width:auto; }
</style>
<table id="donations">
<tr><td>Bitcoin BTC</td><td>1Prh5VnUJRQV3sARhEfQAMKv9UzGqgAMXg</td></tr>
</table>

Make link in table cell fill the entire row height

I have a table of data and each cell is a link. I want to allow the user to click anywhere in the table cell and have them follow the link. Sometimes the table cells are more than one line but not always. I use td a {display: block} to get the link to cover most of the cell. When there is one cell in a row that is two lines and the others are only one line the one liners don't fill the entire vertical space of the table row. Here is the sample HTML and you can see it in action here http://www.jsfiddle.net/RXHuE/:
<head>
<style type="text/css">
td {width: 200px}
td a {display: block; height:100%; width:100%;}
td a:hover {background-color: yellow;}
</style>
<title></title>
</head>
<body>
<table>
<tbody>
<tr>
<td>
<a href="http://www.google.com/">Cell 1<br>
second line</a>
</td>
<td>
Cell 2
</td>
<td>
Cell 3
</td>
<td>
Cell 4
</td>
</tr>
</tbody>
</table>
</body>
Set an arbitrarily large negative margin and equal padding on the block element and overflow hidden on the parent.
td {
overflow: hidden;
}
td a {
display: block;
margin: -10em;
padding: 10em;
}
http://jsfiddle.net/RXHuE/213/
You need a small change in your CSS. Making td height:100%; works for IE 8 and FF 3.6, but it doesn't work for Chrome.
td {
width: 200px;
border: solid 1px green;
height: 100%
}
td a {
display: block;
height:100%;
width:100%;
}
But making height to 50px works for Chrome in addition to IE and FF
td {
width: 200px;
border: solid 1px green;
height: 50px
}
td a {
display: block;
height:100%;
width:100%;
}
Edit:
You have given the solution yourself in another post here; which is to use display: inline-block;.
This works when combined with my solution for Chrome, FF3.6, IE8
td {
width: 200px;
border: solid 1px green;
height: 100%}
td a {
display: inline-block;
height:100%;
width:100%;
}
Update
The following code is working for me in IE8, FF3.6 and chrome.
CSS
td {
width: 200px;
border: solid 1px green;
height: 100%;
}
td a {
display: inline-block;
height:100%;
width:100%;
}
td a:hover {
background-color: yellow;
}
HTML
<table>
<tbody>
<tr>
<td>
<a href="http://www.google.com/">Cell 1<br>
second line</a>
</td>
<td>
Cell 2
</td>
<td>
Cell 3
</td>
<td>
Cell 4
</td>
</tr>
</tbody>
</table>
The example lays here
Little late to the party, but there's a nice solution I just discovered.
You can use a combination of relative and absolute positioned elements, along with a pseudo element to get the effect you're looking for. No extra markup needed!
Change the table cell (<td>), to be position: relative;, and create a ::before or ::after pseudo element on the <a> tag, and set it to position: absolute;, and also use top: 0; left: 0; right: 0; bottom: 0;.
Because the pseudo element is attached to the anchor tag, and you're telling it to take up the entire table cell, it will force the anchor tag to be at least that size, whilst not affecting the actual content of the anchor tag itself (thereby retaining its vertically centered alignment).
For example
table {
border-collapse: collapse;
table-layout: fixed;
}
td {
position: relative;
width: 200px;
padding: 0.5em 1em;
border: 2px solid red;
background-color: lime;
}
td a {
/* FONT STYLES HERE */
text-decoration: none;
}
td a::after {
content: '';
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
z-index: 0;
}
<table>
<tbody>
<tr>
<td>
<a href="http://www.google.com/">Cell 1<br>
second line</a>
</td>
<td>
Cell 2
</td>
<td>
Cell 3
</td>
<td>
Cell 4
</td>
</tr>
<tr>
<td>
Cell 5
</td>
<td>
<a href="http://www.google.com/">Cell 6<br>
second line</a>
</td>
</tr>
</tbody>
</table>
Hope this helps!
Following hack works [Tested on Chrome / Firefox / Safari]
Have the same padding for td and anchor elements. And for anchor also have margin which is equal to -ve of padding value.
HTML
<table>
<tr>
<td><a>Hello</a></td>
</tr>
</table>
CSS:
td {
background-color: yellow;
padding: 10px;
}
a {
cursor:pointer;
display:block;
padding: 10px;
margin: -10px;
}
Working Fiddle :http://jsfiddle.net/JasYz/
Try display: block:
td a {display: block; height:100%;}
[EDIT] WTF ... I can confirm this doesn't work in FF 4 and Chrome. This works:
td a {display: block; height: 2.5em; border: 1px solid red;}
That suggests that height:100%; isn't defined in a table cell. Maybe this is because the cell gets its size from the content (so the content can't say "tell me your size" because that would lead to a loop). It doesn't even work if you set a height for the cells like so:
td {width: 200px; height: 3em; padding: 0px}
Again the code above will fail. So my suggestion is to use a defined height for the links (you can omit the width; that is 100% by default for block elements).
[EDIT2] I've clicked through a hundred examples at http://www.cssplay.co.uk/menus/ but none of them mix single line and multi-line cells. Seems like you hit a blind spot.
I will post the same answer here, as I did on my own question.
Inspired by Jannis M's answer, I did the following:
$(document).ready(function(){
$('table tr').each(function(){
var $row = $(this);
var height = $row.height();
$row.find('a').css('height', height).append(' ');
});
});
I added a since empty links (not containing text nodes) can not be styled(?).
See my updated fiddle.
Only problem here is that using display: block forces the browser to ignore the vertical align: center...
oops.
I jury rigged it to look right for one cell with height:60 and a font that occupied 20 pixels by adding a br... Then I realized that I had some items with 2-line text. Dang.
I ended up using the javascript. The javascript doesn't give the nice mousey pointy clicker thing, but the line of text does, so it will actually trigger a visual response, just not where I want it to... Then the Javascript will catch all the clicks that 'miss' the actual href.
Maybe not the most elegant solution, but it works well enough for now.
Now if I could only figure out how to do this the right way....
Any ideas on how to add the mouse icon change to a hand for the area covered by the onclick? Right now, the click to page works, but the icon only changes when it hits the href which only affects the text.
Why don't you just get rid of the <a> altogheter and add an onClick to the <td> directly?
<head>
<style type="text/css">
td {
text-align:center;
}
td:hover {
cursor:pointer;
color:#F00;
}
</style>
<title></title>
</head>
<body>
<table>
<tbody>
<tr>
<td onclick="location.href='http://www.google.com/';">Cell 1<br />second line</td>
<td onclick="location.href='http://www.google.com/';">Cell 2</a></td>
<td onclick="location.href='http://www.google.com/';">Cell 3</td>
<td onclick="location.href='www.google.com';">Cell 4</td>
</tr>
</tbody>
</table>
This way you cut out the middle man.
PS: i know this was asked and answered many years ago, but none of the answers above solved the problem in my case. Hope this helps someone.
For me the only solution is to replace <table> <tr> with <div>s and style them using display:table and display:table-row accordingly.
Then you can replace <td> with just <a> and style it with display:table-cell.
Work perfectly even on varying heights of <td> contents.
so original html without anchors:
<table>
<tr>
<td>content1<br>another_line</td>
<td>content2</td>
</tr>
</table>
now becomes:
a:hover
{
background-color:#ccc;
}
<div style="display:table; width:100%">
<div style="display:table-row">
content1<br>another_line
content2
</div>
</div>
I have used this solution: works better then the rest in my case.
CSS:
.blocktd {width: 100%; height: 100%; padding: 0px; overflow: hidden}
a.blocktd {margin: 0em; padding: 50px 20px 50px 20px; display: block;}
a.blocktd:hover {border: 4px solid #70AEE8; border-radius: 10px; padding: 46px 16px 46px 16px; transition: 0.2s;}
And in HTML: ...