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.

CSS How to Make a Website CSS: Cascading Style Sheets Center the Wrapper

The wrapper has a specified width but the images contained within this div tag, go over the right side. Why?

I don't understand why the images don't conform to the width specified in the wrapper. why does part of the image stick out on the right side?

2 Answers

geoffrey
geoffrey
28,724 Points

It's because the wrapper has a fixed width of 940 px and the image is wider. You'll probably see later in the course that to fix it, you just have to assign a max-width property to the images and set the value to 100%. Another possibilty is to set the property overflow and the value hidden on the wrapper itself. However this approach might give an ugly result, as the image is going to be truncated.

img{
max-width: 100%;
}

Hi Geoffrey

Can you view the link below? Am not sure how to attach images http://port-80-0p31uza9o3.treehouse-app.com/

HTML Code: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Thania Abrahams | FED</title> <link rel="stylesheet" href="css/normalize.css"> <link rel="stylesheet" href="css/main.css"> </head> <body> <header> <a href="index.html" id="logo"> <h1>Thania Abrahams</h1> <h2>Front End Developer</h2> </a> <nav> <ul> <li><a href="index.html">Portfolio</a></li> <li><a href="about.html">About</a></li> <li><a href="contact.html">Contact</a></li> </ul> </nav> </header> <div id="wrapper"> <section> <a href="img/numbers-01.jpg"> <img src="img/numbers-01.jpg" alt=""> <p>Experimentation with color and texture.</p> </a> <a href="img/numbers-02.jpg"> <img src="img/numbers-02.jpg" alt=""> <p>Playing with blending modes in Photoshop.</p> </a> <a href="img/numbers-06.jpg"> <img src="img/numbers-06.jpg" alt=""> <p>Trying to create an 80's style of glows.</p> </a> <a href="img/numbers-09.jpg"> <img src="img/numbers-09.jpg" alt=""> <p>Drips created using Photoshop brushes.</p> </a> <a href="img/numbers-12.jpg"> <img src="img/numbers-12.jpg" alt=""> <p>Creating shapes using repetition.</p> </a> </section> <footer> <a href="http://twitter.com/thaniabrahams"><img src="img/twitter-wrap.png" alt"Twitter Logo"></a> <a href="http://facebook.com/tye1509"><img src="img/facebook-wrap.png" alt"Facebook Logo"></a> <p>© 2015 Thania Abrahams.</p> </footer> </div> </body> </html>

CSS Code: a { text-decoration: none; }

wrapper {

max-width: 940px; margin: 0 auto; padding: 0 5%; background-color: orange; }

Now the padding is 5% on the left and right side ye? But why is there still more space on the left side between the image and the end of the wrapper but less space on the right side between the image and the wrapper? Why is it not equal? There is no unordered list and so no bullet points pushing it more towards the right side anymore. If you look at my code I have removed them.

Can all of this be explained simply by native browser css?

geoffrey
geoffrey
28,724 Points

Hi Thania, that's pretty much the same issue than the previous time you posted here about that. I checked your code.

What you absolutely need to do in this case, is set this css rule in your stylesheet:

img{
 max-width:100%;
}

Why ? because as you stated, your #wrapper has a max-width of 940px. This means that if it contains images larger than its owns width, images are going to exceed the #wrapper's width. Which is what you have right now.

So first of all, you need to set the css snippet I gave you above, once done, you'll be able to play again with the padding of your #wrapper. You'll see that the padding will be indeed 5% on the left and right this time.

Hope that helps.

Yes and I now understand why the image goes over, but I do not understand why it is not placed flush against the left of the wrapper