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

How do I insert an Image as a div with specific proportions?

How do I insert an Image as a div with specific proportions?

1 Answer

Jamie Reardon
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Jamie Reardon
Treehouse Project Reviewer

You could use the background-image property for the image to replace the element's default transparent background. Then you can use the width and height property to give it the defined dimensions you want.

If you do add any padding for content breathing room, you could add the box-sizing property and value of border-box to ensure that your image element won't get any larger than the dimensions you defined.

Here I made an example with the div tag and used the background shorthand property to define the image.

div {
  background: orange url("URL_to_image") no-repeat center / cover;
  width: 200px: /* replace this value to your own */
  height: 200px; /* replace this value to your own */
  box-sizing: border-box;
}