Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

General Discussion

scaling images in height, not width

Hi, is it possible to make an image scale in height, but cut off in width from the right?

Not sure how this would be done, css? or even if it's possible? :)

4 Answers

lordcozycat
lordcozycat
11,940 Points

Something like this?

img {
    max-height: 500px;
    width: 200px;
}

I would say it's for sure possible, but I'm not sure what you mean by cut it off from the right. Could you explain?

Hi, thanks for the reply Bryan :)

Similar to Lee McIntyre's site. I can see he's using a background image and when you scale the site down to mobile his pic reduces in height and then starts to cut off from the side, rather than it getting so small you can no longer see him.

http://www.getmoremomentum.com

lordcozycat
lordcozycat
11,940 Points

On that site, the image is set like so:

img {
    background-size: cover;
    width: 100%;
}

That sets it to cover (This keyword specifies that the background image should be scaled to be as small as possible while ensuring both its dimensions are greater than or equal to the corresponding dimensions of the background positioning area.), and width to 100%. That of course means that the image will fit to the size of it's container, which is set to scale with the page.

lordcozycat
lordcozycat
11,940 Points

I'd try setting width to whatever you need, then setting height to auto. Hope that works out for you.

Thanks Bryan. I'll give that a try :)