0

I have an div

which I keep box shaped all the time using:

div{
    float:left;
    width:  447px;
    height:     445px;
    background-color: blue;
}


div:after{
   content: "";
   display: block;
   padding-bottom:100%
}

the problem occurs when I want to add an image to the div

<div><img src="as.jpg"></div>

using:

img{
  width:100%;
  height:100%;
}

Does one of the two things:

  1. the image does not cover whole div

  2. the image does not cover whole div and box shape is ruined

Is there a way how to fix this? Demo

Bomskie
  • 171
  • 8
Darlyn
  • 4,715
  • 12
  • 40
  • 90

2 Answers2

3

Use the image as background-image.

Example :

div {
  display: block;
  width: 500px;
  height:500px;
  background: url('http://www.nasa.gov/sites/default/files/images/729223main_728322main_messenger_orbit_image20130218_2_full_full_full.jpg') no-repeat;
  background-size: contain;
}
<div>
</div>

If you would like to use bootstrap framework, its simpler.

Example (using Bootstrap):

<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">

<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<!-- Latest compiled JavaScript -->
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<div class="row">
  <div class="col-sm-3">
    <img src="http://images.all-free-download.com/images/wallpapers_large/love_wallpaper_vector_3d_wallpaper_261.jpg" class="img-responsive" style="padding-top: 5px">
  </div>
</div>

You may also do it the usual way, just set the height of the image.

Image in a div, with image height defined :

<div>
  <img src="http://images.all-free-download.com/images/wallpapers_large/love_wallpaper_vector_3d_wallpaper_261.jpg" alt="image" height="200px" />
</div>
Ani Menon
  • 27,209
  • 16
  • 105
  • 126
  • the background-image works fine , but what if i want more divs with different images? Thats just mor classes. Is there a way using img inside? That would also work for others element , in case i do not want to add just an image. – Darlyn May 13 '16 at 11:59
  • @trolkura read through again. I have given three solutions. Use the third one(__with image height defined__) if you just want img in div. – Ani Menon May 13 '16 at 13:52
0

That is happening because the parent element, hence the div.small hasn't its height explicitly set.

For elements height is auto by default, meaning every element has height equal its contents. Contents of an element determine its height.

For an element to have height: 100%, hence to use percentage to fit in container's height, you should determine the height of the parent in absolute value.

Try this:

Try set for example height:400px; to div.small and height: 100%; for the image. Then the image will fit its container.

Check the fiddle here.

Update

Also, please check this tutorial by lynda.com, as it provides an explanation about elements height. A bit old, but very good stuff.

gdyrrahitis
  • 5,598
  • 3
  • 23
  • 37