I have table with 3 cells, and I want to make middle one higher than side ones, so for example middle one's height is 300px and side one's is 200px. I gave middle one seperate class than side ones and I set middle one to 300px and side ones to 200px. But they are still the same height, why?
#mid {
width: 600px;
height: 400px;
border: solid 1px black;
}
.side {
width: 300px;
height: 300px;
border: solid 1px black;
}
<table>
<tr>
<td class="side"></td>
<td id="mid"></td>
<td class="side"></td>
</tr>
</table>
You should only use tables for tabular data and not for layout. What you are trying to do is not achievable with a table as all table cells in a row will be the same height as the tallest cell of that row.
Instead you should use divs, in the following example I have used flexbox to align the divs in a row:
.container {
display: flex;
width: 1200px;
margin: auto;
}
.mid {
width: 600px;
height: 400px;
border: solid 1px black;
}
.side {
width: 300px;
height: 300px;
border: solid 1px black;
}
<div class="container">
<div class="side"></div>
<div class="mid"></div>
<div class="side"></div>
</div>
LIke others have mentioned in comments you are better off using a div or some other tags
but if you still want to use table you can do something like this
<table>
<tr>
<td> </td>
<td id = "mid" rowspan="2"></td>
<td></td>
</tr>
<tr>
<td class = "side"></td>
<td></td>
<td class ="side"></td>
</tr>
</table>
here is a fiddle
https://jsfiddle.net/vdadekvL/28/
Related
I am learning HTML and I am trying to align different icons in a line but in a different positions. The first element should be aligned to the left and the others aligned to the right.
E1| |E2|E3|E4
I am not sure how to implement that. Should I use a table? Or a div with <li> elements?
I tried to use a table with a blank column but it's not working
td {
background-color: yellow;
border: 1px solid black;
}
<table>
<tr>
<td width="60%"> test </td>
<td> blank </td>
<td> 1 </td>
<td> 2 </td>
<td> 3 </td>
</table>
You mean this?
Flex ratio
.box {
display: flex;
}
.box div {
background-color: yellow;
border: 1px solid black;
}
.e1{
flex: .1 0 0;
}
<div class="box">
<div class="e1">E1</div>
<div>E2</div>
<div>E3</div>
<div>E4</div>
</div>
or using table
td {
background-color: yellow;
border: 1px solid black;
}
<table>
<tr>
<td>E1</td>
<td width="60%"> </td>
<td>E2</td>
<td>E3</td>
<td>E4</td>
</table>
A common method for this is to use display: flex for the container and apply margin-left: auto to the first of the child elements that should be moved to the right. That way that left margin will grow as wide as possible within the given container and with the given settings:
.container {
width: 100%;
display: flex;
background: #ccc;
}
.container>div {
padding: 5px 10px;
}
.container>div:nth-child(2) {
margin-left: auto;
}
<div class="container">
<div> 1 </div>
<div> 2 </div>
<div> 3 </div>
<div> 4 </div>
</div>
What I want is the image to be the same height of the td when the image isn't there, i.e. 300px, not the height of the image src. I can't specifiy the height of the image, td or table since the parent div represents the height of a responsive container. I've spent far too long on this and tried many things and for some reason the image always insists on being its full height.
<div style='height:300px;width:300px;'>
<table style='height:100%;width:100%;'>
<tr>
<td>
<img style='height:100%;width:100%;' src='https://placehold.it/1920x1200'>
</td>
</tr>
</table>
</div>
Try using CSS instead of inline styles. This helps keep your code more flexible. I've set the height and width to be auto and the max-height and max-width to be at 100% so that the image is contained inside the table cell, but also correctly scaled.
.table-container {
height: 300px;
width: 300px;
padding: 5px;
border: 1px solid red;
}
table td {
border: 1px solid blue;
padding: 0;
}
table td img {
display: block;
width: auto;
height: auto;
max-width: 100%;
max-height: 100%;
}
<div class='table-container'>
<table>
<tr>
<td>
<img src='https://placehold.it/1920x1200' />
</td>
</tr>
</table>
</div>
Try This, added "overflow: hidden;position: relative;" to parent and "position: absolute;" to child
<div style='height:300px;width:300px;'>
<table style='height:100%;width:100%; overflow: hidden;position: relative;'>
<tr>
<td style='position: absolute;'>
<img style='height:100%;width:100%;' src='https://placehold.it/1920x1200'>
</td>
</tr>
</table>
</div>
output screen
I'm trying to make a 2 columns table with the following properties :
the table must fit to its parent container
the first column must fit to its content
the second column content must be horizontally scrollable if it
exceed the table width.
► I began with the following code :
(css here is just to "see" the main container and the table - width: 500px is just for the example, it could be any value)
.main {
width: 500px;
border: 1px solid black;
}
table {
background: rgba(0,0,0,0.5);
}
<div class="main">
<table>
<tr>
<td class="one">content_1</td>
<td class="two">
<div class="content">short string</div>
</td>
</tr>
<tr>
<td class="one">content_2</td>
<td class="two">aVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongStringWithoutSpaces</td>
</tr>
</table>
</div>
Point 1 is not ok : The table does not fit to its container
Point 2 is ok : first column fit to its content
point 3 not ok : as the table does not fit its container, I can't apply an overflow: auto for now
► the only way I found to make the table fit its container is to add the following css properties : table-layout: fixed & width: 100%
then I can add width:100%, display: inline-block & overflow: auto to the second column to make the content scrollable if it exceed the table width
.main {
width: 500px;
border: 1px solid black;
}
table {
background: rgba(0,0,0,0.5);
table-layout: fixed;
width: 100%;
}
.two {
width:100%;
display: inline-block;
overflow: auto;
}
<div class="main">
<table>
<tr>
<td class="one">content_1</td>
<td class="two">
<div class="content">short string</div>
</td>
</tr>
<tr>
<td class="one">content_2</td>
<td class="two">aVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongStringWithoutSpaces</td>
</tr>
</table>
</div>
Point 1 is now ok : The table fit to its container
Point 2 is not ok anymore : table-layout: fixed makes the two columns
point 3 now ok : the content of the second column is scrollable if its too long
► So now I'm looking for a solution to have the 3 points OK.
to get the closest to wha I need, I set width: 50px to the .one class, but in my project I can't do that as I have several tables with differents content size in the first column, so I need the first column's size to be set automatically to fit to the content as in the first snippet.
Any help ?
you can use word-break:break-all for .two
.main {
width: 500px;
border: 1px solid black;
}
table {
background: rgba(0,0,0,0.5);
width: 100%;
}
.two{
word-break:break-all;
}
<div class="main">
<table>
<tr>
<td class="one">content_1</td>
<td class="two">
<div class="content">short string</div>
</td>
</tr>
<tr>
<td class="one">content_2</td>
<td class="two">aVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongStringWithoutSpaces</td>
</tr>
</table>
</div>
I would like to have the inner divs within the bottom row of this table fill the bottom half of the table in height. Any help would be appreciated.
http://jsfiddle.net/6QGDn/1/
This is what I see in Firefox 28.0
EDIT: This was fixed by using the .relative class on the td in the bottom row. Problem solved, thanks for your help.
HTML
<table>
<tr>
<td>
Top Row 1
</td>
<td>
Top Row 2
</td>
<td>
Top Row 3
</td>
<td>
Top Row 4
</td>
<td>
Top Row 5
</td>
</tr>
<tr>
<td class="no-border relative" colspan="5">
<div class="left-half">
<div class="div-td">
Bottom Row 1
</div>
</div>
<div class="right-half">
<div class="div-td">
Bottom Row 2
</div>
</div>
</td>
</tr>
</table>
CSS
table {
width: 400px;
height: 240px;
}
table, td, tr, .div-td {
border: 1px solid #000;
}
.no-border {
border: none;
}
.relative {
position: relative;
height: 50%;
}
.left-half {
float: left;
height: 100%;
width: 50%;
}
.right-half {
float: right;
height: 100%;
width: 50%;
}
.div-td {
height: 100%;
}
I have a HTML table and I want the first few columns to be quite long. I am doing this in CSS:
td.longColumn
{
width: 300px;
}
and here is a simplified version of my table
<table>
<tr>
<td class='longColumn'></td>
<td class='longColumn'></td>
<td class='longColumn'></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
[ . . and a bunch more columns . . .]
</tr>
</table>
For some reason the table seems to make this column < 300px when there are a lot of columns. I basically want it to keep that width no matter what (and just increase the horizontal scroll bar).
The container that the table is inside, doesn't have any type of max width so I can't figure out why it's squeezing this column down as opposed to respecting this width.
Is there anyway around this so no matter what, this column will stay a certain width?
Here is the CSS of the outer container div:
#main
{
margin: 22px 0 0 0;
padding: 30px 30px 15px 30px;
border: solid 1px #AAAAAA;
background-color: #fff;
margin-bottom: 30px;
margin-left: 10px;
_height: 1px; /* only IE6 applies CSS properties starting with an underscrore */
float: left;
/*width: 1020px;*/
min-width:1020px;
display: block;
overflow: visible;
z-index: 0;
}
You may get more luck with setting widths for your table cells if you apply the rule table-layout: fixed to the table - this has helped me with a lot of cell-sizing issues when using tables. I would not recommend switching to using just DIVs to arrange your content if it fits the purpose of tables - to display multidimensional data.
Giving it both max-width and min-width attributes should work.
I agree with Hristo but there are some cases where table need to be used and solution to your table problem is adding below class to the table and then changing any td width as per your need.
.tables{ border-collapse:collapse; table-layout:fixed;}
I hope this helps for someone who is looking for table solution!
I had the same problem with a bunch of columns where I wanted spacers columns.
I used to do:
<td style='width: 10px;'> </td>
But when the table was wider than window, the spacers were not really 10px, but maybe 5px.
And using only DIVs without a TABLE was not an option in my case.
So I tried:
<td><div style='width: 10px;'></div></td>
And it worked very well ! :)
The best way to set your column widths (td's) is to use a table header (th's). Table headers will set the width on your td's automatically. You just have to make sure that your columns inside your thead are the same number of columns in your tbody.
Check it out here:
http://jsfiddle.net/tKAj8/
HTML
<table>
<thead>
<tr>
<th class="short-column">Short Column</th> <!-- th sets the width -->
<th class="short-column">Short Column</th> <!-- th sets the width -->
<th class="long-column">Long Column</th> <!-- th sets the width -->
</tr>
</thead>
<tbody>
<tr>
<td class="lite-gray">Short Column</td> <!-- td inherits th width -->
<td class="lite-gray">Short Column</td> <!-- td inherits th width -->
<td class="gray">Long Column</td> <!-- td inherits th width -->
</tr>
</tbody>
</table>
CSS
table { table-layout: fixed; border-collapse: collapse; border-spacing: 0; width: 100%; }
.short-column { background: yellow; width: 15%; }
.long-column { background: lime; width: 70%; }
.lite-gray { background: #f2f2f2; }
.gray { background: #cccccc; }
I had issues with not being able to size columns in a table-layout: fixed table that was using a colspan. For the benefit of anyone experiencing a variant of that issue where the suggestion above doesn't work, colgroup worked for me (variation on OP's code):
div {
margin: 22px 0 0 0;
padding: 30px 30px 15px 30px;
border: solid 1px #AAAAAA;
background-color: #fff;
margin-bottom: 30px;
margin-left: 10px;
_height: 1px; /* only IE6 applies CSS properties starting with an underscrore */
float: left;
/*width: 1020px;*/
min-width:1020px;
display: block;
overflow: visible;
z-index: 0;
}
td.longColumn {
width: 300px;
}
table {
border: 1px solid;
text-align: center;
width: 100%;
}
td, tr {
border: 1px solid;
}
<div>
<table>
<colgroup>
<col class='longColumn' />
<col class='longColumn' />
<col class='longColumn' />
<col/>
<col/>
<col/>
<col/>
</colgroup>
<tbody>
<tr>
<td colspan="7">Stuff</td>
</tr>
<tr>
<td>Long Column</td>
<td>Long Column</td>
<td>Long Column</td>
<td>Short</td>
<td>Short</td>
<td>Short</td>
<td>Short</td>
</tr>
</tbody>
</table>
</div>
For those that are having Table Cell/Column width problems and table-layout: fixed did not help.
When applying fixed widths to table cells (<td> or <th>), do not assign a width to all of the cells. There should be at least one cell with an (auto) width. This cell will act as a filler for the remaining space of the table.
e.g.
<table>
<thead>
<tr>
<th style="width: 150">Assigned 150 width to Table Header Cell</th>
<th style="width: 100">Assigned 100 width to Table Header Cell</th>
<th>No width assigned</th>
</tr>
</thead>
<tbody>
<tr>
<td style="width: 150">Assigned 150 width to Table Body Cell</td>
<td style="width: 100">Assigned 100 width to Table Body Cell</td>
<td>No width assigned</td>
</tr>
</tbody>
</table>
P.S. you can use style classes here, you don't need to use an in-line style.
Use table-layout property and the "fixed" value on your table.
table {
table-layout: fixed;
width: 300px; /* your desired width */
}
After setting up the entire width of the table,
you can now setup the width in % of the td's.
td:nth-child(1), td:nth-child(2) {
width: 15%;
}
You can learn more about in on this link: http://www.w3schools.com/cssref/pr_tab_table-layout.asp
Can't modify <td> width; that is, column width isn't settable. You can add the styling white-space:nowrap; which might help. Or you can add s to add space to columns.
Maybe you could set col width the HTML way: <td width="70%">January>/td>
Unfortunately, in HTML 4.01 and later, that way isn't valid.
How about something like this...
http://jsfiddle.net/qabwb/1/
HTML
<div id="wrapper">
<div class="row">
<div class="column first longColumn">stuff</div>
<div class="column longColumn">more stuff</div>
<div class="column">foo</div>
<div class="column">jsfiddle</div>
</div>
<div class="row">
<div class="column first longColumn">stuff</div>
<div class="column longColumn">more stuff</div>
<div class="column">foo</div>
<div class="column">jsfiddle</div>
</div>
<div class="row">
<div class="column first longColumn">stuff</div>
<div class="column longColumn">more stuff</div>
<div class="column">foo</div>
<div class="column">jsfiddle</div>
</div>
</div>
CSS
#wrapper {
min-width: 450px;
height: auto;
border: 1px solid lime;
}
.row {
padding: 4px;
}
.column {
border: 1px solid orange;
border-left: none;
padding: 4px;
display: table-cell;
}
.first {
border-left: 1px solid orange;
}
.longColumn {
min-width: 150px;
}