I need to reduce the whitespace between Name: and the "Bob",similary between Age: and "20".
How can I accomplish using css.
fiddle:
http://jsfiddle.net/QfN3f/2/
html:
<table class="table">
<tr class="table-row">
<td class="table-col">Name:</td>
<td class="table-col">Bob</td>
<td class="table-col">Age:</td>
<td class="table-col">20</td>
</tr>
</table>
css:
.table {
width:100%
}
I'd add a class to your "labels"
<table class="table">
<tr class="table-row">
<td class="table-col label">Name:</td>
<td class="table-col">Bob</td>
<td class="table-col label">Age:</td>
<td class="table-col">20</td>
</tr>
</table>
CSS
.table {
width:100%
}
.label {width:50px; font-weight:bold;}
http://jsfiddle.net/QfN3f/5/
You could also use n-th child.
tr td:nth-child(odd) {width:50px; font-weight:bold;}
http://jsfiddle.net/QfN3f/6/
I would also re-consider the appropriatness of a table here as it may not actually be tabular data. Maybe consider a definition list.
If you must use a table for this, I'd suggest (assuming that your label td elements are always the odd-numbered elements):
td:nth-child(odd) {
text-align: right;
}
td:nth-child(even) {
text-align: left;
}
JS Fiddle demo.
But, I'd strongly suggest moving to more semantic HTML, such as a definition list:
<dl>
<dt>Name</dt>
<dd>Bob</dd>
<dt>Age</dt>
<dd>20</dd>
</dl>
With the CSS:
dt, dd {
display: inline-block; /* allows the elements to be side-by-side */
width: 5em;
margin: 0; /* removes the default margins */
padding: 0.2em 0; /* aesthetics, but sets/overrides the defaults */
}
dt {
text-align: right; /* to move the 'label' towards the 'value' */
}
dd {
text-align: left;
text-indent: 0.5em; /* aesthetic, gives a small separation; adjust to taste */
}
JS Fiddle demo.
The gaps are so wide because table cells will expand to take up as much space as is available. To reduce the whitespace, stop stretching the table all the way across the parent div. Simply remove width:100% from the table.
Of course, you may want to simultaneously add a min-width to the cells, so that the columns don't get too close together.
Add a % or fixed width to the column:
<td class="table-col" width="20px">Name:</td>
or
HTML
<td class="table-col name-col">Name:</td>
CSS
.name-col{
width:20px;
}
If no width per column is specific the columns width is the same for each one.
If you want to reduce to the gaps space you may also want to reduce the table width by removing width:100% and writing a fixed width or smaller one or writing a fixed width to all the columns.
Jsfiddle
You can remove width:100% and specify padding for table cells (td).
.table td {
padding: 10px
}
jsfiddle
Also if you want to customize some individual cells you can add different class to some <td class="myCustomClass"> and assign different width/padding or whatever property you want in css.
Use this html code:
<table class="table">
<tr class="table-row">
<td class="table-col-a">Name:</td>
<td class="table-col-b">Bob</td>
<td class="table-col-a">Age:</td>
<td class="table-col-b">20</td>
</tr>
</table>
And this css code:
.table-col-a{float:right;}
Also you can use paddings to make it exactly as you want.
See working jsFiddle demo
Simply modify your HTML to:
<table class="table">
<tr class="table-row">
<td class="table-cola">Name:</td>
<td class="table-colb">Bob</td>
<td class="table-cola">Age:</td>
<td class="table-colb">20</td>
</tr>
</table>
and your CSS to:
.table
{
width:100%
}
.table td
{
width: 25%;
}
.table-cola
{
text-align: right;
}
.table-colb
{
text-align: left;
}
that will yield:
Related
I want that a table column uses the minimum of place but after a certain width, it should wrap.
Here is what I have:
<table id="#table">
<tr>
<td>Content with a fix with</td>
<td class="min">This content should only use the necessary place but wrap after 200px</td>
</tr>
</table>
<style>
#table{
width: 100%;
}
.min {
width: 1%;
white-space: nowrap;
}
</style>
This makes the column use only the place which it needs but it it ets too long, it will make the table endless longer and I want to set the maximum of the allowed space.
Edit:
I think that with js it would be possible to calculate the width and change the css but I would prefer a solution without javascript or jquery.
Edit 2: I forgot to mention that the table has a width of 100%. So if I follow the given answer, the table cell has its autowidth (which is too wide) and a max-width so if the text is short, the td has a white space which I do not want.
Edit 3: Here is an image which explains better than me.
#billTable{
width:100%;
}
#numberTd{
text-align: center;
width:18%;
}
#weightTd{
text-align: center;
width:20%;
}
#nameTd{
text-align: left;
width: 1%;
white-space: nowrap;
}
#kgPriceTd{
text-align: right;
width:20%;
}
#priceTd {
text-align: right;
}
<div style="width:550px;">
<table>
<tr>
<th id='numberTd'>Packetzahl</th>
<th id='weightTd'>Kg</th>
<th id='nameTd'>Artikel</th>
<th id='kgPriceTd'>CHF / Kg</th>
<th id='priceTd' colspan="2">Total</th>
</tr>
<tr>
<td id='numberTd'>1</td>
<td id='weightTd'>0.688</td>
<td id='nameTd' class='min'>Siedfleisch</td>
<td id='kgPriceTd'>44.00</td>
<td id='priceTd'>8.2</td>
</tr>
</table>
</div>
You can control max width of an element by using max-width
https://developer.mozilla.org/en-US/docs/Web/CSS/max-width
The max-width CSS property sets the maximum width of an element. It prevents the used value of the width property from becoming larger than the value specified by max-width.
.min {
max-width: 200px;
}
<table>
<tr>
<td>Content with a fix with</td>
<td class="min">This content should only use the necessary place but wrap after 200px</td>
</tr>
</table>
You can apply width in percentages for the flexible columns, space it out so it looks good or make your table not 100%
Use the CSS property "max-width". I've attached an example with a border that shows this in use.
table, td {
border:1px solid #555;
border-collapse:collapse;
}
.min {
max-width:200px;
}
<table>
<tr>
<td>Content with a fix with</td>
<td class="min">This content should only use the necessary place but wrap after 200px</td>
</tr>
</table>
CSS:
td, table {
border:
1px solid black
}
.space {
padding:0px;
border-spacing:1px;
}
HTML:
<table style="width:800px" class="space">
<tr>
<td><p>A</p></td>
<td><p>B</p></td>
<td><p>C</p></td>
</tr>
<tr>
<td><p>D</p></td>
<td><p>E</p></td>
<td><p>F</p></td>
</tr>
<tr>
<td><p>G</p></td>
<td><p>H</p></td>
<td><p>I</p></td>
</tr>
<tr>
<td><p>J</p></td>
<td><p>K</p></td>
<td><p>L</p></td>
</tr>
</table>
I can't make the table height smaller. I want the rows to be about half as small.
Suggestions?
(This is the code to the original table, without any alteration trying to make it lower in height as I want it)
The p tag has a margin. So that causes the height issue. Add the following code to your css file to get rid of that margin:
p {
margin: 0;
}
add this to your css:
.p{
height: 0.7em;
}
As #Wezelkrozum says, p has a default height.
Alternatively, you could try using div & br instead of p
So I have this code here:
<table>
<tr>
<td width="200px" valign="top">
<div class="left_menu">
<div class="menu_item">
Home
</div>
</div>
</td>
<td width="1000px" valign="top">Content</td>
</tr>
</table>
with the CSS
.left_menu {
background: none repeat scroll 0 0 #333333;
border-radius: 5px 5px 5px 5px;
font-family: Arial,Helvetica,sans-serif;
font-size: 12px;
font-weight: bold;
padding: 5px;
}
.menu_item {
background: none repeat scroll 0 0 #CCCCCC;
border-bottom: 1px solid #999999;
border-radius: 5px 5px 5px 5px;
border-top: 1px solid #FFFFCC;
cursor: pointer;
padding: 5px;
}
It works fine on my browser and I have tested it in every browser both mac and PC, but someone is complaining that the td with the width of 200 keeps changing width. I have no idea what he is talking about. Does anyone know why he or she is seeing the width change on the td?
It should be:
<td width="200">
or
<td style="width: 200px">
Note that if your cell contains some content that doesn't fit into the 200px (like somelongwordwithoutanyspaces), the cell will stretch nevertheless, unless your CSS contains table-layout: fixed for the table.
EDIT
As kristina childs noted on her answer, you should avoid both the width attribute and using inline CSS (with the style attribute). It's a good practice to separate style and structure as much as possible.
<table style="table-layout:fixed;">
This will force the styled width <td>. If the text overfills it, it will overlap the other <td> text. So try using media queries.
Width and/or height in tables are not standard anymore; as Ianzz says, they are deprecated. Instead the best way to do this is to have a block element inside your table cell that will hold the cell open to your desired size:
<table>
<tr>
<td valign="top">
<div class="left_menu">
<div class="menu_item">
Home
</div>
</div>
</td>
<td valign="top" class="content">Content</td>
</tr>
</table>
CSS
.content {
width: 1000px;
}
.left_menu {
background: none repeat scroll 0 0 #333333;
border-radius: 5px 5px 5px 5px;
font-family: Arial,Helvetica,sans-serif;
font-size: 12px;
font-weight: bold;
padding: 5px;
width: 200px;
}
.menu_item {
background: none repeat scroll 0 0 #CCCCCC;
border-bottom: 1px solid #999999;
border-radius: 5px 5px 5px 5px;
border-top: 1px solid #FFFFCC;
cursor: pointer;
padding: 5px;
}
This problem is quite easily solved using min-width and max-width within a css rule.
HTML
<table>
<tr>
<td class="name">Peter</td>
<td class="hobby">Photography</td>
<td class="comment">A long comment about something...</td>
</td>
</table>
CSS
.name {
max-width: 80px;
min-width: 80px;
}
This will force the first column to be 80px wide. Usually I only use max-width without min-width to reign in text that is very occasionally too long from creating a table that has a super wide column that is mostly empty. The OP's question was about setting to a fixed width though, hence both rules together. On many browsers width:80px; in CSS is ignored for table columns. Setting the width within the HTML does work, but is not the way you should do things.
I would recommend using min and max width rules, and not set them the same but rather set a range. This way the table can do it's thing, but you can give it some hints on what to do with overly long content.
If I want to keep the text from wrapping and increasing the height of a row - but still make it possible for a user to see the full text, I use white-space: nowrap; on the main rule, then apply a hover rule that removes the width and nowrap rules so that the user can see the full content when they over their mouse over it.
Something like this:
CSS
.name {
max-width: 80px;
white-space: nowrap;
overflow: hidden;
}
.name:hover {
max-width: none;
white-space: normal;
overflow:auto;
}
It just depends on exactly what you are trying to achieve. I hope this helps someone.
PS As an aside, for iOS there is a fix for hover not working - see CSS Hover Not Working on iOS Safari and Chrome
You can't specify units in width/height attributes of a table; these are always in pixels, but you should not use them at all since they are deprecated.
You can try the "table-layout: fixed;" to your table
table-layout: fixed;
width: 150px;
150px or your desired width.
Reference:
https://css-tricks.com/fixing-tables-long-strings/
You can use within <td> tag css : display:inline-block
Like: <td style="display:inline-block">
try this:
word-break: break-all;
try to use
word-wrap: break-word;
hope this help
I use
<td nowrap="nowrap">
to prevent wrap
Reference: https://www.w3schools.com/tags/att_td_nowrap.asp
Note that adjusting the width of a column in the thead will affect the whole table
<table>
<thead>
<tr width="25">
<th>Name</th>
<th>Email</th>
</tr>
</thead>
<tr>
<td>Joe</td>
<td>joe#email.com</td>
</tr>
</table>
In my case, the width on the thead > tr was overriding the width on table > tr > td directly.
I tried with many solutions but it didn't work for me so I tried flex with the table and it worked fine for me with all table functionalities like border-collapse and so on only change is display property
This was my HTML requirement
<table>
<thead>
<tr>
<th>1</th>
<th colspan="3">2</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td colspan="3">2</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td colspan="2">3</td>
</tr>
</tbody>
</table>
My CSS
table{
display: flex;
flex-direction: column;
}
table tr{
display: flex;
width: 100%;
}
table > thead > tr > th:first-child{
width: 20%;
}
table > thead > tr > th:last-child{
width: 80%;
}
table > tbody tr > td:first-child{
width: 10%;
}
table > tbody tr > td{
width: 30%;
}
table > tbody tr > td[colspan="2"]{
width: 60%;
}
table > tbody tr > td[colspan="3"]{
width: 90%;
}
/*This is to remove border making 1px space on right*/
table > tbody tr > td:last-child{
border-right: 0;
}
If you don't set the table to have table-layout: fixed and a certain width, then the table cells will stretch beyond their own width if content is wider. That's what he/she was complaining about.
Use
<table style="table-layout:fixed;">
It will force table to set to 100% width.Then use this code
$('#dataTable').dataTable( {
bAutoWidth: false,
aoColumns : [
{ sWidth: '45%' },
{ sWidth: '45%' },
{ sWidth: '10%' },
]
});
(table id is dataTable and having 3 column)
to specify length to each cell
Essentially what I want is a table, with one row, 5 cells... The first / left cell should be left justfied, the last / right cell should be right justified, and the middle 3 cells need to be centered with equal amounts of spacing between each cell. The table itself is "100% width, so that is where the spacing between cells would come from.
How would I write this (using html / css)? "table" tags or "divs" etc are both valid, I don't really mind which approach is taken as long as the end result looks correct.
Edit:
The problem is the spacing; the table itself isn't an issue, but simply setting the alignment on the cells will not work correctly; the free space between the cells is not 100% divided equally between the cells.
I also don't want to specify cell width as the content is dynamic and there is no way to know before hand how much width is needed.
HTML only version
<table>
<tr>
<td width="20%"></td>
<td width="20%" align="center"></td>
<td width="20%" align="center"></td>
<td width="20%" align="center"></td>
<td width="20%" align="right"></td>
</tr>
</table>
CSS Version
<table>
<tr>
<td style="width:20%;"></td>
<td style="width:20%; text-align:center"></td>
<td style="width:20%; text-align:center"></td>
<td style="width:20%; text-align:center"></td>
<td style="width:20%; text-align:right"></td>
</tr>
</table>
If you are using a table, assign unique ids to each cell and then use css to justify as required, e.g.
HTML:
<td id="firstcell">...</td>
<td id="secondcell">...</td>
<td id="thirdcell">...</td>
<td id="fourthcell">...</td>
<td id="fifthcell">...</td>
CSS:
table {table-layout:fixed;} /* ensure the widths are absolutely fixed at the specified width*/
td{ width: 20%;} /* allocate space evenly between all 5 cells */
td#firstcell {text-align:left;}
td#secondcell, td#thirdcell, td#fourthcell {text-align:center;}
td#fifthcell {text-align:right;}
td
{
text-align:center;
}
td:first-child
{
text-align:left;
}
td:last-child
{
text-align:right;
}
<style type="text/css">
.five_columns {
width: 100%;
}
.five_columns > div {
width: 20%;
float: left;
text-align: center;
overflow: auto;
}
.five_columns > div:first-child {
text-align: left;
}
.five_columns > div:last-child {
text-align: right;
}
</style>
<div class="five_columns">
<div>...</div>
<div>...</div>
<div>...</div>
<div>...</div>
<div>...</div>
</div>
overflow: auto is set because if you strictly want the width to be the same there really isn't much you can do (as far as I know) other than force scrollbars on anything that's too long.
I'm trying to achieve table similar to this using css/html only. Is it possible ?
So the white area is the places table. This is the HTML for the table :
<table class="places">
<tr>
<td class="solid">K</td>
<td> </td>
<td> </td>
<td class="solid">P</td>
<td> </td>
</tr>
<tr>
<td class="solid">25</td>
<td class="solid">26</td>
<td> </td>
<td class="solid">47</td>
<td class="solid">48</td>
</tr>
(...)
</table>
And my css :
.places{
position:relative;
background:white;
width:160px;
margin:0 auto;
text-align:left;
padding:5px;
border-collapse: collapse;
}
.places tr {
}
.places td {
width:22px;
height:22px;
text-align:center;
}
.solid {
border: 1px solid #d2cdd1;
border-top:none;
background-color:#e7e7e7;
text-align:center;
cursor:pointer;
}
I was pretty sure, that although tables are a bit different than other html objects, padding should work here. But it looks that I was wrong. Cellspacing/cellpading have no effect. Currently I was able to get something looking like this :
You need the border-spacing property.
Table cells are not like other elements, because while div and p gets are block level elements, and span and input are inline, table cells and rows get their own table-cell and table-row display values.
Using border-spacing with border-collapse: separate will give you what you'd need. Have a look: http://jsfiddle.net/kjag3/1/
PS. I've also taken the liberty of cleaning up the HTML by separating them into two tables, so you won't need the fillers for the empty cells.
The reason you can't set any spacing between the cells is that you have border-collapse set to collapse in the styles for your table. If you use border-collapse:separate instead, you should be able to add margins to your table cells and put spacing between them.
Using border-collapse:collapse makes it so that adjacent table cells use the same border; naturally, you wouldn't be able to put space between two elements when they're attached to each other.
I wonder whether a table structure is appropriate for what you're trying to achieve?
To me, it looks like the 'K' and 'P' are headings, and the gap between the 'K' and 'P' numbers suggests that 'K' and 'P' are separate and shouldn't be part of the same table. So I suggest getting rid of the table and restructuring your HTML to use simple headings and div tags like this:
HTML:
<div class="places">
<h2>K</h2>
<div>25</div>
<div>26</div>
<div>23</div>
<div>24</div>
<div>21</div>
<div>22</div>
</div>
<div class="places">
<h2>P</h2>
<div>47</div>
<div>48</div>
<div>45</div>
<div>46</div>
<div>43</div>
<div>44</div>
</div>
CSS:
.places {
width: 55px;
float: left;
margin: 0 25px 0 0;
}
.places h2, .places div {
width: 22px;
height: 22px;
margin: 0 3px 3px 0;
border: 1px solid #d2cdd1;
border-top:none;
background-color:#e7e7e7;
text-align:center;
cursor:pointer;
font-weight: normal;
font-size: 12pt;
}
.places div {
float: left;
}