0

I'm using the code below to display an image as a background in a div. How do I resize it?

#header {
    height: 255px;
    -webkit-background-size:cover;
    -moz-background-size:cover;
    background-size:cover;
    background-position: 50% 50%;
    background-image: url('../simg/header.png');
}
0

3 Answers 3

3

You can try controlling the size of the background by using the background-size property.

E.g.:

#header {
height: 255px;
-webkit-background-size:50%;
-moz-background-size:50%;
background-size:50%;
background-position: 50% 50%;
background-image: url('../simg/header.png');
}

This will reduce the background image to exactly half in either dimension.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks I was using "cover" and that was throwing me off. one other question when I resize the browser how would I set the "height" to auto adjust with the background-size?
You can use media queries to control the height based on size of the browser window.
1

try Easy Background Resize .js http://johnpatrickgiven.com/jquery/background-resize/

1 Comment

I don't think he's looking for JS here.
0

Another approach you can try is to set the css

background-size:contain;  
width:50%; 
max-height:255px; 

and let the browser figure out the height which results in it maintaining the aspect ratio.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.