0

I've got a page and want to show an image in it. I want it to be inside a div that has a fixed size.

enter image description here

The button browses a local disk for an image. If the image is smaller than the containing div, no problem, it stretches to fit the div. However, when it's larger than the div, it simply isn't displayed.

Here's the code:

HTML

<div class="padded">

    <div class="container">

        <img width="100%" src="@ViewBag.imgPath"/>

    </div>

</div>

CSS

.padded
{
    padding: 20px 0px 0px 20px;
}

.container
{
    width: 250px;
    height: 300px;
}

img
{
    max-width: 100%;
    max-height: 100%;
}

I tried, as can be seen in the above code, the max-width and max-height properties, setting a fixed size for the containing div but it doesn't work.

Here's a small list of questions I've checked without finding the solution:

And here's the fiddle.

EDIT

Note that it works with an image from the internet but not with one from my local machine. Example: if the src property is like src="http://whatever/image.png", it works fine. If it is something like src="c:\whatever\image.png", it doesn't work. In my case, the images will be on my machine so I need a solution for that.

Community
  • 1
  • 1
chiapa
  • 4,212
  • 8
  • 52
  • 103
  • 1
    can you make us a fiddle? – giorgio Jul 17 '14 at 15:15
  • I just added a fiddle – chiapa Jul 17 '14 at 15:18
  • 1
    dude... I could've pasted the code in jsfiddle myself as well... But obviously the relevant css and _a accessible image_ is missing... The whole goal of a fiddle is a working example _that demonstrates the problem_ – giorgio Jul 17 '14 at 15:20
  • Just edited my question – chiapa Jul 17 '14 at 15:25
  • ehrm... a browser actually doesn't really care about the location of an image, as long as it can reach it... So if it works with an image on the internet, but _not_ with an image on your local machine, then either your code (html/css) is different, or the image is different... – giorgio Jul 17 '14 at 15:32
  • If you use local files then avoid absolute paths. If you don't want to use relative path, then use `file:///C:/ ...` scheme to link to local files with an absolute path. – Tom Jul 17 '14 at 15:35
  • @giorgio, let me explain it better: as you can see, the img src property is filled via a ViewBag, which is filled from a database record; on the DB, the sources are local paths. I've tried inserting an internet path for a large image and it resizes perfectly. If it's a local path for a large image, it doesn't. **That** is the problem. More: I've tried to fill the src of the img manually for both cases and the problem is, once again, it works fine for internet images and not for local machine images. I hope you understood. And the code on the question is the running code, nothing is different – chiapa Jul 17 '14 at 15:38

2 Answers2

0

Try just removing the width="100%" from the img tag. Also add display: block to your img.

mattcouchman
  • 346
  • 2
  • 7
  • Just read your edit, so the problem is with your img source? I don't understand. – mattcouchman Jul 17 '14 at 15:32
  • No, the img source is always a local path and the larger images aren't displayed. I added that information to point that I've tried with sources that are urls and the images are properly resized to fit – chiapa Jul 17 '14 at 15:34
0

You can try putting the image in the same folder as the file and instead of the whole path to image file, use only the file name and see if it works. If it does then there is something wrong with the path that you are putting in.

raw_orb
  • 181
  • 1
  • 12
  • I've tried that but also no luck, that's isn't the problem. Again, if the img src property is a local path for an image smaller than the div, everything fine. If it's larger than the div, it's not displayed. – chiapa Jul 25 '14 at 15:13
  • I don't know why this is happening because I tried the code from your jsfiddle and it works fine for me with every image size. Though I did not try loading the image dynamically, I only tried it with static images. – raw_orb Jul 25 '14 at 15:30
  • Are you trying with images from your local machine? – chiapa Jul 25 '14 at 15:35
  • Yes, I am using a wamp localhost server – raw_orb Jul 25 '14 at 15:36
  • I don't know what that is – chiapa Jul 25 '14 at 15:50
  • wamp is just a software with which you can create a virtual server on your PC. Its mainly used for testing server side scripts. – raw_orb Jul 25 '14 at 15:55