This link is my html's code,I use bootstrap to design my page.
I can't reduce the width of "th" and "td",I tried to use this code style="width:50px" to reduce the width of "th":
<th class="text-center" style="width:50px">JAN</th>
And this code style="width:50px" to reduce "td":
<td class="text-center" style="width:50px">
<input type="text">
</td>
But there is no effect,the "th" and "td" element still very wide,like this picture:
How to reduce the width of "th" and "td"?
The input elements are breaking your table layout.
Set the input elements inside the td as width: 100%
table td input {
width: 100%;
}
See fiddle: https://jsfiddle.net/pfxcswy2/9/
At the top th tag add width='1'
This will make the column width depending on rows content
<th width='1' class="text-center" style="width:50px">JAN</th>
You can try table-condensed class in your table.
The .table-condensed class makes a table more compact by cutting cell padding in half.
<table border="1" class="table table-bordered text-right table-condensed">
<thead>
<tr bgcolor="#337ab7">
<th class="text-center">No</th>
<th class="text-center">JAN</th>
<th class="text-center">name</th>
<th class="text-center">size</th>
<th class="text-center">except</th>
<th class="text-center"></th>
</tr>
</thead>
<tbody>
This is a quick solution. See if this helps otherwise we will have to write our own css class with higher specificity.
This behavior is due to the bootstrap's styling of th and elements which is given the width of 10%.
.table>thead>tr>th {
width: 10%;
}
All we need to do is use our own style's to override the bootstrap style.
use this code in your external style sheet.It reduces the width to minimum.
Do the same for td's with small modifications according to your need.
CSS code:[ To have the same behaviour through out use below CSS code]
.table>tbody>tr>td, .table>tbody>tr>th, .table>tfoot>tr>td,
.table>tfoot>tr>th, .table>thead>tr>td, .table>thead>tr>th {
width:0% !important;
}
But this change will be overrided everywhere you use table.Better is to give some class name for the particular row you want your new styles on.
HOPE THIS WILL HELP !
Related
Hi Im using html tables + css properties as said in my school textbook(were learning this in class). Basically the error I have is the fact that the layout is not making much of a difference to the dimensions of the table which I have specified in the format.
See the code for more references:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Employees table</title>
<style>
body{background-color:RGB(255,251,214)}
table,td,tr,th{border:1px solid blue}
tr{hover:background-color:blue}
th{text-align:center}
table{table-layout:fixed;}
td{vertical-align:middle}
h2{color:red}
</style>
</head>
<body>
<h2>Table layout fixed</h2>
<table style="table-layout:auto">
<tr>
<th width=" 40%">Emp-id</th>
<th width = 30%>Name</th>
<th width = 17%>Basic</th>
<th width = 28%>HRA</th>
</tr>
<tr><td>101</td><td>Ajay</td><td>20000000000000</td><td>300</td></tr>
<tr><td>102</td><td>Shashi</td><td>15000000000000000</td><td>2800</td></tr>
<tr><td>103</td><td>Mukesh</td><td>130000</td><td>2000</td></tr>
</table>
<h2>Table layout auto</h2>
<table style="table-layout:auto">
<tr>
<th width= "40%">Emp-id</th>
<th width = 30%>Name</th>
<th width = 10%>Basic</th>
<th width = 28%>HRA</th>
</tr>
<tr><td>101</td><td>Ajay</td><td>20000000000000</td><td>300</td></tr>
<tr><td>102</td><td>Shashi</td><td>15000000000000000</td><td>2800</td></tr>
<tr><td>103</td><td>Mukesh</td><td>130000</td><td>2000</td></tr>
</table>
</body>
</html>
And also ps can someone give me a more clear definition of what are table layouts
There are 2 mistakes in your code.
Your total percent is more than 100%, which will render the table wrong than you intended.
More importantly, you had override table-layout in both tables to auto, so table-layout: fixed won't work, thus you can't see the different.
Now with that corrected, you will see the string data you wrote in the td of fixed table get overflow to outside, and with layout auto, the cell will just expand till it force all other cell to minimum width, and then make the table larger to fit the content. To fix this, you can use word-break: break-all in td to force break the string to fit the cell's defined width.
Oh and you wrote :hover wrong too so it won't work, fixed it in the demo for you.
body{background-color:RGB(255,251,214)}
table,td,tr,th{border:1px solid blue}
tr:hover{background-color:blue}
th{text-align:center}
table{table-layout:fixed;width: 100%;}
td{vertical-align:middle; word-break: break-all}
h2{color:red}
<table style="">
<tr>
<th width="35%">Emp-id</th>
<th width="30%">Name</th>
<th width="17%">Basic</th>
<th width="18%">HRA</th>
</tr>
<tr><td>101</td><td>Ajay</td><td>20000000000000</td><td>300</td></tr>
<tr><td>102</td><td>Shashi</td><td>150000000000000000000000000000000000000000000000000000000000000000000000000000000000</td><td>2800</td></tr>
<tr><td>103</td><td>Mukesh</td><td>130000</td><td>2000</td></tr>
</table>
<h2>Table layout auto</h2>
<table style="table-layout:auto">
<tr>
<th width= "30%">Emp-id</th>
<th width ="40%">Name</th>
<th width ="10%">Basic</th>
<th width ="20%">HRA</th>
</tr>
<tr><td>101</td><td>Ajay</td><td>20000000000000</td><td>300</td></tr>
<tr><td>102</td><td>Shashi</td><td>150000000000000000000000000000000000000000000000000000000000000000000000000000000000</td><td>2800</td></tr>
<tr><td>103</td><td>Mukesh</td><td>130000</td><td>2000</td></tr>
</table>
I have an html table with table-layout: fixed and a td with a set width. The column still expands to hold the contents of text that doesn't contain a space. Is there a way to fix this other than wrapping the contents of each td in a div?
Example: http://jsfiddle.net/6p9K3/29/
<table>
<tbody>
<tr>
<td style="width: 50px;">Test</td>
<td>Testing 1123455</td>
</tr><tr>
<td style="width: 50px;">AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA</td>
<td>B</td>
</tr>
</tbody>
</table>
table
{
table-layout: fixed;
}
td
{
border: 1px solid green;
overflow: hidden;
}
In the example, you can see that the column with AAAAAAAAAAAA... expands despite being explicitly set to 50px wide.
Specify the width of the table:
table
{
table-layout: fixed;
width: 100px;
}
See jsFiddle
Try looking into the following CSS:
word-wrap:break-word;
Web browsers should not break-up "words" by default so what you are experiencing is normal behaviour of a browser. However you can override this with the word-wrap CSS directive.
You would need to set a width on the overall table then a width on the columns. "width:100%;" should also be OK depending on your requirements.
Using word-wrap may not be what you want however it is useful for showing all of the data without deforming the layout.
Make the table rock solid BEFORE the css. Figure your width of the table, then use a 'controlling' row whereby each td has an explicit width, all of which add up to the width in the table tag.
Having to do hundreds html emails to work everywhere, using the correct HTML first, then styling w/css will work around many issues in all IE's, webkit's and mozillas.
so:
<table width="300" cellspacing="0" cellpadding="0">
<tr>
<td width="50"></td>
<td width="100"></td>
<td width="150"></td>
</tr>
<tr>
<td>your stuff</td>
<td>your stuff</td>
<td>your stuff</td>
</tr>
</table>
Will keep a table at 300px wide. Watch images that are larger than the width by extremes
You can add a div to the td, then style that. It should work as you expected.
<td><div>AAAAAAAAAAAAAAAAAAA</div></td>
Then the css.
td div { width: 50px; overflow: hidden; }
You can also use percentages, and/or specify in the column headers:
<table width="300">
<tr>
<th width="20%">Column 1</th>
<th width="20%">Column 2</th>
<th width="20%">Column 3</th>
<th width="20%">Column 4</th>
<th width="20%">Column 5</th>
</tr>
<tr>
<!--- row data -->
</tr>
</table>
The bonus with percentages is lower code maintenance: you can change your table width without having to re-specify the column widths.
Caveat: It is my understanding that table width specified in pixels isn't supported in HTML 5; you need to use CSS instead.
You can also work with "overflow: hidden" or "overflow-x: hidden" (for just the width). This requires a defined width (and/or height?) and maybe a "display: block" as well.
"Overflow:Hidden" hides the whole content, which does not fit into the defined box.
Example:
http://jsfiddle.net/NAJvp/
HTML:
<table border="1">
<tr>
<td><div>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</div></td>
<td>bbb</td>
<td>cccc</td>
</tr>
</table>
CSS:
td div { width: 100px; overflow-y: hidden; }
EDIT: Shame on me, I've seen, you already use "overflow". I guess it doesn't work, because you don't set "display: block" to your element ...
I would try setting it to:
max-width: 50px;
This works for me
td::after {
content: '';
display: block;
width: 30px;
}
I have a table inside some other HTML elements and it renders strange - the border space appears even if I set it in css as collapsed.
The table looks like this:
and the html is this:
<table cellspacing="0" border="1" class="scrollable-headers-only" title="" cellpading="0">
<thead title="">
<tr title="">
<th class="check" title=""></th>
<th class="type" title=""></th>
<th class="name" title="">Name</th>
<th class="address center-column" title="">Address</th>
<th class="domain" title="">Domain</th>
<th class="status" title="">Status</th>
</tr>
</thead>
</table>
and the css, as it appears in FireBug is this:
for the main table:
for each th:
I added border only too see the s[aces between cells. My issue is to eliminate that space. I added cellpading and cellspacing = 0 to the table, but doesn't make difference. I added css to eliminate that space, without any change in rendering.
This table is used as header for another table, and I need to align it's columns to the other one - as you can see from image, there are small lines under the table. I need to align this table with the second one, for this reason is important to get 100% precision.
What's wrong?
Thank you.
I would start with removing display: inline-block; for th and table elements. Firefox treats those as inline elements and inserts extra space. Another way to eliminate it is to set font-size: 0; for table and in example font-size: 12px; for th element.
It seems align is not working for the th element. Here is my HTML:
<div style="width: 100%; height: 175px; overflow: auto;">
<table class="grid" id="table">
<thead>
<tr>
<th class="not_mapped_style" style="display: none;" align="center">id</th>
<th class="not_mapped_style" align="center">DisplayName</th>
<th align="center">PrimaryEmail</th>
<th align="center">Age</th>
<th align="center">Phone</th>
</tr>
</thead>
<caption>Contacts</caption>
<tbody>
<tr>
<td style="display: none;" property_value="0" property_name="id" align="center">0</td>
<td property_value="rpcuser" property_name="DisplayName" align="center">rpcuser</td>
<td property_value="admin#example.com" property_name="PrimaryEmail" align="center">admin#example.com</td>
<td property_value="69" property_name="Age" align="center">69</td>
<td property_value="+722616807" property_name="Hand_Phone" align="center">+18007</td>
</tr>
</tbody>
</table>
</div>
Text aligning is working just fine for the td elements but fails for the th. Why?
Try:
text-align: center;
You may be familiar with the HTML align attribute (which has been discontinued as of HTML 5). The align attribute could be used with tags such as
<table>, <td>, and <img>
to specify the alignment of these elements. This attribute allowed you to align elements horizontally. HTML also has/had a valign attribute for aligning elements vertically. This has also been discontinued from HTML5.
These attributes were discontinued in favor of using CSS to set the alignment of HTML elements.
There isn't actually a CSS align or CSS valign property. Instead, CSS has the text-align which applies to inline content of block-level elements, and vertical-align property which applies to inline level and table cells.
Try to use text-align in style attribute to align center.
<th class="not_mapped_style" style="text-align:center">DisplayName</th>
Try using style for th
th {text-align:center}
If you want to center the th of all tables:
table th{ text-align: center; }
If you only want to center the th of a table with a determined id:
table#tableId th{ text-align: center; }
In HTML5, the easiest, and fastest, way to center your <th>THcontent</th> is to add a colspan like this:
<th colspan="3">Thcontent</th>
This will work if your table is three columns. So if you have a four-column table, add a colspan of 4, etc.
You can manage the location furthermore in the CSS file while you have put your colspan in HTML like I said.
th {
text-align: center; /* Or right or left */
}
HTML:
<tr>
<th>Language</th>
<th>Skill Level</th>
<th> </th>
</tr>
CSS:
tr, th {
padding: 10px;
text-align: center;
}
For inline css (which which you really shouldn't do), you should do it like this:
<th style="text-align: center;">I'm a heading!</th>
The answers provided by others above tell you how to do it in a css file, which is a better practice. Using inline styling can make things harder to read and harder to expand. Not to mention, harder to debug. (gotta maintain separation of concerns).
Your code works, but it uses deprecated methods to do so. You should use the CSS text-align property to do this rather than the align property. Even so, it must be your browser or something else affecting it. Try this demo in Chrome (I had to disable normalize.css to get it to render).
Source: http://jsfiddle.net/EDSWL/
Demo: http://jsfiddle.net/EDSWL/show
For me none of the above worked. I think it is because I have two levels of header and a fixed width on level 1. So I couldn't align the text inside the corresponding columns on level 2.
+---------------------------+
| lvl 1 |
+---------------------------+
| lvl 2 col a | lvl 2 col b |
+---------------------------+
I had to use the combination of width:auto and text:align-center :
<th style="width:auto;text-align:center">lvl 2 col a</th>
<th style="width:auto;text-align:center">lvl 2 col b</th>
I had to put my th text inside a div with display: flex in order for the text to be middle aligned.
<th style="width: 15%;">
<div style="width: 100%; display: flex; flex-direction: row; align-items: center; justify-content: center">
Blah blah blah
</div>
</th>
Check out this fiddle:
http://jsfiddle.net/foreyez/hQ9ZR/
<table background='red'>
<thead>
<tr>
<th style='width:3000px;background:red'>Students</th>
</tr>
</thead>
<tbody>
<tr>
<td>Joe</td>
</tr>
<tr>
<td>Shmoe</td>
</tr>
</tbody>
and the css is:
html,body { overflow:auto; }
When I make the width of the Header 3000px I'd expect it to cause an overflow (scrollbar) in the page. But it doesn't. It's because the width of the header cell doesn't change the overall table's width.
If you set the width of the table to 3000px, you'll see the overflow (scrollbar)
So basically, I'm wondering if there's a way to make it so the table's width is defined by it's headers widths?
Thanks
First of all this is not the way you style tables these days
<table background='red'>
use this instead
<table style="background:red;">
And secondly you need to use min-width for th instead of width
My Fiddle
<th style='border: 1px solid #fffff0;min-width:5000px;background:red'>Students</th>