HTML: align images at top and bottom of cells - html

I need to create a table with 2 cells (or 2 divs. Doesn't matter), which will be contained within a TD tag, like this:
<td style="height:200px;">
<table>
<tr>
<td>Top Cell</td>
</tr>
<tr>
<td>Bottom Cell</td>
</tr>
</table>
</td>
Each cell will contain 1 image (see image below). My problem however, is the image in the top cell always needs to be at the very top and the image in the bottom cell always needs to be at the very bottom, irregardless of the height of the parent TD tag. So in the example below, lets say sample #1's parent TD tag is 200px height. The images align to the very top and bottom of their cells. If I switch the height of the TD tag to 800px (Sample #2), then the images should still align properly.
alignment sample http://functionalevaluations.com/images/imagealignment.jpg
I should also mention that I can't hardcode a height into the table itself. The height of the table will always need to be 100% of the parent TD tag and the only value where I can manually adjust the height value is in the parent TD.
How can I do this? Oh, also it doesn't matter if this is a table or divs or whatever. The only thing that needs to be there is the parent TD tag.
Finally, here's my current HTML. My parent TD adjusts fine, however the height of my table is always no more than the height of my 2 images. I can't seem to get it to be the same height of the parent TD:
<td width="155" valign="top" rowspan="2" style="border:solid 1px #000;height:200px; ">
<table border="1" cellspacing="0" cellpadding="0" style="height: 100%;">
<tr>
<td valign="top" style="height:50%;">
<img src='includes/images/itemImages/TopImage.jpg' border="0"
style="vertical-align: top">
</td>
</tr>
<tr>
<td valign="bottom" style="height:50%;">
<img src='includes/images/itemImages/bottomImage.jpg' border="0" style="vertical-align: bottom">
</td>
</tr>
</table>
</td>

Try this - DEMO
HTML
<table border="1" cellspacing="0" cellpadding="0">
<tr>
<td class="top">
<img src='http://lorempixel.com/100/100' />
</td>
</tr>
<tr>
<td class="bottom">
<img src='http://lorempixel.com/100/100' />
</td>
</tr>
</table>
CSS
img {
display: block;
margin: auto;
}
.top { vertical-align: top; }
.bottom { vertical-align: bottom; }

Related

Why td adds extra padding in table?

<html>
<body style="background:grey;">
<table width="500" border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td style="
background-image: url(https://s28.postimg.org/yrbcuftd9/zip.png);
height: 9px;
"></td>
</tr>
<tr>
<td style="
background-image: url(https://s28.postimg.org/yrbcuftd9/zip.png);
height: 9px;
"></td>
</tr><tr>
<td><img width="500" src="https://s28.postimg.org/yrbcuftd9/zip.png"></td>
</tr>
</tbody>
</table>
</body></html>
I don't understand why the height of third zip image is 18. Basically, when I add the image as background,there is no gap between each row (No gap between row 1 and row 2). However, when I use image tag, it creates gap between row 2 and row 3. I don't understand why. Any ideas? And also how can I delete the gap between row 2 and there.
<img> is considered inline by default, so like other inline elements, it has a line height and space for descenders. Descender space is that little padding under each line that isn't accounted for anywhere, for the hanging bits of letters like in 'p' or 'q'.
If you set it to display: block, everything will click together.
A <td> tag will by default inherit display:table-cell; as a style. To resolve this, simply override that to flex.
<html>
<body style="background:grey;">
<table width="500" border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td style="
background-image: url(https://s28.postimg.org/yrbcuftd9/zip.png);
height: 9px;
"></td>
</tr>
<tr>
<td style="
background-image: url(https://s28.postimg.org/yrbcuftd9/zip.png);
height: 9px;
"></td>
</tr><tr>
<td style="display:flex;"><img width="500" src="https://s28.postimg.org/yrbcuftd9/zip.png"></td>
</tr>
</tbody>
</table>
</body></html>
Anything added through the CSS is not considered 'content' by the browser.
The only reason you see the first two rows is because of the height:9px; CSS.
If you remove that you will see that the rows do not appear (0 height) this is because the rows do not contain any content so they rely on CSS to specify their dimensions.
Because the last <td> has content in the <img ... tag it will display the image with the standard table spacing as this has not been overridden by any css.

Why image not fit to table cell height in html?

I want to split screen to two area left and right,in right segment show a image and in right segment show textbox,for that purpose write this html code:
<table style="width:100%">
<tr>
<td style="width:50%;height:100%;" align="center">
<img src="../Content/45.png" style="display:block;height:100%;width:100%;" />
</td>
<td style="width:50%;">
behzad
</td>
</tr>
</table>
but when i run the html page ,image not fit to the table column height,and i must scroll the browser to see all of the page,i want just fit to the screen height,my out put i this :
Use viewport units vh/vw, and to keep aspect ratio on the image, set the style on the image to display:block;max-width:50vw;max-height:100vh
html, body {
margin: 0;
}
table {
border-collapse: collapse;
}
td {
padding: 0;
vertical-align: top;
}
<table style="width:100vw">
<tr>
<td style="width:50vw;height:100vh;" align="center">
<img src="http://lorempixel.com/600/600/animals" style="display:block;max-width:50vw;max-height:100vh" />
</td>
<td style="width:50vh;">
behzad
</td>
</tr>
</table>
in this case you'd better use background-image instead of <img> to avoid stretching
check the following demo
html,body{
height:100%;
}
table{
height:100%;
}
<table style="width:100%">
<tr>
<td style="width:50%;height:100%;background-image:url(http://placehold.it/600x600)" align="center">
</td>
<td style="width:50%;">
behzad
</td>
</tr>
</table>
emphasized text
instead of this section
<img src="../Content/45.png" style="display:block;height:100%;width:100%;" />
try this section
<img src="../Content/45.png" height="50%" />
I hope it works

Set table's height to its parent div's height whose height is variable

I have a table inside a div, which contains an image with "position:absolute", "width:100%" and "height:auto". The div has a variable height, and I need the table to have the same height as the div, so the cells can have percentage heights...
It also would help if I found the way for the div, or a table, to adapt to the height of the image when it is a background-image. Because when I set the width as 100%, the image doesn't show full vertically, only horizontal and gets cut by the size of the table/div.
#footer {
width: 100%;
}
<div id="footer">
<img src="All/footer.png" style="position:absolute; width:100%">
<table>
<tr>
<td style="height:30%;"></td>
</tr>
<tr>
<td style="width:10%;"></td>
<td id="content" style="width:40%; height:30%;">ParagraphParagraphParagraphParagraphParagraph
<br/>ParagraphParagraphParagraphParagraphParagraph
<br/>ParagraphParagraphParagraphParagraphParagraph
<br/>ParagraphParagraphParagraphParagraphParagraph
<br/>ParagraphParagraphParagraphParagraphParagraph</td>
<td style="width:50%;"></td>
<td rowspan="5"></td>
</tr>
<tr>
<td style="height:25%;"></td>
</tr>
<tr>
<td id="slogan" colspan="3" style="height:10%">One Line Text. Slogan</td>
</tr>
<tr>
<td colspan="3" style="height:10%;"></td>
</tr>
</table>
</div>

Underline text in HTML

I want to write the simpliest example of the following image
My example should work in ie6,7,8,9 and so on. So I can't use float or anything helpfull. I made jsFiddle using table
<table width="500px">
<tr>
<td width="45px"><span>e-mail</span>
</td>
<td align="center"> <div style="border-bottom: 1px solid;">test#gmail.com</div></td>
</tr>
<tr>
<td width="45px"></td>
<td align="center"> <span>(email)</span>
</td>
</tr>
</table>
, but the bottom (email) have margin from the line.
And I want that everything was like on my first image. Thanks
I have tried this.
Check this jsFiddle link
<table width="500px" style="padding:0px; border-spacing:1px">
<tr>
<td width="45px" style="padding:0px"><span>e-mail</span>
</td>
<td align="center" style="padding:0px"> test#gmail.com <hr noshade style="border-top:1px; -webkit-margin-before: 0; -webkit-margin-after: 0; -webkit-margin-top: 0; -webkit-margin-bottom: 0;"/> </td>
</tr>
<tr>
<td width="45px" style="padding:0px"></td>
<td align = "center" style = "margin-top:0px; padding:0px">(email)</td>
</tr>
The “margin” you are referring to is partly spacing between cells, partly padding inside the cell, partly leading, and partly spacing in the font. In your approach, the simplest fix is probably to set cell spacing to zero and to move the cell content upwards a bit, using relative positioning.
<table width="500" cellspacing="0">
<tr>
<td width="45">e-mail
</td>
<td align="center" style="border-bottom: 1px solid">test#gmail.com</td>
</tr>
<tr>
<td width="45"></td>
<td align="center"><span style="position: relative; top: -0.15em">(email)</span>
</td>
</tr>
</table>
apply border-spacing: 0 to the table's style (border-collapse: collapsewould work too, aswell as cellspacing attribute)
add padding: 0 to the tdcontaining <span>(email)</span>
To increase the space between the upper e-mail-adress and the bottom border (which you did not mention explicitly but is not the same as your example image):
move the border-bottom style from div to its parent td
add a padding-bottom to the same td

Dynamically change table height

I'm facing problem with table in html,
actually I fixed the width,and want to increase the td height as per the content in it,
for example : if td width is 30px and when data inside td crosses the td width i want to show remaining data in next line and so on..
table:
<table width="100%" border="1" >
<tr>
<td align="left" width="30px">
laaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa text
</td>
<td align="left" width="30px">
text
</td>
</tr>
</table>
Here is the solution to your problem! The width of the columns are equal in this case, though they can be changed, not to mention.
<table border="1" width="100%" style="table-layout:fixed">
<tr>
<td style="word-wrap:break-word">Hellohellohellohellohellohellohellohellohellohellohello
<td>text</td>
</tr>
</table>
And here is the demo http://jsbin.com/ihaxob/2/edit ..
I have seen the solution from this thread!
Word-wrap in an HTML table