css - 使用 flexbox 将相邻图像居中

标签 css image svg flexbox

我正在尝试使用 flexbox 在容器中水平居中显示数量可变的图像图标。

  1. 单个图标应缩放到容器高度并水平居中。
  2. 少数图标应缩放到容器高度并直接相邻并居中绘制。
  3. 如果图标的数量超过了容器高度水平容纳的数量,则应缩放它们以便在不改变纵横比的情况下适应。

我在 (1) 和 (3) 上都成功了,但是 (2) 给我带来了麻烦。图像不是彼此相邻绘制的。这是它的样子:

scaled/centered icon group test

这是生成该输出的 HTML 文档 (also jsfiddle):

<!DOCTYPE html>
<style>
.flex-container {
  display: flex;
  justify-content: center;
  width: 2in;
  height: 0.5in;
  background: lightblue;
  margin-bottom: 1em;
}

.flex-item {
  flex: 1 1;
  height: 100%;
  width: auto;
  object-fit: contain;
}

.block-container {
  width: 2in;
  height: 0.5in;
  background: lightblue;
  margin-bottom: 1em;
  text-align: center;
}
</style>
<body>
  One item displays centered at container height.
  <div class="flex-container">
    <svg class="flex-item" width="1.0055in" height="1.0055in" version="1.1" viewBox="0 0 72.395996 72.395999" xmlns="http://www.w3.org/2000/svg">
      <g transform="translate(72.198 -89.402)">
        <circle cx="-36" cy="125.6" r="36" fill="#fc0000" stroke="#000" stroke-linecap="round" stroke-width=".396"/>
      </g>
    </svg>
  </div>

  Many items get scaled to fit in the box, which is
  what I want. The image aspect ratio correctly remains
  unchanged.
  <div class="flex-container">
    <svg class="flex-item" width="1.0055in" height="1.0055in" version="1.1" viewBox="0 0 72.395996 72.395999" xmlns="http://www.w3.org/2000/svg">
      <g transform="translate(72.198 -89.402)">
        <circle cx="-36" cy="125.6" r="36" fill="#fc0000" stroke="#000" stroke-linecap="round" stroke-width=".396"/>
      </g>
    </svg> 
    <svg class="flex-item" width="1.0055in" height="1.0055in" version="1.1" viewBox="0 0 72.395996 72.395999" xmlns="http://www.w3.org/2000/svg">
      <g transform="translate(72.198 -89.402)">
        <circle cx="-36" cy="125.6" r="36" fill="#fc0000" stroke="#000" stroke-linecap="round" stroke-width=".396"/>
      </g>
    </svg> 
    <svg class="flex-item" width="1.0055in" height="1.0055in" version="1.1" viewBox="0 0 72.395996 72.395999" xmlns="http://www.w3.org/2000/svg">
      <g transform="translate(72.198 -89.402)">
        <circle cx="-36" cy="125.6" r="36" fill="#fc0000" stroke="#000" stroke-linecap="round" stroke-width=".396"/>
      </g>
    </svg> 
    <svg class="flex-item" width="1.0055in" height="1.0055in" version="1.1" viewBox="0 0 72.395996 72.395999" xmlns="http://www.w3.org/2000/svg">
      <g transform="translate(72.198 -89.402)">
        <circle cx="-36" cy="125.6" r="36" fill="#fc0000" stroke="#000" stroke-linecap="round" stroke-width=".396"/>
      </g>
    </svg> 
    <svg class="flex-item" width="1.0055in" height="1.0055in" version="1.1" viewBox="0 0 72.395996 72.395999" xmlns="http://www.w3.org/2000/svg">
      <g transform="translate(72.198 -89.402)">
        <circle cx="-36" cy="125.6" r="36" fill="#fc0000" stroke="#000" stroke-linecap="round" stroke-width=".396"/>
      </g>
    </svg> 
  </div>

  But if there are not enough images to fill the container
  then I want the images adjacent and centered. Instead
  they are distributed.
  <div class="flex-container">
    <svg class="flex-item" width="1.0055in" height="1.0055in" version="1.1" viewBox="0 0 72.395996 72.395999" xmlns="http://www.w3.org/2000/svg">
      <g transform="translate(72.198 -89.402)">
        <circle cx="-36" cy="125.6" r="36" fill="#fc0000" stroke="#000" stroke-linecap="round" stroke-width=".396"/>
      </g>
    </svg> 
    <svg class="flex-item" width="1.0055in" height="1.0055in" version="1.1" viewBox="0 0 72.395996 72.395999" xmlns="http://www.w3.org/2000/svg">
      <g transform="translate(72.198 -89.402)">
        <circle cx="-36" cy="125.6" r="36" fill="#fc0000" stroke="#000" stroke-linecap="round" stroke-width=".396"/>
      </g>
    </svg> 
  </div>

  This is what the above case should look like, but it
  doesn't use flexbox so it won't scale properly with
  many items.
  <div class="block-container">
    <svg class="flex-item" width="1.0055in" height="1.0055in" version="1.1" viewBox="0 0 72.395996 72.395999" xmlns="http://www.w3.org/2000/svg">
      <g transform="translate(72.198 -89.402)">
        <circle cx="-36" cy="125.6" r="36" fill="#fc0000" stroke="#000" stroke-linecap="round" stroke-width=".396"/>
      </g>
    </svg><svg class="flex-item" width="1.0055in" height="1.0055in" version="1.1" viewBox="0 0 72.395996 72.395999" xmlns="http://www.w3.org/2000/svg">
      <g transform="translate(72.198 -89.402)">
        <circle cx="-36" cy="125.6" r="36" fill="#fc0000" stroke="#000" stroke-linecap="round" stroke-width=".396"/>
      </g>
    </svg> 
  </div>
</body>

我真的认为 justify-content: center 正是针对这种情况的,但我无法让它发挥作用。我觉得我错过了一些明显的东西。有人可以指出吗?

最佳答案

实际上,您只需删除 flex:1 1;,因为它会使图像占用可用空间。

jsfiddle 示例:https://jsfiddle.net/gzxu5va8/2/

CSS:

.flex-container {
  display:flex;
  flex-direction:row;
  justify-content:center;
  width: 2in;
  height: 0.5in;
  background: lightblue;
  margin-bottom: 1em;
}

.flex-item {
  height: 100%;
  width: auto;
  display:flex;
  flex-direction:row;
  justify-content:center;
}

.block-container {
  width: 2in;
  height: 0.5in;
  background: lightblue;
  margin-bottom: 1em;
  display:flex;
  flex-direction:row;
  justify-content:center;
}

关于css - 使用 flexbox 将相邻图像居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61293274/

相关文章:

html - 在不破坏当前布局的情况下放置 Div 的位置

css - 无法定位首页边距

css - Joomla 3 编辑器删除内联 css

php - 如何缓存base64编码图像

javascript - 如何向 d3 生成的凸包添加标签?

css - 需要帮助并排对齐 3 个 div

image - 计算像素的积分图像(求和面积表)

python - Python 的 DXT 压缩

css - SVG 图像悬停动画未在 css 背景中显示

html - 将在 Illustrator 中创建的 SVG 文件链接到我的 html 并赋予其悬停效果的最简单方法是什么?