Mike R
Mike R

Reputation: 21

Resize div if image is too large

Hello i am wondering how can i resize a div, to the height, width of a image + 15px padding?

#mainFrame {
  width: 200px;
  height: 200px;
  display: grid;
  grid-template-rows: 40px 1fr;
}

#header {
  background: black;
}

#content {
  display: grid;
  background: gray;
  padding: 10px;
  align-content: center;
  justify-content: center;
}
<div id="mainFrame">
  <div id="header"></div>
  <div id="content">
    <img src="https://via.placeholder.com/120x120">
  </div>
</div>

As you can see here I have a div of 200x200, and I want it to be the default size of the div. But if let's say the image is inside the #content div and it's bigger than default div size, 200x200 in this case, I want to stretch the whole mainFrame so it fits nicely. How can I archive that?

Upvotes: 1

Views: 43

Answers (1)

isherwood
isherwood

Reputation: 61063

It's pretty easy if you don't need to keep the grid layout. Is that critical here?

#mainFrame {
  /* display: grid;
  grid-template-rows: 40px 1fr; */
}

#header {
  background: black;
}

#content {
  min-width: 100px;
  min-height: 100px;
  display: inline-block;
  background: pink;
  padding: 10px;
}
<div id="mainFrame">
  <div id="header"></div>
  <div id="content">
    <img src="https://via.placeholder.com/120x120">
  </div>
</div>

Upvotes: 1

Related Questions