简体   繁体   English

当方向为列时,Flex 收缩无法按预期工作

[英]Flex shrink not working as expected when direction is column

I've provided the following little snippet and sandbox below.我在下面提供了以下小片段和沙箱。 Images shrink to fit as expected when flex-direction is set to "row".当 flex-direction 设置为“row”时,图像会按预期缩小以适应。 However, when I set to "column", the images no longer shrink as expected.但是,当我设置为“列”时,图像不再按预期缩小。 I've tried setting the height as per other posts and cannot get it to work.我试过根据其他帖子设置高度,但无法正常工作。

Try it yourself in the sandbox by changing "row" to "column" under flex-direction.通过在 flex-direction 下将“行”更改为“列”,在沙箱中自己尝试一下。 You can change the window size of the sandbox to see how the images change sizes to fit.您可以更改沙箱的 window 大小,以查看图像如何更改大小以适应。 When set to column, the images just end up being 100% width of the parent.当设置为列时,图像最终只是父级的 100% 宽度。 I can't seem to figure this out!!我似乎无法弄清楚这一点!

CSS: CSS:

.container {
  background-color: blue;
  min-width: 0px;
  min-height: 0px;
  max-height: 400px;
  max-width: 400px;
}

.cell {
  display: flex;
  flex-wrap: wrap;
  flex-direction: row;
}
.div-item {
  flex: 1 0 50px;
}

.image-pic {
  display: block;
  min-width: 0px;
  min-height: 0px;
  max-width: 100%;
  max-height: 100%;
  object-fit: cover;
  opacity: 0.5;
}

React:反应:

import React from "react";
import "./styles.css";
import { Image } from "react-bootstrap";
import useravatar from "./user.jpg";

export default function App() {
  return (
    <>
      <div className="container">
        <div className="cell">
          <div className="div-item">
            <Image src={useravatar} className="image-pic" />
          </div>
          <div className="div-item">
            <Image src={useravatar} className="image-pic" />
          </div>
          <div className="div-item">
            <Image src={useravatar} className="image-pic" />
          </div>
          <div className="div-item">
            <Image src={useravatar} className="image-pic" />
          </div>
          <div className="div-item">
            <Image src={useravatar} className="image-pic" />
          </div>
          <div className="div-item">
            <Image src={useravatar} className="image-pic" />
          </div>
          <div className="div-item">
            <Image src={useravatar} className="image-pic" />
          </div>
         
         
        </div>
      </div>
    </>
  );
}

Check out the sandbox here:在此处查看沙箱:

https://codesandbox.io/s/flex-wrap-with-bootsrap-forked-808gd?file=/src/styles.css https://codesandbox.io/s/flex-wrap-with-bootsrap-forked-808gd?file=/src/styles.css

I am a beginner so I might be wrong but my idea to fix your problem is when you give a certain width and height to your image parent div you can control them.我是初学者,所以我可能是错的,但我解决问题的想法是,当您为图像父 div 提供一定的宽度高度时,您可以控制它们。 So with this way you can avoid them getting bigger when they wrap.因此,通过这种方式,您可以避免它们在包装时变大。 (Also you use flex: 1 0 50px but flex-basis only excepts % ) (您也使用flex: 1 0 50px但仅 flex -basis除外%

.cell {
  width: 100%;
  display: flex;
  flex:0 0 100%;
  flex-wrap: wrap;
  flex-direction: row;
}
.div-item {
  width:50px;
  height:50px;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM