html - 在 flexbox 的图像下居中文本/内容?

标签 html css alignment flexbox line-breaks

我一直试图在 flexbox 中的垂直对齐图像下添加段落文本,但一直无法将其渲染到下一行。是否可以让文字在图片下方居中?

这是我拥有的:JsFiddle

HTML:

<section class="landing">
  <div class="left-half">
    <article>
      <img src="http://placehold.it/400x600" />
      <div class="text">
        <p>I need this text under the image</p>
      </div>
    </article>
  </div>
</section>

CSS:

.landing {
  display: flex;
}

.left-half {
  flex: 1;
  height: 100vh;
}

.left-half article {
  height: 100%;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.left-half article img {
  max-width: 50%;
  display: block;
}

.caption {
  display: block;
}

最佳答案

只需将 .left-half article 设置为 flex-direction:column; :

.left-half article {
    height: 100%;
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction:column;
}

JSFiddle Demo

关于html - 在 flexbox 的图像下居中文本/内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37577928/

相关文章:

php - 点击特定按钮后,访客 IP 被禁止 24 小时

html - 如何使导航上方的绿色条在 CSS 中 float

html - 将结构与表示分离 - html/css

java - 如何为 JavaFX TextField 提供 Swing 风格的低斜 Angular 边框?

html - 如何水平对齐溢出的文本?

html - 为什么html,body{height :100%} causing a scroll bar to appear?

javascript - 使用 bxslider 将小图像添加到图像 slider 标题?

html - div背景颜色未设置所有区域

css - 为什么 Bootstrap 导航下拉菜单向右对齐?

Qt QTableView - 使用 IsUserCheckable 时复选框的对齐方式