0

I tried to make some kind of a gallery made out of blocks. So I searched for the question: "How to set height equal to width". After trying it out it worked. But the method they explained was the famous 'pading-bottom in % = width in %'. And of course since there is a padding my image can't be placed inside the box.

Is there a way to place the image inside the box?

If any of you know the answer or another method, please let me know.

.imagebox{
border: 1px solid red;
width:25%;
padding-bottom:25%;
float:left;
}
<!DOCTYPEhtml>

<html>

<head>

</head>

<body>

  <div class="imagebox">
    <img src="">
  </div>
  
  <div class="imagebox">
    <img src="">
  </div>
  
  <div class="imagebox">
    <img src="">
  </div>
  

 
</body>

</html>
takendarkk
  • 3,347
  • 8
  • 25
  • 37

1 Answers1

0

your code works OK, but then the img adds height to the div, so you should make div relative, and img absolute:

.imagebox {
  border: 1px solid red;
  width: 25%;
  padding-bottom: 25%;
  float: left;
  position: relative;
  overflow:hidden;
}

.imagebox img{
  position: absolute;
  width: 100%;
  height: auto;
}
<div class="imagebox">
  <img src="http://lorempixel.com/400/400/?a=1">
</div>

<div class="imagebox">
  <img src="http://lorempixel.com/400/400/?a=2">
</div>

<div class="imagebox">
  <img src="http://lorempixel.com/400/400/?a=3">
</div>
MoLow
  • 3,056
  • 2
  • 21
  • 41