惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
人人都是产品经理
人人都是产品经理
Cisco Talos Blog
Cisco Talos Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
V
V2EX
博客园 - 三生石上(FineUI控件)
Martin Fowler
Martin Fowler
WordPress大学
WordPress大学
D
Docker
S
SegmentFault 最新的问题
博客园 - 聂微东
美团技术团队
Apple Machine Learning Research
Apple Machine Learning Research
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Last Week in AI
Last Week in AI
M
MIT News - Artificial intelligence
F
Fortinet All Blogs
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
The GitHub Blog
The GitHub Blog
GbyAI
GbyAI
L
LangChain Blog
Vercel News
Vercel News
博客园 - 叶小钗
MongoDB | Blog
MongoDB | Blog
Stack Overflow Blog
Stack Overflow Blog
H
Help Net Security
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
The Cloudflare Blog
Engineering at Meta
Engineering at Meta
T
Threat Research - Cisco Blogs
T
Threatpost
Scott Helme
Scott Helme
T
Tailwind CSS Blog
Latest news
Latest news
Stack Overflow Blog
Stack Overflow Blog
Blog — PlanetScale
Blog — PlanetScale
The Register - Security
The Register - Security
罗磊的独立博客
P
Proofpoint News Feed
腾讯CDC
S
Schneier on Security
雷峰网
雷峰网
A
About on SuperTechFans
T
Tenable Blog
F
Full Disclosure
Cyberwarzone
Cyberwarzone
博客园_首页
有赞技术团队
有赞技术团队
K
Kaspersky official blog

文章列表

win or mac clash 无 TUN 让 Antigravity、Chrome 强制 proxy(解决 Antigravity 无法加载选择 model、自动更新无法登录、跳转) 【大杂烩】在 pnpm 中直接修改 node_modules(.pnpm) 中的依赖项,项目中持久化 - pnpm 中的依赖处理、幽灵依赖、寻址规则等 在 html 中直接使用 Esm、Jsx 脚本快速调试和使用 React@19 和 Vue@3 源码,解决 React19 UMD 构建等问题 一键在本地批量检测并升级更新 package.json 中的模块依赖,ncu(npm-check-updates)在 npm、pnpm 或 workspace 项目中的使用教程 解决 Mac Docker Desktop 中启动出现的问题合集 通过阿里云、腾讯云无服务器搭建自定义的企业域名邮箱,实现在 QQ邮箱 收发等功能(附腾讯 SMTP 和 IMAP) 解决使用代理(clash 等)进行 SSH 连接(如 Github ssh key clone/push)出现 kex_exchange_identification 错误 静态文件资源 cdnjs, jsdelivr 抖音字节国内快速 CDN 镜像推荐【2025】- 仍在使用 bootcdn 和 staticfile CDN 请注意验证资源的完整性(SRI) pnpm monorepo 中管理依赖的最佳实践,与 Catalogs(目录)协议的使用(monorepo 中统一版本管理) Web 安全中的 Secure Contexts(安全上下文)- 解决在本地中使用 clipboard 或 Crypto 等 API 限制或关闭上下文限制 使用 serve 配合 openssl 或 mkcert 创建本地自签名可信任的证书 - 创建本地 TLS\SSL https 协议服务 利用 Github Actions 和 Acme 自动申请、更新和部署至阿里云、腾讯云 CDN Lets Encrypt SSl\TLS ECC RSA 双证书 在线工具 - 一键获取下载抖音无水印视频、抖音去水印解析工具、下载抖音无水印高清图集【2025 最新】 【React Router】v6 data router 在非组件(或工具方法)中如何优雅的跳转路由 【React】为什么路由跳转时页面滚动高度不会被重置(保留上个页面高度)?理解 history scrollRestoration 的场景与使用,以及如何使用 React Router 重置和跳转前保留滚动高度
【CSS】解决在 flex 容器中使用 align-content 或 justify-content 属性 center 居中时的溢出滚动和截断问题 - 理解 safe 关键字
kshao · 2024-08-01 · via

场景

在布局时,我们经常使用 flex 来实现居中效果。例如下图场景,可能是平时比较常见的,列表中的子元素居中效果。

<style>
  .flex-wrap {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 300px;
    height: 150px;

    & > div {
      width: 100px;
      height: 80px;
    }
  }
</style>

<div class="flex-wrap">
  <div>1</div>
  <div>2</div>
</div>

flex 容器限制了宽度,且内容不够 flex 元素分配时,此时 flex 元素将会溢出容器

使用 overflow

出现上述情况时,可能第一反应是给 flex 容器添加 overflow-y: auto 来解决。如下图,会发现 flex 容器左侧的内容仍旧无法查看。

为什么 justify-content: center; 会溢出,为什么 overflow 不是从溢出开始的地方滚动?

justify-content 是控制元素在容器主轴方向上的属性。如下图,设置 justify-content: center; 后,元素向主轴的中心进行排列,当 flex 容器限制宽度后,且剩余可分配空间为负数后,则两端溢出的长度相等。

css-flexbox > W3C Css3-flexbox 中文

W3C 中,滚动的原点是矩形的锚点坐标(默认左上角,会被 direction影响)开始由 padding box 盒模型组成的矩形。且在任一轴上超出滚动原点的区域被视为负可滚动溢出区域:在此处呈现的内容对读者不可访问

Additionally, due to Web-compatibility constraints (caused by authors exploiting legacy bugs to surreptitiously hide content from visual readers but not search engines and/or speech output), UAs must clip any content in the negative scrollable overflow region (thereby behaving as if they had no scrollable overflow on the wrong side of the scroll origin).

scroll-origin

更换 direction

ltr => rtl

解决方案

safe 关键字

溢出问题在 交叉轴 上同样存在,可参考。

.flex-wrap {
  display: flex;
  /* 同,在交叉轴(垂直方向)上居中时也可使用 safe */
  align-items: center;
  justify-content: safe center;
}

[{"url":"https://static.ksh7.com/post/css-flex-center-safe/0085UwQ9ly1hs8s1ccmubj30mq0ao3yr.webp?imageMogr2/thumbnail/!50p","dataset":{"originPic":"https://static.ksh7.com/post/css-flex-center-safe/0085UwQ9ly1hs8s1ccmubj30mq0ao3yr.webp","thumbnail":""}},{"url":"https://static.ksh7.com/post/css-flex-center-safe/0085UwQ9ly1hs8s1cgt6ej30k20auaag.webp?imageMogr2/thumbnail/!50p","dataset":{"originPic":"https://static.ksh7.com/post/css-flex-center-safe/0085UwQ9ly1hs8s1cgt6ej30k20auaag.webp","thumbnail":""}}]

flex-item margin: auto

给首个 flex item 和 最后一个 flex item 分别添加 margin-left: automargin-right: auto

.flex-wrap {
  & > div:first-child {
    margin-left: auto;
  }
  & > div:last-of-type {
    margin-right: auto;
  }
}

[{"url":"https://static.ksh7.com/post/css-flex-center-safe/0085UwQ9ly1hs8sxi8cxbj30ka0aijrj.webp?imageMogr2/thumbnail/!50p","dataset":{"originPic":"https://static.ksh7.com/post/css-flex-center-safe/0085UwQ9ly1hs8sxi8cxbj30ka0aijrj.webp","thumbnail":""}},{"url":"https://static.ksh7.com/post/css-flex-center-safe/0085UwQ9ly1hs8sxi8v3jj30ji0acjrs.webp?imageMogr2/thumbnail/!50p","dataset":{"originPic":"https://static.ksh7.com/post/css-flex-center-safe/0085UwQ9ly1hs8sxi8v3jj30ji0acjrs.webp","thumbnail":""}}]

添加父级,再给父级添加 overflow: auto

添加父元素,麻烦但兼容。

.flex-parent {
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: auto;

  .flex-wrap {
    display: flex;
    gap: 10px;
  }
}

参考

css-align-3
css-overflow-3
Can’t scroll to top of flex item that is overflowing container
【布局技巧】Flex 布局下居中溢出滚动截断问题