html - 小文本和大图像并排,使用 flexbox

标签 html css flexbox

我有一些关于“foo”和“bar”图像的简短文本。我想并排对齐图像和这些相应的文本,目前我使用 ::after 伪元素实现了它。

但也许有更好的方法来实现它,也许是使用 flexbox?

JS fiidle

<style>
  .image { width: 510px; float: right; }

  p::after {
    content: " ";
    visibility: hidden;
    display: block;
    height: 0;
    clear: both;
  }
</style>

<div class="image">
  <img src="Foo.jpg">
  <p class="caption">Caption</p>
</div>

<p>Text about Foo. Text text text text text text text text text 
text text text text text text text text text text text text text 
text text</p>

<div class="image">
  <img src="Bar.jpg">
  <p class="caption">Caption</p>
</div>

<p>Text about Bar. Text text text text text text text text text 
text text text text text text text text text text text text text 
text text</p>

最佳答案

是的!您可以使用 CSS Flexbox。但是你必须稍微改变一下你的结构。

看看下面的代码片段(我稍微缩小了图像的尺寸以使其适合框架):

.image {
  display: flex;
  align-items: center;
}

.image img {
  max-height: 200px;
  margin-right: 15px;
}
    <div class="image">
      <img src="http://i.imgur.com/yyJ04Sa.png">
      <div>
      <p class="caption">Caption</p>
          <p>Text about Foo. Text text text text text text text text text 
    text text text text text text text text text text text text text 
    text text</p>
      </div>
    </div>

    <div class="image">
      <img src="http://i.imgur.com/iWUPy0n.png">
      <div>
      <p class="caption">Caption</p>
          <p>Text about Bar. Text text text text text text text text text 
    text text text text text text text text text text text text text 
    text text</p>
      </div>
    </div>

希望这对您有所帮助!

关于html - 小文本和大图像并排,使用 flexbox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40688856/

相关文章:

html - 水平放置 DIV 元素(CSS 和 HTML)

css - 在 IE10 中使用 flex 布局的响应图像

html - 限制显示 Sprite 表作为文本框背景

javascript - 如何从网站中提取动态生成的 HTML

css - 更少的 CSS - html 中的类声明?

html - Bootstrap 0边距

javascript - 计算函数在 VueJS 中被调用两次

html - 当祖先显示 : flex 时,IE11 表格布局会中断

html - 无法使 flexbox 垂直和水平居中元素

PHP 包含不会在 Firefox 中显示