7

I've tried every answer I could find on all the sites I could find, but still haven't been able to properly resize an image using CSS. I've got it inside a div, and tried resizing either one and resizing both. I'm trying to fit the image (underneath my navigation bar) to the page (meaning: as wide as the page, and relative height).

<div id="banner"><img src="resources/img/banner.png" class="myImage"></div>

First attempt:

.myImage{
    max-width: 100%;
    height: auto;
    left: 0px;
    right: 0px;
}

Second attempt:

#banner{
    max-width: 100%;
    height: auto;
    left: 0px;
    right: 0px;
}

Third attempt:

<div id="banner"><img src="resources/img/banner.png" id="myImage"></div>

#banner{
    max-width: 100%;
    height: auto;
    left: 0px;
    right: 0px;
}

#myImage{
    max-width: 100%;
    height: auto;
    left: 0px;
    right: 0px;
}
4
  • What goes wrong with your attempts? Seems to work fine for me. Commented Jul 29, 2014 at 20:46
  • Have you considered width:100%? You seem to have only tried max-width. Commented Jul 29, 2014 at 20:46
  • can you publish one screenshot or the image you're trying to resize? Commented Jul 29, 2014 at 20:49
  • Is this answer what you are looking for? Commented Jul 29, 2014 at 20:49

5 Answers 5

6

If your image is smaller than the screen, it will use the image width. If it is bigger, it uses max-width. So assuming your image is smaller than the display, you want to change your "max-width" to "width" to increase the image size.

<div id="banner"><img src="resources/img/banner.png" id="myImage"></div>

#banner{
    max-width: 100%;
    height: auto;
    left: 0px;
    right: 0px;
}

#myImage{
    width: 100%;
    height: auto;
    left: 0px;
    right: 0px;
}
Sign up to request clarification or add additional context in comments.

1 Comment

@JoseMagana, i hope it so ;)
1

The CSS property background:cover will help you!

html { 
 background: url(images/bg.jpg) no-repeat center center fixed; 
 background-size: cover;
}

Cover will extend your background to full screen.

Comments

0

CSS-Tricks has the best solution for this that I could find

html { 
  background: url(images/bg.jpg) no-repeat center center fixed; 
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
  background-size: cover;
}

Link to CSS-Tricks for the source and original code: https://css-tricks.com/perfect-full-page-background-image/

1 Comment

Consider adding a link to the original source where you found this solution so that they can be properly cited/credited.
0

Did you remember to link to your stylesheet? I was just having this issue, then I realized that I forgot to link to the CSS and face-palm.

Comments

-1

You have to give the id banner a specific width that is less than 100%. You don't need a height, it already is automatic. You have to target the image inside the banner and not the class added to the image. So it should look like this:

<div id="banner"><img src="resources/img/banner.png" class="myImage"></div>

#banner{
    width: 60%;
    left: 0px;
    right: 0px;
}

#banner img{
    width: 100%;
    left: 0px;
    right: 0px;
}

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.