flex布局在垂直居中里,元素超过容器大小后,不能通过滚动条滚动到顶端,这是个flex的bug
The Problem
Flexbox makes centering very easy.
By simply applying align-items: center and justify-content: center to the flex container, your flex item(s) will be vertically and horizontally centered.
However, there is a problem with this method when the flex item is bigger than the flex container.
As noted in the question, when the flex item overflows the container the top becomes inaccessible.

For horizontal overflow, the left section becomes inaccessible (or right section, in RTL languages).
Here's an example with an LTR container having justify-content: center and three flex items:

See the bottom of this answer for an explanation of this behavior.
Solution #1
To fix this problem use flexbox auto margins, instead of justify-content.
With auto margins, an overflowing flex item can be vertically and horizontally centered without losing access to any part of it.
So instead of this code on the flex container:
#flex-container {
align-items: center;
justify-content: center;
}
Use this code on the flex item:
.flex-item {
margin: auto;
}

Solution #2 (not yet implemented in most browsers)
Add the safe value to your keyword alignment rule, like this:
justify-content: safe center
or
align-self: safe center
From the CSS Box Alignment Module specification:
4.4. Overflow Alignment: the
safeandunsafekeywords and scroll safety limitsWhen the [flex item] is larger than the [flex container], it will overflow. Some alignment modes, if honored in this situation, may cause data loss: for example, if the contents of a sidebar are centered, when they overflow they may send part of their boxes past the viewport’s start edge, which can’t be scrolled to.
To control this situation, an overflow alignment mode can be explicitly specified.
Unsafealignment honors the specified alignment mode in overflow situations, even if it causes data loss, whilesafealignment changes the alignment mode in overflow situations in an attempt to avoid data loss.The default behavior is to contain the alignment subject within the scrollable area, though at the time of writing this safety feature is not yet implemented.
safeIf the size of the [flex item] overflows the [flex container], the [flex item] is instead aligned as if the alignment mode were [
flex-start].
unsafeRegardless of the relative sizes of the [flex item] and [flex container], the given alignment value is honored.
Note: The Box Alignment Module is for use across multiple box layout models, not just flex. So in the spec excerpt above, the terms in brackets actually say "alignment subject", "alignment container" and "start". I used flex-specific terms to keep the focus on this particular problem.
Explanation for scroll limitation from MDN:
Flexbox's alignment properties do "true" centering, unlike other centering methods in CSS. This means that the flex items will stay centered, even if they overflow the flex container.
This can sometimes be problematic, however, if they overflow past the top edge of the page, or the left edge [...], as you can't scroll to that area, even if there is content there!
In a future release, the alignment properties will be extended to have a "safe" option as well.
For now, if this is a concern, you can instead use margins to achieve centering, as they'll respond in a "safe" way and stop centering if they overflow.
Instead of using the
align-properties, just putautomargins on the flex items you wish to center.Instead of the
justify-properties, put auto margins on the outside edges of the first and last flex items in the flex container.The
automargins will "flex" and assume the leftover space, centering the flex items when there is leftover space, and switching to normal alignment when not.However, if you're trying to replace
justify-contentwith margin-based centering in a multi-line flexbox, you're probably out of luck, as you need to put the margins on the first and last flex item on each line. Unless you can predict ahead of time which items will end up on which line, you can't reliably use margin-based centering in the main axis to replace thejustify-contentproperty.
flex布局在垂直居中里,元素超过容器大小后,不能通过滚动条滚动到顶端,这是个flex的bug的更多相关文章
- 关于stl advance函数移动步数超过容器大小(越界)的研究
今天使用advance遇到个问题,当advance移动步数超过容器大小时,表现的结果居然不一样. 再来看下stl源码 template<typename _BidirectionalIterat ...
- 使用flex布局解决百分比高度元素垂直居中
方法一: align-self(解决父元素下面单个子元素布局方式) 父级加上 div{display:flex} 子元素 span { flex-grow: 1; align-self: center ...
- css flex布局,小程序flex布局,垂直居中完美解决
flex弹性布局,很好的解决了垂直居中的问题,上代码: wxml: <view class='container'> <view class='item item1'>item ...
- 使用flex布局,垂直居中
要完成下面的样式: 1:绿色部分宽度固定,红色部分自适应宽度: 2:整体高度自适应,红色和绿色部分的内容垂直居中: html代码: <div class="main"> ...
- flex布局控制最后一个元素右浮动
可以在最后一个元素添加css属性 margin-left: auto; 例如我一排排列的元素 ,子元素并没有完全排列撑开父元素的宽度,这时候要使最后一个元素想最右 可以让最后一个元素的 margin- ...
- flex布局 一行4个元素 后面不够4个元素对齐
html 父元素 .container { display: flex; flex-wrap: wrap;} 子元素.list { width: 24%; height: 100px; backgro ...
- flex布局入门总结——语法篇
前几天看了阮一峰的Flex布局教程,讲的很不错,总结一下,有兴趣的可以去看原文http://www.ruanyifeng.com/blog/2015/07/flex-grammar.html 一 F ...
- flex布局基本语法
注 : 本文章按照菜鸟教程 Flex布局语法教程为原型稍加修改,以方便自己学习. 菜鸟教程地址:http://www.runoob.com/w3cnote/flex-grammar.html 2009 ...
- Flex布局(CSS Flexbox)
参考:Flex 布局语法教程 Flex布局是什么? Flex是Flexible Box的缩写,意为”弹性布局”,用来为盒状模型提供最大的灵活性. 任何一个容器都可以指定为Flex布局 注意:设为Fle ...
随机推荐
- Java-idea-FindBugs、PMD和CheckStyle对比
一.对比 工具 目的 检查项 备注 FindBugs 检查.class 基于Bug Patterns概念,查找javabytecode (.class文件)中的潜在bug 主要检查bytecode中的 ...
- nodejs Async详解之二:工具类
Async中提供了几个工具类,给我们提供一些小便利: memoize unmemoize log dir noConflict 1. memoize(fn, [hasher]) 有一些方法比较耗时,且 ...
- mysql数据库表的查询操作-总结
转自:https://www.cnblogs.com/whgk/p/6149009.html 序言 1.MySQL表操作(创建表,查询表结构,更改表字段等), 2.MySQL的数据类型(CHAR.VA ...
- 模块讲解----XML模块
XML实现不同语言或者程序之间进行数据交换的协议XML文件格式如下: 1.浏览器返回的字符串格式类型有: a.HTML b.Json c.XML 2.XML格式如下: <data> #表示 ...
- Jitamin
安装环境要求 PHP 5.6或更高(推荐使用PHP7) 数据库, 推荐使用MySQL 或 PostgreSQL. 当然SQLite也可以运行. Composer 安装手册 一. 克隆代码 假设我们把j ...
- 使用Ajax验证用户是否已存在
在服务器端使用Servlet,里面在集合里存了几个字符串,没有对数据库操作. 前台input页面和Ajax验证: <%@ page language="java" conte ...
- Spring笔记一
什么是Spring spring (由rod johnson创建的一个开源框架) spring是一个开源框架,spring是于2003 年兴起的一个轻量级的java 开发框架,由rod johnson ...
- (转)WPF学习资源整理
由于笔者正在学习WPF,所以整理出网络中部分WPF的学习资源,希望对同样在学习WPF的朋友们有所帮助. 首推刘铁猛的<深入浅出WPF>系列博文 1.深入浅出WPF(1)——什么是WPFht ...
- ZW团队:IN_OUT传播模型简介
传统媒体,网络媒体的整合推广,我曾经提出过一个:Tn-Out模式 In-Out是NBA的篮球术语,你自己百度下 传统媒体承担"IN"的角色,负责传播的深度和建立公信力 网络媒体充当 ...
- vs显示行号
1.工具 2.选项 3.文本编辑器 4.C\C++ 5.常规->显示行号