HTML/CSS Scrollable Spans inside Table TDS - html

I have HTML Like Below. I have a table inside another table.Each TD in the inner table will contain a Scrollable Span defined with a CSS Class as below. The span will have scroll bars and scroll only if the width is defined as a value(like 100px).If I give Width as Auto the scroll bar does not appear and text simply overflows the TD and Table boundaries and spoils the layout. What I have to do is generate this dynamically(from ASP.NET Web Control). The inner table row can have many TDS. So each TD will be generated with a width as 100/#ofTDs %. And each TD will contain a scrollable Span. I cannot set the width on the span.I need it to be the same as the containing TD and scroll the rest by displaying a horizontal scroll bar. Any Ideas ??
.OuterTable
{
Color:Red;
width:120px;
white-space:nowrap;
}
.SpanClass
{
overflow:auto;
display:block;
width:auto;
color:Blue;
}
<table class="OuterTable">
<tr>
<td>Outer Table Column</td>
<td>
<table class="InnerTable">
<tr>
<td><span class="SpanClass"> This is A LINE IN Inner
table .this is the first line . Line 1 line1 jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj</span></td>
</tr>
</table>
</td>
</tr>
</table>

First try: http://jsfiddle.net/zZDvq/
This is similar to what you want to create?

In the serverside code which generates the span, set the width as an inline style, i.e (pseudocode)
<span style = "width:100/#TDs %">

Related

How to control image height to match adjoining text in html email

I have this html in my email:
<li><h4>Sample Text 31 <small><img src="<link to image>"></small></h4></li>
The image is misaligned and the height width of the img tag is way bigger than the text size. See below
How can I keep the image to appear next to text and be of the same size?
To support most email clients, I would put this content in an actual table and you can use the valign attribute if you need to. valign doesn't seem necessary in chrome here, but it might be for outlook. You would add valign="middle" to the td's. You might also be able to put the text and image together in a single td and use valign="middle".
You should be able to nest a table in the li, or just use a table there instead.
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td style="font-size:40px;font-weight:bold;">Sample Text 31</td>
<td><img src="https://upload.wikimedia.org/wikipedia/commons/2/21/Speaker_Icon.svg" width="20" height="20"></td>
</tr>
</table>
You can display the li tag as a table and its children as cells. then vertical align the contents in the middle
snippet below
li {
display:table;
vertical-align:middle;
}
li *{
display:table-cell;
border:solid red;
vertical-align:middle;
}
<li><h4>Sample Text 31 </h4><small><img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTbmbMn9M5hlfXUXtw0a_ZsH3aS0HWvXT0eZiDeFauNmJOInHgm"></small></li>

Two block level elements 1 fixed width 1 stretch inside table

I have a table with multiple columns. In one of the column rows I want to add 2 elements which will be next to each other. One text element and one icon. The icon has a fixed with, the text element needs to be dynamic and has to be truncated with ... when the column cannot stretch anymore.
This is the HTML:
<table>
<tr>
<td>
</td>
<td>
<span>Truncated text goes here</span>
<i class="icn sprite icn-name></i>
</td>
<td>
</td>
</tr>
</table>
How do I do this? Using display: table; will make the HTML all buggy.
As said in comments, if you allow text and image to stay in adiacent cells, you can try the following.
<table>
<tr>
<td>Truncated text goes here</td>
<td><img src="imageURL" /></td>
</tr>
</table>
You can use vertical-align:top; in td style to align text on the top of the cell. And then you can use the following to set image width.
td>img {
vertical-align:top;
display:inline-block;
width:80px;
}
Fiddle
UPDATE
If you don't want to add extra cells to your table, you can create an internal div inside the cell, display it with display:table; property, and then display both span and img with display:table-cell; property.
Fiddle
I added the <i> element in front of the <span> element and gave the <i> element a float: right; and the <span> element the truncate styles.
Works fine now!

Streatch the inner div inside td to span the height of td

I have dynamic text content inside all first tds. So the trs and tds get expanded automatically. Now for all second tds there is div with background image and dont have any text inside. I need this divs with background image to be expanded or collapsed automatically along with the cells in table so that the red image will span over the height of each cell. Any way to do this purely with css as I dont want to introduce script here? jsfiddle
<table border="1">
<tr>
<td>
This content makes the trs and tds to expand/contract horizontally automatically
</td>
<td>
<div class="img"></div>
</td>
</tr>
<tr>
<td>
xyz
</td>
<td>
All divs inside second tds should get expanded automatically which is not happening
</td>
</tr>
</table>
CSS
td
{
width:200px
}
.img{
width:30px;
height:40px;
background: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABMAAAAXCAIAAABmo+ONAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAApSURBVDhPY3gro0IG+v///6hOvGhUJyE0qpMQGtVJCFGmE4jJAf//AwBnlUxAq2HzYQAAAABJRU5ErkJggg==) no-repeat;}
Just set background for that td with 100% width and height.
Fiddle: http://jsfiddle.net/mttSA/
.empty{
background: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABMAAAAXCAIAAABmo+ONAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAApSURBVDhPY3gro0IG+v///6hOvGhUJyE0qpMQGtVJCFGmE4jJAf//AwBnlUxAq2HzYQAAAABJRU5ErkJggg==) no-repeat;
background-size: 100% 100%;
}
HTML:
<td class="empty">
</td>

How to line up a table's th cells to be in the middle (on the border) of the td cells below it

I'm trying to create this table layout. Basically the orange 18 you see in the grid means 18% usage between 11am and 12pm on Tuesday. So that's why the hours along the top are best on the edges of the table cell, not in the middle of the cell. That way it's showing the data representing usage over a one hour time range.
I have basically applied a basic hack and right aligned the hours along the top so they kinda look like they're inbetween the cells. This isn't perfect as you can see.
What I want to do is actually have the hours along the top centered nicely between the data cells. I think I could do it with a fixed size column widths, but the table needs to stretch to 100% of the page width and the column widths a percentage. Then it's scalable down to a smaller browser.
Is there a way to do this in HTML and CSS?
To have the first row truly centered between the bottom cells with a single table you can use colspan + widths in percentages without using positioning. That way it will be fluid, it will work with any font, and it won't get screwed when you use 2 digit numbers.
HTML:
<table border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td> </td>
<th colspan="2">1</th>
<th colspan="2">1</th>
<th colspan="2">1</th>
<th colspan="2">1</th>
<th colspan="2">1</th>
<td> </td>
</tr>
<tr>
<td colspan="2">0</td>
<td colspan="2">0</td>
<td colspan="2">0</td>
<td colspan="2">0</td>
<td colspan="2">0</td>
<td colspan="2">0</td>
</tr>
</tbody>
</table>
CSS:
table {
text-align: center;
width: 70%;
}
table td {
width:8.33%; // 100% divided by (double the number of bottom cells)
}
table th {
width:16.66%; // 200% divided by (double the number of bottom cells)
}
table td[colspan="2"] {
background:yellow;
}
table td,
table th {
outline:1px solid tan;
}
Demo: http://jsfiddle.net/G7KZe/
You could use position: relative; to place your month numbers to be where you want but it's tricky because table cells often behave weirdly with CSS positioning. And the exact positioning can depend on the font used.
I've come up with a solution that requires 2 tables. The idea is to have one table for the headings, one table for the content. The trick is to have 1 cell less in the headings.
Live example: http://jsfiddle.net/w6TnE/
As you can see, the month numbers are perfectly aligned with the borders. But keep in mind that this setup requires a fixed width, in this case, 60px:
td, th{ border:1px solid #ccc; padding:5px 0; text-align:center; width:60px;}
I just added some additional styling to make it clear.
You can use an absolutely positioned element inside a relatively positioned element to get the effect you want. The idea is to style the <th> elements with position: relative and then style the hour numbers themselves in an element with position: absolute. You can then position the numbers anywhere you want in relation to the cell.
Here is an example jsfiddle. To adjust the position of the numbers you may want to use a pixel value instead of a percentage for the right property in the th > span block.
For more information, you might want to read about the different positioning methods.
table td{ text-align:center;}
This will align the text of each cell to the center.
You could always wrap each of the table heading text in like a <div> tag and use the css position:relative and left:2px or whatever number of pixels to make it look good.
example
<table>
<tr>
<th style="text-align:right;"><div style="position:relative;left:2px;">1</div></th>
</tr>
</table>

Table doesn't want to stretch inside div

I have a <table> inside a <div> tag, which doesn't want to span as long as it needs to be. I need to specify a width in px for the <table> to span and thus cause the <div> container it is inside to scroll. Otherwise, the <table> just spans to a width of 100%, i.e. the width of the <div>.
My example is as follows:
<div style="width:880px; overflow:scroll;">
<table> // I need to explicitly specify a width for it, otherwise it just span 100% which is incorrect
<tr><td></td><td></td><td></td></tr>
</table>
</div>
I have specified for all the <td> tags a width inside my CSS.
I don't understand why the <table> can't determine it's own width or just respect the widths of all the <td> tags.
Try setting white-space: nowrap; on the td in your table and dump a lot of text inside each td you will start seeing a scroll bar on your div.
Are you sure there isn't any unintended CSS being applied to the table? By default the table only expands to accommodate its columns.
<div style="width:880px; overflow:scroll; background-color: green;">
<table style="background-color: red;">
<tr>
<td>one</td>
<td>two</td>
</tr>
</table>
</div>
Using this code, you can see the red table is only as big as its columns in relation to the green div as long as no other CSS is involved.
Use a tool like Firebug or IE's Developer Tools (F12) to see which styles are actually being applied to the table element.
See the example here.