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 ...
 
随机推荐
- 26. Remove Duplicates from Sorted Array(删除排序数组中的重复元素,利用排序的特性,比较大小)
			
Given a sorted array, remove the duplicates in-place such that each element appear only once and r ...
 - Java 和 Python 解析动态 key 的 JSON 数据
			
一.概述 解析JSON过程中,什么情况都可能遇到.遇到特殊的情况,不会怎么办?肯定不是设计的问题,一定是你的姿势不对. 有这样一种JSON需要解析: { "b3444533f6544&quo ...
 - 随机生成气泡碰撞(原生js)
			
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>随 ...
 - Python numpy有什么用?
			
NumPy is the fundamental package for scientific computing with Python.就是科学计算包. a powerful N-dimensio ...
 - struts2中的错误--java.lang.NoClassDefFoundError: org/apache/commons/lang3/StringUtils
			
2013-4-7 10:13:56 org.apache.catalina.startup.HostConfig checkResources 信息: Reloading context [/chap ...
 - centos配置用户级别的jdk的环境变量
			
前面讲解了centos配置jdk的环境变量 的root级别的jdk配置 ,这里讲解用户级别的jdk配置. 在用户的当前目录下,如下,有四个隐藏的文件,文件打头是.bash******: 1.编辑.ba ...
 - nodejs 8 利用原生 util.promisify() 实现 promise.delay()
			
Nodejs 8 在 util 包里新增了 promisify() .这个方法基本和 bluebird 的 promisify() 作用一样,即把最后一个参数是 callback 函数的函数变成返回 ...
 - linux内核启动时报错ubi0 error: validate_ec_hdr: bad VID header offset 256, expected 64
			
1.详细错误报告如下: ubi0 error: validate_ec_hdr: bad VID header offset 256, expected 64 ubi0 error: validate ...
 - UVa 11021 麻球繁衍
			
https://vjudge.net/problem/UVA-11021 题意:有k只麻球,每只活一天就会死亡,临死之前可能会生出一些新的麻球.具体来说,生i个麻球的概率为Pi.给定m,求m天后所有麻 ...
 - 山东省第四届ACM程序设计竞赛部分题解
			
A : Rescue The Princess 题意: 给你平面上的两个点A,B,求点C使得A,B,C逆时针成等边三角形. 思路: http://www.cnblogs.com/E-star/arch ...