Resize an image using css when its nested in table - html

Im trying to create a dynamic site that will display a specified profile image throughout the page in various areas.
As such, I'd like to have only one file, and resize it to fit in each area, much like Facebook does with its usergroups and profile pics.
I had no problem doing this until i tried to put the image inside a table:
HTML
<table width='600px'>
<tr>
<td rowspan="3" width="75px" height="75px">
<div id="cp_image" class="hidden rborder">
<img class="cp_img_resize" src="<?php echo $profile[0]['cp_img_loc'] ?>"/>
</div>
</td>
<td>
<strong><?php echo $profile[0]['name'] ?></strong>
</td>
</tr>
<tr>
<td>
<em><?php echo $profile[0]['description']?></em>
</td>
</tr>
<tr>
<td><?php echo $popular ?></td>
</tr>
</table>
My CSS
#cp_image {
width: 100px;
height: 100px;
}
#cp_name {
height: 35px;
min-width: 200px;
}
.cp_img_resize {
width:100px;
height: auto;
}
Is there something I'm missing? Any reason the image is not resizing?
Thanks

You are setting the width of the image TD inline to a smaller px:
<td rowspan="3" width="75px" height="75px">
The width of the TD is overriding the width you are declaring for the IMG.

See http://jsfiddle.net/DmnV7/2/
CSS:
.table{
width:600px;
}
.cp_img{
height:75px;
width:75px;
}
.cp_img>img {
max-height:75px;
max-width:75px;
display:block;
margin:0 auto;
}
HTML:
<table class="table">
<tr>
<td rowspan="3" class="cp_img">
<img src="[image]" />
</td>
<td>
<strong>Name</strong>
</td>
</tr>
<tr>
<td>
<em>Description</em>
</td>
</tr>
<tr>
<td>Popular</td>
</tr>
</table>

Related

How can I get the information table and image aligned on the same line

I am trying to make a web-page where I have a table of information and an image side-by-side as in the example below
Example:
bar fOo
Instead of:
bar
fOo
I have placed the image within a div and the information table in another div element, and I have been playing with the CSS properties to try to get them to be side-by-side, however they refuse to work as expected.
The CSS is below, #book is the image itself, while book_information is the information inside the table.
#book
{
float: right;
flex: 33.33%;
padding: 5px;
margin-left: 0;
width: 50%;
}
#book_information
{
width: 50%;
float: left;
padding-right: 0;
margin-right: 0;
}
table
{
border-collapse: collapse;
float:top;
border: 1px solid black;
table-layout: fixed;
width: 50%;
}
What should I use to allow this to work as expected? and what improvements could work I work on to get it responsive?
Below is the html structure of the page at the moment that I am using along with this:
<main>
<div>
<div class= "left" id="book"><img src="Book.jpg" alt="></div>
<div class="left" id="book_information">
<table id="information">
<tr>
<td class="1">Price:</td>
<td class="1">€18.90</td>
</tr>
<tr>
<td>Author</td>
<td></td>
</tr>
<tr>
<td class="1">About</td>
<td class="1"><p></p></td>
</tr>
<tr>
<td>Where to get it:</td>
<td>Amazon ,
Casa Del Libro ,
FNAC ,
Libelista
</td>
</tr>
</table>
</div>
</div>
</main>
There are different ways of doing this.
Here's a solution that involves using float:left style for your table:
table {
float: left;
}
img {
width: 100px;
height: 100px;
}
<table>
<tr>
<td>Price</td>
<td>$5</td>
</tr>
<tr>
<td>Author</td>
<td>Bob</td>
</tr>
</table>
<div>
<img
src="https://via.placeholder.com/120.png?text=Book+Image"
alt="Book Image Here">
</div>
This one involves putting your table and image in yet another table:
img {
width: 100px;
height: 100px;
}
<table>
<tr>
<td>
<table>
<tr>
<td>Price</td>
<td>$5</td>
</tr>
<tr>
<td>Author</td>
<td>Bob</td>
</tr>
</table>
</td>
<td>
<div>
<img
src="https://via.placeholder.com/120.png?text=Book+Image"
alt="Book Image Here">
</div>
</td>
</tr>
</table>

How to get an image to scale vertically inside a table cell?

I'm working with a legacy table layout, and I've come across a layout problem with the scaling of images within a table cell.
The following code when in a page, scales the image up and down as the width of the browser window changes. However if you change the browswer height of the window up and down, it doesn't scale the image it seems to be locked to whatever the width of the image is. So if the window is wide, then the green section disappears off the bottom of the window
How can I make it so it scales based on height as well? Meaning the red section and the green section should squash the image till it is tiny as the height of the browser gets smaller.
There are javascript solutions to this problem, but I'd like to do it with HTML/CSS.
<style>
.red {
background-color: red;
}
.green {
background-color: green;
}
table {
height: 100vh;
table-layout: fixed;
}
img {
width: 100%;
height: 100%;
}
</style>
</head>
<body>
<table style="width:100%">
<col width="100">
<col width="100%">
<col width="100">
<tr class="red" style="height:100px;"> <td> </td> <td> </td> <td></td> </tr>
<tr>
<td> </td>
<td>
<img src="https://upload.wikimedia.org/wikipedia/commons/c/c4/PM5544_with_non-PAL_signals.png" />
</td>
<td> </td>
</tr>
<tr class="green" style="height:200px;">
<td> </td> <td> </td> <td> </td>
</tr>
</table>
</body>
so here is the width compressed, and the image shrinks...
here, the width is wider, but the height is the same, but the image is now taking more vertical height
Maybe those edits will be useful, play with it.
.red {
background-color: red;
}
.green {
background-color: green;
}
table {
height: 100vh;
table-layout: fixed;
}
img {
width: 100%;
height: 100%;
}
.image{
position: relative;
padding-top: 30vh;
}
.image img{
position: absolute;
top: 0;
height: auto;
max-height: 100%;
width: auto;
max-width: 100%;
right: 0;
margin: auto;
left: 0;
}
<table style="width:100%">
<col width="100">
<col width="100%">
<col width="100">
<tr class="red" style="height:100px;"> <td> </td> <td> </td> <td></td> </tr>
<tr>
<td> </td>
<td class="image">
<img src="https://upload.wikimedia.org/wikipedia/commons/c/c4/PM5544_with_non-PAL_signals.png" />
</td>
<td> </td>
</tr>
<tr class="green" style="height:200px;">
<td> </td> <td> </td> <td> </td>
</tr>
</table>
I set max-height for your image to visible the bottom green section in all devices. So you can resize it. You can adjust the max-height based on your requirement. I hope this solution may help you.
.red {
background-color: red;
}
.green {
background-color: green;
}
table {
height: 100vh;
table-layout: fixed;
}
img {
width: 100%;
height: 100%;
max-height: 450px;
object-fit: contain;
}
<table style="width:100%">
<colgroup>
<col width="100">
<col width="100%">
<col width="100">
</colgroup>
<tr class="red" style="height:100px;">
<td> </td>
<td> </td>
<td></td>
</tr>
<tr>
<td> </td>
<td>
<img src="https://upload.wikimedia.org/wikipedia/commons/c/c4/PM5544_with_non-PAL_signals.png" />
</td>
<td> </td>
</tr>
<tr class="green" style="height:200px;">
<td> </td>
<td> </td>
<td> </td>
</tr>
</table>
Your <col> widths were 100px, 100%, 100px which is 200px over. The middle column is a non-specified width so it takes whatever space the left and right fixed columns do not take.
The height for the first row is 100px and last row is 200px. The height of the middle row takes up the remaining vertical space. Special pseudo-classes were used to target the td (not the tr). tr seems the logical choice to modify height but it isn't't, because the nature of a html is content oriented therefore witdh and height is 90% td and 10% table.
tr:first-of-type td:first-of-type
tr:last-of-type td:last-of-type
<html> and <body> are set to 100% x 100% position:relative so the <table> assigned with position:absolute sits squarely within the viewport.
The middle row was expanded by colspan='3' and the image is now a background-image. With a minor change the image can expand and fill the whole row without overflow or shifting the other rows further. set background-size to cover from contain.
Demo
html,
body {
height: 100%;
width: 100%;
position:relative;
overflow:hidden;
}
* {
margin:0;
padding:0;
}
.red {
background-color: red;
}
.green {
background-color: green;
}
table {
height: 100%;
width:100%;
table-layout: fixed;
position:absolute;
left:0;
top:0;
bottom:0;
right:0;
}
tr:nth-of-type(2) td {background:url("https://upload.wikimedia.org/wikipedia/commons/c/c4/PM5544_with_non-PAL_signals.png" )no-repeat; background-size:contain;background-position:center center;}
tr:first-of-type td:first-of-type {
min-height: 100px;
}
tr:last-of-type td:last-of-type {
min-height: 200px
}
<table style="width:100%">
<col width="100">
<col>
<col width="100">
<tr class="red">
<td height='100'> </td> <td>
</td><td></td>
</tr>
<tr>
<td colspan='3'> </td>
</tr>
<tr class="green">
<td height='200'> </td>
<td></td>
<td> </td>
</tr>
</table>
Like this?
https://jsfiddle.net/xhc6ovzL/
<table style="width:100%">
<col width="100">
<col width="100%">
<col width="100">
<tr class="red" style="height:100px;"> <td> </td> <td> </td> <td></td> </tr>
<tr>
<td colspan="3">
<img src="https://upload.wikimedia.org/wikipedia/commons/c/c4/PM5544_with_non-PAL_signals.png" />
</td>
<td> </td>
</tr>
<tr class="green" style="height:200px;">
<td> </td> <td> </td> <td> </td>
</tr>

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

Setting background to a table with images and text

I am trying to create a table with text and images with html and css. I want to give a color to the whole table and show image over that back ground color
HTML
<table>
<tr>
<td class="timg">
<img src="s.jpg" />
</td>
<td>
<p><h3>This is some text </h3> all the bbbbbbbbbbbbbbbbb</p>
</td>
</tr>
<tr>
<td class="timg"><img src="p.jpg" /> </td>
<td> <p> This is some text</p> </td>
</tr>
</table>
CSS
.timg{
height: 100px;
width: 150px;
}
.timg img {
height: 100px;
width:150px;
}
jsFiddle

Full width table in a scrollingcontainer

I'm about to create a table which is going to look like this one right here:
Now it's getting tricky since the website is going to be responsive. That's why I thought it would be nice to wrap the table in a div .cross-tab which has overflow: scroll.
<div class="bs-example">
<div class="cross-tab">
<table>
<thead>
<tr>
<? for ($i=0; $i < 19; $i++) { ?>
<th>
<img src="http://placehold.it/40x40" class="team-icon">
</th>
<? } ?>
</tr>
</thead>
<tbody>
<tr></tr>
</tbody>
</table>
</div>
</div>
The div should have a certain width the overflow: scroll. So the table would be full-sized on any device but get cropped off on small screens with a scrollbar to scroll arround:
Is there a way to accomplish this with pure CSS or do I have to calculate the full table with with js and so on?
You can fix your problem with media query.
For example :
HTML
<body>
<div>
<table border="1" cellspacing="2" cellpadding="0">
<tr> <td> </td> </tr> <tr> <td> </td> </tr>
<tr> <td> </td> </tr> <tr> <td> </td> </tr>
<tr> <td> </td> </tr> <tr> <td> </td> </tr>
<tr> <td> </td> </tr> <tr> <td> </td> </tr>
<tr> <td> </td> </tr> <tr> <td> </td> </tr>
</table>
</div>
</body>
CSS
div
{
background : red;
width:100%;
overflow:auto;
}
table
{
width :100%;
overflow:hidden;
}
#media screen and (max-width: 640px)
{
table
{
width : 800px;
}
div
{
height:200px;
}
}
codepen : http://codepen.io/ColoO/pen/xuACl