Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

flex 指定了宽度,依然会被压缩宽度 #80

Open
lovelmh13 opened this issue Jun 16, 2021 · 1 comment
Open

flex 指定了宽度,依然会被压缩宽度 #80

lovelmh13 opened this issue Jun 16, 2021 · 1 comment

Comments

@lovelmh13
Copy link
Owner

为指定宽度的元素设置 flex: none

flex: none 是 flex: 0 0 auto 的简写。

0: flex-grow: 0 表示放大比例为 0,有剩余空间也不放大

0: flex-shrink: 0 表示缩小比例为 0,表示空间不足,也不缩小

auto: flex-basis: auto 表示分配多余空间之前,项目占据的主轴空间,auto 即项目本来的大小

与之对应的还有 flex: auto ,即 flex: 1 1 auto。 会随着空间放大缩小

@lovelmh13
Copy link
Owner Author

前提:

包含了图片的容器设置了flex

image

.container {
  width: 6.4rem;
  height: 2rem;
  box-sizing: border-box;
  display: flex;
  .img {
    width: 2rem;
    height: 2rem;
    border-radius: 0.2rem;
  }
}

原因:

flex 布局默认不换行,所以宽度不够的时候,元素会被挤

解决办法:

让要固定宽高的元素不会变形

.img {
  width: 2rem;
  height: 2rem;
  border-radius: 0.2rem;
  flex: none; // 或者 flex: 0 0 auto
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant