Flexbox包装特定内容

时间:2018-03-09 23:04:49

标签: html css flexbox

如何使用flexbox将content部分很好地包装到图像的右侧?

.container {
  display: flex;
  align-items:center;
  flex-wrap:wrap; /* we force a wrap so textarea is in the next line*/
}
.content {
  flex-grow: 1;
}
textarea {
  flex-basis: 100%;
  margin-left:80px; /* use margin to adjust, change this value depending the icon and image width */
}
img {
  border-radius: 30px;
  margin: 0 10px;
}
<div class="container">
  <div>
    x
  </div>
  <!-- better remove the div around the image, it's useless and avoid having a white space issue -->
  <img src="http://via.placeholder.com/50x50">
  <div class="content">
    <div>Brad</div>
    <div>When this text is long it goes to the next line and it doesn't wrap nicely. It should stay right of the image</div>
  </div>
  <textarea></textarea>
</div>

3 个答案:

答案 0 :(得分:2)

你的css需要更改,请看下面的新代码:

.container {
  display: flex;
  align-items:center;
  flex-wrap:wrap; /* we force a wrap so textarea is in the next line*/
}
.content {
  flex: 1;
}
textarea {
  flex-basis: 100%;
  margin-left:80px; /* use margin to adjust, change this value depending the icon and image width */
}
img {
  border-radius: 30px;
  margin: 0 10px;
}
<div class="container">
  <div>
    x
  </div>
  <!-- better remove the div around the image, it's useless and avoid having a white space issue -->
  <img src="http://via.placeholder.com/50x50">
  <div class="content">
    <div>Brad</div>
    <div>When this text is long it goes to the next line and it doesn't wrap nicely. It should stay right of the image</div>
  </div>
  <textarea></textarea>
</div>

答案 1 :(得分:0)

您可以应用与其下方的textarea相同的弹性基础,以确保它们始终保持相同的大小:

.container {
  display: flex;
  align-items:center;
  flex-wrap:wrap; /* we force a wrap so textarea is in the next line*/
}
.content {
  flex-basis: calc(100% - 80px);
}
textarea {
  flex-basis: calc(100% - 80px);
  margin-left:auto;
}
img {
  border-radius: 30px;
  margin: 0 10px;
}
<div class="container">
  <div>
    x
  </div>
  <img src="http://via.placeholder.com/50x50">
  <div class="content">
    <div>Brad</div>
    <div>When this text is long it goes to the next line and it doesn't wrap nicely. It should stay right of the image</div>
  </div>
  <textarea></textarea>
</div>

答案 2 :(得分:0)

您还可以使用display: grid来实现布局。 例如:

.container {
  display: grid;
  grid-template-columns: 10px 50px 1fr;
  grid-template-rows: 1fr auto;
  grid-gap: 0 10px;
}

img {
  border-radius: 30px;
}

textarea {
  grid-row: 2;
  grid-column: 3
}

.close {
  margin: auto;
}
<div class="container">
  <div class="close">
    x
  </div>
  <img src="http://via.placeholder.com/50x50">
  <div class="content">
    <div>Brad</div>
    <div>When this text is long it goes to the next line and it doesn't wrap nicely. It should stay right of the image</div>
  </div>
  <textarea></textarea>
</div>