How do I highlight table cell borders when empty? - html

I am using AngularJS to create dynamic rows and columns. And then on the save button click when there is no data in any cell I want highlight that cell.
So below is the JSFiddle for my demo:
http://jsfiddle.net/jyshdf85/
My table code as below:
<table class="table table-bordered" ng-form="targetTableForm" ng-class="{submitted: targetTableSubmitted}">
<colgroup>
<col class="unique-id">
</colgroup>
<thead>
<tr>
<th class="unique-id">Name #</th>
<th contenteditable="true" ng-repeat="c in targetTable.columns" ng-model="c.label"></th>
<!--<th class="view-next-set">...</th>-->
<td class="center add-column"><a href ng-click="open()">+ Add Column</a></td>
</tr>
</thead>
<tbody>
<tr ng-repeat="r in targetTable.rows">
<td contenteditable="true"></td>
<td contenteditable="true" ng-repeat="column in targetTable.columns" ng-model="r[column.id]" ng-blur="!r.id? addNewRow(r[column.id], r): undefined" ng-required="!$parent.$last"></td>
<!--<td class="blank" colspan="2"></td>-->
</tr>
</tbody>
</table>
I have my CSS as:
.table.submitted td.ng-invalid {
border-color: red;
}
How this works is:
Add a couple of columns by clicking on add column, then type in the table cell of the newly added columns,it would create a new row automatically. Create couple of rows. Then remove the values from the table cell. Now when you press Save button it highlights that table cell.
All this works fine the only issue being the way the table cell is being highlighted. The border of the cell is not fully highlighted. You can see the demo in the JSFiddle. I have also uploaded the image of this as:
https://imgur.com/a/FAHNYOn
I can change the background color of the cell and it works but looks ugly with all cells highlighted.
Can anyone point out what I am doing wrong here?

Looks like the border is not fully visible because covered by other cells' border.
A quick solution I suggest is to use the outline property instead of the border to highlight cells:
.table.submitted td.ng-invalid {
outline: 1px solid red;
outline-offset: -1px;
}
Or you can simply make the border wider, to cover other cells' border, but I think this is not the best way:
border: solid 3px red;
Hope this helps you getting the desired result!

A better and complete solution to this problem is covered in the following CSS-Tricks article: https://css-tricks.com/table-border-collapse-issue/
Basically, what you need to do is to put a <div> element inside your <td> and, using negative margins and z-index, you'll get the job done in no time. Refer directly to the article linked for a complete example with a Codepen.
Check it out and let me know if this works for you! :)

Related

Alternate table row color using CSS, individual cells get colored not the even rows

Somehow I can't figure out the following: Alternate table row color using CSS?
If I use the code suggested in the aforementioned topic the even cells get colored (the first, third and fifth. There is a total of 5 cells in a row). It doesn't matter if I use 'even' or 'odd'.
td:nth-child(odd)
{
background: #f5f6fa;
}
If use the following code, all the rows get colored.:
tr:nth-child(odd)
{
background: #f5f6fa;
}
#svdh You've got tags outside of your body and html tags also which in a normal web page wouldn't be rendered. The problem with your HTML is you're setting up loads of tables instead of one with multiple rows. It should be like this.
<table>
<tr>
<td>One</td>
<td>One.</td>
</tr>
<tr>
<td>Two</td>
<td>Two.</td>
</tr>
<tr>
<td>Three</td>
<td>Three.</td>
</tr>
</table>
Fiddle here..
https://jsfiddle.net/fo7Ldfqs/
UPDATED:
If you've got multiple tables and you're trying to color every other one then just use:
table:nth-child(odd){background:#ff0000;}
Fiddle here.. https://jsfiddle.net/4641ph6u/

Align a column with CSS

I want to align columns in a table based on a class on the header.
For example, if I have the following table:
<table>
<thead>
<th>Name</th>
<th class="price">Price</th>
</thead>
<tbody>
....
</tbody>
<table>
I know I can use :nth-of-type but sometimes the column will be the 2th, other time will be the 5th and in some places I'll have several columns in the same table align to the right.
Is there a way to accomplish that?
I don't need to support legacy browsers, not even Internet Explorer 9 (if is works on chrome and/or firefox is enough for me)
Just for clarification, I want to align the text in the columns that are in the body associated with the column at the head
No, you cannot style table cells so that styling depends on a class attribute on a column header th. There is nothing in CSS that connects cells that way.
The most robust way to align a column is to generate class attributes on each cell in it. Well, technically, using the HTML align attribute is even more robust.
EDIT: As commented below this only works with a few properties. Text-align isn't included.
Put this in your CSS:
col.price { text-align:right; }
And your HTML:
<table>
<col />
<col class="price" />
<tr>
<td>First TD of first TR</td>
<td>9,95</td>
</tr>
<tr>
<td>First TD of second TR</td>
<td>4,85</td>
</tr>
</table>
Reference:
http://quirksmode.org/css/css2/columns.html

nesting html tables issue

Hello I have nested a table inside a gridview(which I think is also basically a table) in asp.net ,but the thing is the table which is inside a cell leaves a space at it edges ,(i.e from the wall of the grid view), I want this space to be minimized this is my grid view :
<FooterTemplate>
<table border="1" cellspacing="0" cellpadding="0" width="auto">
<tr><td><asp:Label ID="cl_crTotal" runat="server" CssClass="alLbl" /></td>
</tr>
<tr><td><asp:Literal runat="server" ID="closingTotC" ></asp:Literal></td>
</tr>
</table>
</FooterTemplate>
I have also given the image ,please check
http://imgur.com/4ukGm&K8tRy "this image"
PS:I know and hope this is a simple problem and would be easily solve just that I am new to this
Without seeing the code of that grid-view, I can only guess, that there are some paddings or margins set, which cause this behavior.
So for the grid cell, in which the table appears set
padding: 0;
and for the table itself set
margin: 0;
Actually, its pretty simple. If you look at all your fields in the data, you see actually every field has it. Its called a padding.
What you should do is remove the padding from that cell. I am going to asume you dont want it in the rest of your table, so what you do is give the specific cell an id, and add in the css a rule to it saying it shouldnt receive any padding.
your html would look like this:
<body>
<table>
<tr>
<td id="cell_id">somedata</td>
</tr>
</table>
</body>
This would look something like this in your css file
#cell_id { padding: 0px; }

CSS border-collapse not working in <table>

I'm having an impossible time getting border-collapse to work for me. The page I'm working with has a table in it. The table has 2 columns, one for a label and the other for data. Sometimes there is no data to display, but I still need to rendor the table row and label column because I have a JQuery script that might need to write data to the data column. In other words, regardless of whether there is data or not, I need to rendor the table row as a placeholder. If there is no data I want the row to collapse.
In the html below, visibility:hidden is working since I won't see the label 'Condition:', but the row doesn't collapse. I've tried looking at it in FireFox 13, Safari 5 and IE 8. All three show the same problem - the row never ccollapses even though it doesn't display anything.
#data
{
font-size: 95%;
}
#data table
{
border-collapse: collapse;
margin-top: 15px;
margin-bottom: 15px;
}
#data table td
{
padding-left: 5px;
}
<div id="data">
....
<table>
<tr style="visibility:hidden;">
<td><div class="datalabel">Condition:</div></td>
<td class="datainfo"></td>
</tr>
</table>
....
</div>
What more do I need to do to make this happen? I'd like it to be cross-browser compatible. I'm trying to support IE7 and above. I'm guessing someone is going to give me hell for using a table in the first place... ;)
The visibility property determines whether a given element is visible
or not (visibility="visible|hidden"). However, when visibility is set
to hidden, the element being hidden still occupies its same place in
the layout of the page.
Display VS Visibility
use display:none; to hide and display:block; to show
<table style="border-collapse:collapse;">
<tr style="display:none;">
<td><div class="datalabel">Condition:</div></td>
<td class="datainfo"></td>
</tr>
</table>
Note: border-collapse:collapse; is used in a situation, where you have borders specified for container and the contained and you want border to be displayed once.
<table border="0" cellpading="0" cellspacing="0">
and try to use and &nbps; or something like that, if you don't have data in a cell
something like:
<table border="0" cellpading="0" cellspacing="0">
<tr style="visibility:hidden;">
<td><div class="datalabel">Condition:</div></td>
<td class="datainfo"> </td>
</tr>
</table>

html table td's and tr's disappearing

I'm going though something quite weird. I was working on a chat system with the rows and stuff based on tables, but the formatting kept messing up. I wondered why until I looked at the part of the source which was not working, which looked like this:
<table border="0">
<tbody>
<tr class="chatline" style="background:white;border-style:none;border-top:1px solid grey;padding:0px;">
<td style="background:#A0D7FF;margin:0px;width:1%;"><span style="padding:2px;background:#A0D7FF;color:black;height:100%;border-right:1px solid grey;">kpsuperplane</span></td>
<td style="color:black;background:white;"><span style="color:black;padding:2px;">test</span></td>
</tr>
<tr class="chatline" style="background:white;border-style:none;border-top:1px solid grey;padding:0px;">
<td style="background:#A0D7FF;margin:0px;width:1%;"><span style="padding:2px;background:#A0D7FF;color:black;height:100%;border-right:1px solid grey;">kpsuperplane</span></td>
<td style="color:black;background:white;"><span style="color:black;padding:2px;">test</span></td>
</tr>
</tbody>
</table>
However, when I view it through dev tools in chrome, I get this:
<table border="0">
<tbody>
<span style="padding:2px;background:#A0D7FF;color:black;height:100%;border-right:1px solid grey;">kpsuperplane</span>
<span style="color:black;padding:2px;">test</span>
<span style="padding:2px;background:#A0D7FF;color:black;height:100%;border-right:1px solid grey;">kpsuperplane</span>
<span style="color:black;padding:2px;">test</span>
</tbody>
</table>
Any idea why this is happening? The td's and tr's are automatically removed from the document when they are rendered. And this is not chrome specific. Live code in dreamweaver gives the same puzzling result.
Pic below:
I tested this in jsfiddle and it doesn't seem to be a problem. I also tested it in my own environment (chrome) and it works fine. Try looking for an unclosed tag in code above the table.
Edit:
Paste the code into w3c validator http://validator.w3.org/check. I found 13 errors/warning in the html. Check out the errors and the specific line numbers.
I found that you have div tags within the table, but they are not wrapped by a tr. I'm sure you'll be able to find the rest within the validators output.
According to your jsfiddle: you have simple mistake in HTML structure, here is copy paste;
</tr>
</table>
</div>
<tr class="chatline" style="background:white;border-style:none;border-top:1px solid grey;padding:0px;">
you close table and then you do not open it. ctrl + f and type /table. I suggest you just going carefully through it and make valid html ;)
Sorry it is not an answer, but it won't let me comment.
I tried same code in chrome using Dev Tools it is showing me tr and td's, so I am not what is happening in your case. I enclosed above code into html and body tags.