Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

CSS

Scale an image?

I'm curious how to scale an image via CSS. I have a high resolution PNG file any help is appreciated.

Thanks.

3 Answers

Usually, if you're scaling a .png, you want to use specific width and height measurements.

First you need to give your image a class or and ID in your HTML.

<img src="../img/facebook.png" alt="facebook logo" class="social-icon">

Then you can set specific measurements in the CSS:

.social-icon {
  width: 20px;
  height: 20px;
}

Let's say you have an image 400px x 400px.

<img class="my-image" src="my_image.png" />

If you set the image height and width properties to 100px using css, then it will be 16 times smaller (4 times less in each dimension). You must not have width and height attributes on the img element itself*.

.my-image {
  height: 100px;
  width: 100px;
}

You can set percentage values for the dimensions, but these will be relative to the parent of the image, not the image size itself. E.g. if you have your image inside a container, and the container dimensions were 300px x 300px, then setting the height and width of the image to 50% would make the image 100px x 100px.

It's a little more complicated if you want the image to scale automatically. First you need to make sure that the image is in a scalable parent element.

<ul class="art-col">
            <li><h1 class="section-label">Arts!</h1></li>
            <li>
              <img src="../img/marquee.jpg" alt="Tampa Theater marquee" class="marquee">
              <p class="caption">The convenience of a movie theater with the history of an opera house.</p>
            </li>
</ul>

Then you need to make sure that all the images on the page are set to take up 100% of the width of the container they're in.

img, iframe {
  max-width: 100%;
}

Finally, you have to make sure that the container scales. Since the image is confined by the container, it will scale too,

.arts-col {
  margin: 0;
  padding: 0;
  list-style: none;
}

.arts-col li {
  width: 45%;
  margin: 2.5%;
}

Since the picture is in a li that will only be 45% of the screen, and the picture is set to take up 100% of that 45%, the picture will also always take up 45% of the screen.

I would recommend setting the image to display: block; as they are by default inline-block which can add unintended spacing.

You would only need to set the display to block if you wanted it to be on a line by itself. If you want the text to flow around the image, or appear next to it, you would set the display to inline.

In a responsive setup, the container would be display: inline and have the text flowing round it, not the image right?

The PNG in question is a logo for the website. I'm not sure if that makes a difference or not.

PNG or not, all images have a default display of inline-block. When inspecting your code, just make sure that it isn't adding unwanted spacing.