6

I am aware of resizing images bases on width:

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

or

img{
  width: 500px;
  height: auto;
}

Is there a way to do it by height? This does not seem to work:

img{
  height: 500px;
  width: auto;
}

How can I do this?

3
  • The percentages you use is a percentage of the container that the element is in. You CAN do a height percentage or actual px should work as well, just don't specify the width? Commented Mar 13, 2014 at 20:02
  • It seems working to me: jsbin.com/sesul/1/edit Commented Mar 13, 2014 at 20:04
  • can you add some HTML code where this doesnt seem to be working ? :o Commented Mar 13, 2014 at 20:16

2 Answers 2

4

From what you have shown it should work fine.

img{
  height: 500px;
  width: auto;
}

The above code does work, make sure it's not in a restrictive div, or another style is overwriting it

Sign up to request clarification or add additional context in comments.

Comments

0

You in fact can explicitly declare a height of 100px (for example) with height: 100px;. If your code isn't working this way, there may be something else outside of this context that is causing the problem.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.