I want to create a squared table 9x9, width and height should be 100%, and each cell should be 11.11% height and width. I could do it with below code but when the text inside the cell is too large the cell grows down with it. I don't want that. I just simply want the text to be hidden. Preserve the size of the table is my priority ;)
This is what I wrote:
<div class="full_screen">
<table border="1" class="full_width full_height">
<tr class="cell_row">
<td class="cell">long long long long long text</td>
<td class="cell">2</td>
<td class="cell">3</td>
<td class="cell">4</td>
<td class="cell">5</td>
<td class="cell">6</td>
<td class="cell">7</td>
<td class="cell">8</td>
<td class="cell">9</td>
</tr>
... other 8 rows and its cells are similar...
the CSS
body {
font-size: 9px;
font-family: Helvetica, Verdana, Arial, sans-serif;
background-color: rgba(0,0,0,0);
height: 100%;
}
div.full_screen {
display: block;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
.cell_row {
width: 100%;
height: 11%;
}
.cell {
width: 11%;
/*height:100%;*/ /*cell height should be always 11% the height of the hole table*/
margin: 0px;
padding: 0px;
word-break: break-all;
}
How can I clip the content of the cell so cell's height will be always 11% (100%/9 =11.11%) of the table?
You have several problem here.
First, tables are no designed to work that way, they expand horizontally, not vertically, so they will never respect a height:11% per row.
Second, TR styles are ignored, so you can safely remove them.
Third, TD's ignore height for the same reason explained on the first point.
BUT, there is a workaround to the third point, you can use line-height to force a TD height or a nested element (ie DIV) with proper height. But that stills leaves you with a problem, no way to get the height as a 11% of the total document/window height.
What you can do is use some JavaScript to update the TD height (using the workaround explained above) on page load (and update on resize).
You can add
table-layout:fixed;
word-wrap:break-word;
to your table class. It will ensure that your td width will not exceed given %. It will automatically wrap your content appropriately.
As per I can see it can be fixed using following two way:
You can add font-size: 11px; .cell class
Are you getting this data from back-end or if you are displaying text using JSP or something. If you know how many letters you have to display you can get substring and append ... (ellipse). Like you string will be long long long ...
If your application allows you.
Related
I have a page which contains a table. On the same page, there is a footer and side bar. I've manipulated the table such that the width is how I like it, however I cannot seem to use the same method for height.
This is what my page currently looks like:
There are many more table items that continue to go down, however as you can see, the items are being cut off by the footer. When I set the height to an explicit value such as 300px I get the effect that I am looking for:
where there is a scrollbar. The problem with this is that it is not dynamic. For example, if I make the window size larger vertically, the table height is not increased with it. I've tried using % values such as 80% but it does not seem to affect anything. I want to be able to resize the window and have the height of the table increase/decrease with the resize so that it fits over the whole screen except the footer. How can I achieve this?
EDIT
Here is my source code (ReactJS):
HTML
<table className='task-table'>
<tbody>
<tr className='tasks-header'>
<th>Store</th>
<th>Product</th>
<th>Size</th>
<th>Profile</th>
<th>Proxies</th>
<th>Status</th>
<th>Actions</th>
</tr>
{this.renderTaskData()}
</tbody>
</table>
SASS
.task-table {
margin-left: 85px;
margin-top: 30px;
border-spacing: 0 10px;
width: calc(100% - 107px);
user-select: none;
display: block;
overflow-y: auto;
overflow-x: hidden;
height: 300px;
tbody {
display: table;
width: 100%;
}
}
.tasks-header {
font-family: 'Muli', sans-serif;
font-size: 12px;
font-weight: $extra-bold;
color: $text-light;
letter-spacing: 1px;
text-align: left;
th {
padding-left: 36px;
}
}
Would you like to share your source code? Meanwhile, perhaps you should try nesting the table in a div since divs are block-level elements and then control the size of the div with % or vh. Your table should reflow according to the height of this container div.
I have the following table:
<table>
<tbody>
<tr>
<td colspan="2">
<img src="http://www.sherv.net/cm/emoticons/cats/cat-headphones-smiley-emoticon.gif" />
<img src="http://www.beaukit.com/catgrpbl.jpg" />
</td>
</tr>
</tbody>
</table>
and the following CSS:
table {
width: 40%;
background-color:grey;
text-align: center;
margin-bottom: 20px;
}
img {
max-width: 100%;
margin: 0 0 0.8em 0;
max-height: 800px !important;
width: auto !important;
height: auto;
}
See Jsfiddle
What I want is that the pictures in the cell fill its width if they are bigger than the cell itself. In case the pictures are smaller, they should keep maintain their native width expressed via max-width. This seems to work well in Chrome, but when I try it in firefox the bigger pictures stretch the width of the cell.
While, if I change the width of the images to: width: 100% !important;, the smaller picture are streched to fill the cell (see table.two).
How can I solve the issue?
try adding
table {
table-layout:fixed;
}
Table cells don't behave like block elements, their widths and heights are defined by the content inside them. From the I.E. documentation:
auto: Default. Column width is set by the widest unbreakable content in the column cells.
fixed: Table and column widths are set either by the sum of the widths on the col objects or, if these are not specified, by the width of the first row of cells. If no width is specified for the table, it renders by default with width=100%.
You have use width auti !important which you should not be to
table tr, table tr td{
width:100%
}
table tr td a{
width:100%;
display:block;
}
img {
width: 100%;
}
Check on Fiddle
This question has been asked several times, but none of the answers provided seem to help me:
See this in action here: http://jsfiddle.net/BlaM/bsQNj/2/
I have a "dynamic" (percentage based) layout with two columns.
.grid {
width: 100%;
box-sizing: border-box;
}
.grid > * {
box-sizing: border-box;
margin: 0;
}
.grid .col50 {
padding: 0 1.5%;
float: left;
width: 50%;
}
In each of these columns I have a table that is supposed to use the full column width.
.data-table {
width: 100%;
}
.data-table td {
white-space: nowrap;
text-overflow: ellipsis;
overflow: hidden;
}
My problem is that some of the columns in that table have content that needs to be truncated to fit in the given width of the table. That does not happen, though. I get two tables that are overlaying each other.
Requirements:
Needs to be percentage based. I can't set absolute sizes.
Each rows' height must not grow beyond one text line (which would happen if I remove white-space: nowrap)
Must work in Chrome, Firefox and Internet Explorer 8+
Can't display tables below each other as it has to fit onto one sheet of paper when printing.
What I tried:
inside of and use width and overflow on that. Changed nothing.
"display: table;" on containing div - instead of having two columns the tables were displayed below each other
"table-layout: fixed;" - Forced all columns to have same width
I know that columns 2+3 have a total of 30% of width so I tried to manually set column 1 to 70% - Did not change anything
Zero-width spaces in content - didn't change anything, probably due to white-space: nowrap;
Related Questions:
Table width exceeds container's width
How do I prevent my HTML table from stretching
HTML CSS How to stop a table cell from expanding
Table Overflowing Outside of Div
you need to add the table-layout property:
table-layout: fixed;
also include width=100% in the table HTML tag, not just the style tag.
http://jsfiddle.net/reeK5/
Maybe you'll be interested in a max-width: 0; hack I've discovered.
It has some limits, we should use CSS tables instead of HTML, but it works:
.leftBlock
{
width: 100%;
max-width: 0;
word-wrap: break-word;
overflow: hidden;
}
.rightBlock
{
width: 200px;
max-width: 200px;
min-width: 200px;
}
http://jsfiddle.net/CyberAP/NUHTk/103/
.div {
width:300px;
border:1px solid;
}
.breaked {
word-break: break-all;
}
table{
border:1px solid red;
}
td {
border:1px solid green;
}
<div class="div">
<table>
<tr>
<td>aaaaaaa_________________________________________-sdasd-ad-f-asfas-df-a a-sd-fa-d-ad-fa-ds-asd-a-ads-fa-df-ads-fa-d-fad-f-ad-fad-ad-sa-fda-df-</td>
<td>13</td>
</tr>
<tr>
<td>ddddddddddddd</td>
<td>aa</td>
</tr>
</table>
<br /><hr/><br />
<table class="breaked">
<tr>
<td>aaaaaaa_________________________________________-sdasd-ad-f-asfas-df-a a-sd-fa-d-ad-fa-ds-asd-a-ads-fa-df-ads-fa-d-fad-f-ad-fad-ad-sa-fda-df-</td>
<td>13</td>
</tr>
<tr>
<td>ddddddddddddd</td>
<td>aa</td>
</tr>
</table>
</div>
Measurements on tables work differently. In general, width on a table cell is handled as min-width.
One solution, if you don't mind adding extra markup, is to put a div inside each table cell in which you put the content. Then give this div a width, or a max-width. So
<td>http://www.xxxxxx.xxxxxxx.com/xxx_xxxx/XXXXXXXX/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/XXXXXXXX_xXxxxx</td>
becomes
<td><div>http://www.xxxxxx.xxxxxxx.com/xxx_xxxx/XXXXXXXX/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/XXXXXXXX_xXxxxx</div></td>
and so on.
See updated fiddle: http://jsfiddle.net/bsQNj/4/
Edit: I see the fiddle needs some work - I forgot to put some divs in where they were necessary. But I hope you can work with this idea.
In your CSS:
table {
table-layout: auto;
width: 100%;
}
That should cover all tables
I need a header that is width:100% and has 3 columns with background images in the 1st and 3rd column:
This solution needs to be cross-browser compatible.
First column is a background image and is 50% width (not including width of caption)
Second column is the caption. This has no background (transparent). It's width should not be any greater than it's contents.
Third column is same as first column.
Using a table this takes 2 seconds: http://jsfiddle.net/aLeyS/
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td></td>
<td>Caption</td>
<td></td>
</tr>
table {
width: 100%;
}
table td:first-child, table td:last-child {
width: 50%;
background-image: url(http://fc01.deviantart.net/fs16/i/2007/132/9/4/BW_Striped_Background_Texture_by_Enchantedgal_Stock.jpg);
}
table td:nth-child(2) {
padding: 0 10px;
font-size: 30px;
}
Without a table this seems to be much trickier.
I've tried using set percentage widths on DIVs inside the parent div, but it always ends up giving the center column more width than it needs, or forcing the caption to wrap if it's not enough percentage.
Again, the center column (caption) should not have any width greater than its content, and it's background needs to be transparent (not white).
You can fix this by setting display:table-cell on the divs. I've updated your jsFiddle.
HTML
<div class='bg'></div>
<div class='caption'>Caption</div>
<div class='bg'></div>
CSS
div {
display: table-cell;
}
div.bg {
width: 50%;
background-image: url(http://fc01.deviantart.net/fs16/i/2007/132/9/4/BW_Striped_Background_Texture_by_Enchantedgal_Stock.jpg);
}
div.caption {
padding: 0 10px;
font-size: 30px;
text-transform: uppercase;
}
Is there a way using HTML/CSS (with relative sizing) to make a row of cells stretch the entire width of the table within which it is contained?
The cells should be equal widths and the outer table size is also dynamic with <table width="100%">.
Currently if I don't specify a fixed size; the cells just autosize to fit their contents.
You don't even have to set a specific width for the cells, table-layout: fixed suffices to spread the cells evenly.
ul {
width: 100%;
display: table;
table-layout: fixed;
border-collapse: collapse;
}
li {
display: table-cell;
text-align: center;
border: 1px solid hotpink;
vertical-align: middle;
word-wrap: break-word;
}
<ul>
<li>foo<br>foo</li>
<li>barbarbarbarbar</li>
<li>baz</li>
</ul>
Note that for table-layout to work the table styled element must have a width set (100% in my example).
Just use percentage widths and fixed table layout:
<table>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
</table>
with
table { table-layout: fixed; }
td { width: 33%; }
Fixed table layout is important as otherwise the browser will adjust the widths as it sees fit if the contents don't fit ie the widths are otherwise a suggestion not a rule without fixed table layout.
Obviously, adjust the CSS to fit your circumstances, which usually means applying the styling only to a tables with a given class or possibly with a given ID.
Using table-layout: fixed as a property for table and width: calc(100%/3); for td (assuming there are 3 td's). With these two properties set, the table cells will be equal in size.
Refer to the demo.
Just put this lines to your table style:
#table_name{
table-layout: fixed;
width: 100%;
}
You have a good example on this codepan.
This is late answer, but it will help for future searches.