【问题标题】:Centering adjacent images with flexbox使用 flexbox 使相邻图像居中
【发布时间】:2020-08-01 05:35:05
【问题描述】:

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

  1. 单个图标应缩放到容器高度并水平居中。
  2. 应将少量图标缩放到容器高度并直接相邻并居中绘制。
  3. 如果图标多于容器高度水平无法容纳的图标,则应缩放它们以使其适合而不改变其纵横比。

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

这是产生该输出的 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 完全适合这种情况,但我无法让它发挥作用。我觉得我错过了一些明显的东西。谁能指出来?

【问题讨论】:

  • 您的 HTML 代码无效 -- 没有 &lt;html&gt; 元素,&lt;style&gt; 应该在 &lt;head&gt; 元素内。
  • @Manjuboyz 是的,我希望第三张和第四张图片看起来一样,但使用了 flexbox。你的答案很好,只是稍晚一点,没有接受答案的解释。

标签: css image svg flexbox


【解决方案1】:

实际上,您只需删除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;
}

【讨论】:

    【解决方案2】:

    我猜这就是你要找的东西:

    .flex-container {
      display: flex;
      margin-right: 0;
      margin-left: 0;
      width: 2in;
      height: 0.5in;
      background: lightblue;
      margin-bottom: 1em;
      padding: 0;
      margin: 0;
      list-style: none;
    }
    
    .center {
      justify-content: center;
    }
    
    .flex-item {
      text-align: center;
      height: 100%;
      width: auto;
      object-fit: contain;
    }
    
    .block-container {
      width: 2in;
      height: 0.5in;
      background: lightblue;
      margin-bottom: 1em;
      text-align: center;
    }
    <body>
      One item displays centered at container height.
      <div class="flex-container center">
        <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 center">
        <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>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-20
      • 2020-11-08
      • 2023-03-24
      • 2011-01-16
      • 2014-09-03
      • 2016-09-15
      • 2016-09-14
      • 1970-01-01
      相关资源
      最近更新 更多