Thomas
Thomas

Reputation: 181735

How to stretch an image to within the padding of its parent?

This is what I want to achieve:

|                           |
|     bla bla text text     |
|     more text and bla     |
+---------------------------+
|IMAGE IMAGE IMAGE IMAGE IMA|
|GE IMAGE IMAGE IMAGE IMAGE |
|IMAGE IMAGE IMAGE IMAGE IMA|
+---------------------------+
|     and the text goes     |
|     on and on and on.     |
|                           |

The markup and CSS look something like:

<div class="container">
  <p>... text ...</p>
  <img>
  <p>... text ...</p>
</div>

.container {
  padding: 100px;
}

Note:

Things I considered:

Is there a clean way to achieve this?

Upvotes: 3

Views: 2525

Answers (2)

Deepak Yadav
Deepak Yadav

Reputation: 7069

.container {
  padding: 100px;
  border: 1px solid #000;
}
.img-holder { /* don't give it width property*/
  margin-left: -100px;
  margin-right: -100px;
}
p {
  border: 1px solid #f00
}
img {
  width: 100%;
}
<div class="container">
  <p>... text ...</p>
  <div class="img-holder">
    <img src="https://www.google.co.in/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png" alt="">
  </div>
  <p>... text ...</p>
</div>

Rest you can style your image as your requirement

Upvotes: 4

karan bhilware
karan bhilware

Reputation: 13

You can use

.container img{
width:100%;
}

its will maintain ratio as per width of parent object.

Upvotes: -1

Related Questions