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

Image Resize

HI,

I am trying to get my image width to span 100% of the screen while maintaining its proportions however I don't want to take up more then 300-400px in height. this might not make sense to all so i have attached a website that shows what i am trying to do. http://www.shanewarne.com

Thanks

Hi Miguel,

This would just be width: 100% and height: 300px in your CSS.

For example:

css"' .image { width: 100%; height: 300px; } "'

Hope this helps, my net is slow, so sorry if this reply doesn't show properly :D

6 Answers

Miguel, there are a couple cool methods for learning how someone else may have coded a website. In your example there I pulled up the 'Web Inspector' in Safari (Developer Tools in Chrome), and what this does is allows me to go through the code, while at the same time viewing the page.

So for your example, on that website this is the line of code that contains that entire header image (specifically it is in a DIV ID'ed as "banner", and each banner has a div class).

<div id="banner" style="text-align: left; float: none; position: absolute; top: 0px; right: auto; bottom: auto; left: 0px; margin: 0px; width: 11760px; height: 450px; opacity: 1; z-index: auto;">
<div class="banner three" style="width: 1680px;">

It looks like they are manually setting the width of the item in pixels( 1680px ), and setting a maximum height for the banner ID as well. Depending on how your website looks, you might even be able to use 100% for the width and constrain the height to the size your desired (somewhere between 300-400px).

+rep for recommending developer tools

If you want to accomplish something similar to this, I would advise looking at the styles of this site with developer tools on Chrome or Firefox.

At it's most basic level, what you need to do to get this effect is to call your image as a background-image with background-size set to "cover", then declare a height and set overflow to hidden. The site you referred to uses a height of 450px.

so

.fixed-proportion-background { position:relative; background: url(/your-url.png); background-size:cover; height:450px; width:100%; overflow:hidden; }

You can then play around with background positioning to get the look right.

Thanks for everyones replies, I used Michaels suggestion and it worked quite well. I understand the developer tools but the css in the develop section is confusing me cause there is a lot crossed out and not much css.

thanks for your help.

One more question on background-position: can i only use top, bottom, centre etc? or can i use numbers?

See here: http://www.w3schools.com/cssref/pr_background-position.asp

You can also use percentages or CSS Units (px, em, rem etc.)

I have another problem guys, On the same website you can see the info box that goes on top of the main picture, when i make my background another color it doesn't change when i preview in my browser? is that because the banner is already a background? do i use positioning? I have tried to use the inspect tools but i couldn't figure it out.

THANKS

Hi again Miguel,

That info box is a div of it's own. That means that it is on top of the full-width image behind it.

If you give this div a class, then in your CSS you will be able to style and position it how you want.

Hope this helps

I have the background pic in a div class of banner. I then have a info box class div nested inside the banner div. is that right? I can get the styling to work except the background color of the info box. When I view all the changes in the browser. They all update except the background color.

It's hard to say what is causing the issue without seeing it. If you look at your info box div in developer tools do you see the background style being applied or is it crossed out?

If it is crossed out then a different style somewhere else in your stylesheet is overriding the background color you are applying.

If you don't the see the background color style at all in developer tools then you might just have a spelling or syntax error in your stylesheet. Do you have a live version of the site you are working on somewhere you could link to?