2

The code below gives this result:

enter image description here

Which is very close to the result I want except three facts:

  1. Why is there a very odd grey area at the bottom of the picture?
  2. How can I make the orange div extend to the bottom of the picture? I tried height: 100% but it didn't work...
  3. How can I also give a 10 pixels space between the div of the text and the picture?

Desired result:

enter image description here

div.div1 {
  background-color: yellow;
  border: 1px solid black;
  padding: 10px;
  overflow: auto;
}

div.div2 {
  border: 1px solid gray;
  float: right;
}

div.div3 {
  background-color: orange;
  border: 1px solid gray;
  height: 100%;
  padding: 10px;
}
<div class="div1">
  <div class="div2">
    <img src="http://splendidwillow.com/wp-content/uploads/2012/04/Allium-Purple-garlic-flowers-200x200.jpg">
  </div>
  <div class="div3">
    Text about flowers
  </div>
</div>
Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
darkchampionz
  • 1,174
  • 5
  • 24
  • 47
  • Assume your desired result as table. You can create same using div's with display:table and two display:table-cell. – divy3993 Mar 09 '17 at 17:09
  • The thing here is that the `parent div` the yellow part does not have a fixed height which kinda not allow the child to expand unless you give it the height of the image – Oke Tega Mar 09 '17 at 17:26

2 Answers2

1
  1. Surely img's margin-bottom, set it to 0.
  2. Flexbox surely is your friend here.
  3. Same as 1, but with margin-left in img and margin-right in .div2, both of 5.
RompePC
  • 815
  • 1
  • 8
  • 17
1

div.div1 {
  display: flex;            /* 1 */
  padding: 10px;
  overflow: auto;
  background-color: yellow;
  border: 1px solid black;
}

div.div2 {
  order: 1;                 /* 2 */
  border: 1px solid gray;
}

div.div3 {
  flex: 1;                  /* 3 */
  margin-right: 10px;
  padding: 10px;
  /* height: 100%  */       /* 4 */
  background-color: orange;
  border: 1px solid gray;
}

img {
  vertical-align: bottom;   /* 5 */
}
<div class="div1">
  <div class="div2">
    <img src="http://splendidwillow.com/wp-content/uploads/2012/04/Allium-Purple-garlic-flowers-200x200.jpg">
  </div>
  <div class="div3">
    Text about flowers
  </div>
</div>

Notes:

  1. Establish flex container. By default, children will line-up in a row (flex-direction: row) with equal height (align-items: stretch).
  2. Make image appear last in visual display (default order value for all flex items is 0)
  3. Make orange box consume all available space in the row.
  4. Remove defined heights on flex items. They will override align-items equal height feature.
  5. Mystery white space underneath image tag
Community
  • 1
  • 1
Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701