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

HTML How to Make a Website Styling Web Pages and Navigation Make a CSS Image Gallery

Tommaso Giuseppini
PLUS
Tommaso Giuseppini
Courses Plus Student 646 Points

How to add CSS that will allow all images to fill their parent element

in this correct? img { max-with: 100%; }

1 Answer

Codin - Codesmite
Codin - Codesmite
8,600 Points

max-width can be any size up until the declared size. In your example max-width: 100%; could mean any size that is either 100% or less.

You would want to declare width: 100% to set it to always be 100% without exception.

img {
   max-width: 100%; /* Any size that is 100% or less */
   min-width: 100%; /* Any size that is 100% or more */
   width: 100%;     /* 100% width without exception */
}

Examples:

/* img would be rendered at 30% width */
img {
   max-width: 100%;
   width: 30%;
}
/* img would be rendered at 90% width */
img {
   max-width: 90%;
   width: 100%;
}
/* img would be rendered at 70% width */
img {
   min-width: 70%;
   width: 30%;
}
/* img would be rendered at 100% width */
img {
   width: 100%;
}