Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

max-height: x% doesn't work on Chrome

I need to use css style like max-width:90% and max-height:90% to define image size not overflow the windows. However, this work well on Safari but not work on Chrome. Here is the demo I write on jsfiddle: http://jsfiddle.net/hello2pig/rdxuk7kj/

<style>
div{
    padding: 0;
    margin: 0;
}
.slide{
    text-align:center; 
    background-size: 100% 100%;
}
.user-img{
    max-height: 80%;
    max-width: 90%;
}
</style>

<body>
   <div class="slide">
      <div id="container0" class="container slideDown front">
         <div class="image-container">
         <img src="http://people.cs.clemson.edu/~bli2/hiOne/image/userImage/1.jpg" class="user-img" ></img>         
         </div>                 
      </div>
   </div>
</body>

If you open this demo in safari, whole image can be displayed, but image overflow the window on Chrome. Any method to fix the problem? Thanks for your help!

like image 874
Rambo Li Avatar asked Jan 16 '15 21:01

Rambo Li


2 Answers

You need to give an explicit value for the container. This would work:

.image-container {
    width: 500px;
    height: 300px;
}

In your case, the % is taken from the <html> element in the fiddle.

From MDN:

percentage

The percentage is calculated with respect to the height of the containing block. If the height of the containing block is not specified explicitly, the percentage value is treated as none.

like image 166
Mosho Avatar answered Nov 04 '22 21:11

Mosho


Some times using percentages for fluidity in layouts is tricky because you have to deal with containers and border-type things.

You might prefer to use viewport units. You can learn about them on css-tricks and caniuse will show you how well it's supported.

Essentially you can say:

<div style="height: 55vh;">Hi</div>

meaning a div element of 55vh height where 1vh is defined as the value of 1% of the viewport's height. Something that is 100vh will be 100% of the viewport's height.

like image 15
Joe Hansen Avatar answered Nov 04 '22 23:11

Joe Hansen