1

I am trying to put an image into my Website. The image is in a div that hast got a fixed size. The Problem is that the image stretches the whole div when I use auto height in CSS.

The Image fits into its div setting its height and width to 100%:

enter image description here

Now I would like to keep the Image unstretched. So I set the width 100% and the height as auto as it is described here After setting that the image is in a layer under the section below but layers on the next part of the page.

enter image description here

here is the HTML Code I used:

<div class="section4">
        <section class="half">
            <div class="officePicContainer">
                <img src="officePic.jpg" alt="New Office of MEGO" class="officePic">
            </div>
        </section>

        <section class="half">


        </section>  
    </div>

And The CSS Code:

 .half {
    height: 50%;
    position: relative;
}

.half:first-child {


}
.half:last-child {
    background: #950049;

}
     .officePic {
        height: auto;
        width: 100%;
    }

How can I resize the image and fitting into its parent div without stretching it? Is it still possible in CSS? Or is Java Script needed?

Thanks for help!

Community
  • 1
  • 1
jublikon
  • 3,427
  • 10
  • 44
  • 82

2 Answers2

0

Create div and, the background-size:cover css tag and set position: fixed

<div class="demo" style="background-image:url(example.png);background-size: cover; background-position:center center;"></div>
Lennart
  • 120
  • 1
  • 11
0

<div><img src="http://www.engineering.com/Portals/0/BlogFiles/swertel/heart-cloud.jpg" width=100% /></div>
imGaurav
  • 1,043
  • 7
  • 14