max-height in conjunction with width

I've got an app that I've developed that allows users to take pictures/upload it to a database of images.  Obviously they can take pictures that are either landscape or portrait - and I'm not quite sure how to properly handle the CSS with respect to that.

In the event that the picture is landscape - I want the width to be 100%.  So, I set the CSS to "width:100%;"

If the picture is portrait, however, there's decent odds that having the width be 100% will result in an image that forces the user to scroll down on the device...(in other words - 100% width causes the image to be too large to be encapsulated on the screen along with the other elements) - so I'd *like* the max-height to be like 70% or so.

How would I go about accomplishing both?  I've tried various combinations of "max-height: 70%; max-width:100%;" etc...but that's not it.  :)  Is there another approach that would work?

Thanks!
LVL 2
erzoolanderAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
You need to constrain your display box to a certain height width and then use

max-width: 100%;
max-height: 100%;

Sample code below - working sample here
The sample demonstrates how you can switch between landscape / portrait in the same box and keep the proportions of the image.
<!doctype html>
<html>
<head>
<title>Test</title>
<link href="css/bootstrap.css" rel="stylesheet" />
<style type="text/css">
img {
  max-width: 100%;
  max-height: 100%;
}
.image-box {
  height: 500px;
}
</style>
</head>
<body>
<div class="container">
  <h4>Demonstration of Portrait / Landscape images</h4>
  <h5>Author: Julian Hansen, August 2015</h5>
  <p>Click the images below to switch them. Change the height of the image box by modifying the value in the input below</p>
  <p>Height <input type="text" id="theheight" value="500" /></p>
  <div class="image-box">
    <img src="images/portrait.jpg" />
  </div>
  <div class="image-box" style="display: none">
    <img src="images/landscape.jpg" />
  </div>
</div>

<script src="http://code.jquery.com/jquery.js"></script>

<!-- Latest compiled and minified JavaScript -->
<script src="js/bootstrap.js"></script>
<script>
$(function() {
  $('#theheight').keyup(function() {
    var height = parseInt($(this).val());
    $('.image-box').css({height: height + 'px'});
  });
  $('.image-box').click(function() {
    $('.image-box').toggle();
  });
});
</script>
</body>
</html>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
erzoolanderAuthor Commented:
Gracias -
0
Julian HansenCommented:
You are welcome.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
CSS

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.