We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
若是行内元素,给改元素的父级设置text-align:center,即可实现行内元素水平居中。demo
text-align:center
若是块级元素, 该元素设置margin:0 auto。demo
margin:0 auto
若子元素包含float:left属性, 为了让子元素水平居中, 则可让父元素宽度设置为fit-content,并且配合margin, 作如下设置:demo
float:left
fit-content
margin
.fit-content { width: -moz-fit-content; width: -webkit-fit-content; width: fit-content; margin: 0 auto; }
目前这个属性只支持Chrome和Firefox浏览器。
使用flex布局,可以很轻松的实现水平居中:demo
.parent { display: flex; justify-content: center; } .son { background-color: red; width: 100px; height: 20px; }
.son 将会被水平居中
**使用CSS3盒模型,父元素display:box;box-pack:center;
display:box;box-pack:center;
.parent { display: -webkit-box; -webkit-box-orient: horizontal; -webkit-box-pack: center; display: -moz-box; -moz-box-orient: horizontal; -moz-box-pack: center; display: -o-box; -o-box-orient: horizontal; -o-box-pack: center; display: -ms-box; -ms-box-orient: horizontal; -ms-box-pack: center; display: box; box-orient: horizontal; box-pack: center; } .son { width: 100px; height: 10px; background-color: red; }
没有深入了解。 基本介绍:display:box;是09年出来的。display: flex;是12年出来的。地址:“Old” Flexbox and “New” Flexbox。 看文章说在手机页面的时候,老机子display:flex会有问题的时候就需要box上场啦。
display:box;
display: flex;
display:flex
通过使用决定对位,以及负值的margin-left来实现:demo
margin-left
.parent { position: relative; } .son { position: absolute; left: 50%; margin-left: -0.5 * 当前元素的宽度; width: 100px; height: 10px; background-color: red; }
使用CSS3中新增的transform属性,以及绝对定位:demo
transform
.parent { position: relative; } .son { position: absolute; left: 50%; transform: translate(-50%, 0); }
使用绝对定位,以及left:0;right:0;margin:0 auto;:demo
left:0;right:0;margin:0 auto;
.parent { position: relative; } .son { position: absolute; left: 0; right: 0; margin: 0 auto; width: 100px; height: 10px; background-color: red; }
元素是单行文本,设置line-height等于父元素的高度。demo
元素是行内块级元素, 基本思想是使用display: inline-block, vertical-align: middle和一个伪元素让内容块处于容器中央。demo
.parent { height: 100px; } .parent::after, .son{ display:inline-block; vertical-align:middle; } .parent::after{ content:''; height:100%; /*width: 10px;*/ /*background-color: blue;*/ } .son { width: 100px; height: 10px; background-color: red; }
通过父元素创建一个看不见的伪类(宽度为0),高度为100%,同时设置为inline-block和vertical-align:middle使元素垂直居中。 这是一种很流行的方法, 也适应IE7。
inline-block
vertical-align:middle
可用vertical-align属性,而vertical-align只有在元素为td或者th时,才会生效,对于其他块级元素,例如div、p等,默认情况是不支持的。为了使用vertical-align,我们需要设置父元素display:table-cell;vertical-align:middle。demo
vertical-align
td
th
div
p
display:table-cell;vertical-align:middle
.parent { height: 100px; display: table-cell; vertical-align: middle; } .son { width: 100px; height: 10px; background-color: red; }
使用flex布局,align-items: center;: demo
align-items: center;
.parent { height: 100px; display: flex; align-items: center; } .son { width: 100px; height: 10px; background-color: red; }
使用CSS3盒模型 demo
.parent { height: 100px; display: -webkit-box; -webkit-box-orient: vertical; -webkit-box-pack: center; display: -moz-box; -moz-box-orient: vertical; -moz-box-pack: center; display: box; box-orient: vertical; box-pack: center; }
通过使用决定对位,以及负值的margin-top来实现:demo
margin-top
.parent { position: relative; height: 100px; } .son { position: absolute; top: 50%; margin-top: -0.5 * 高度的一半; width: 100px; height: 10px; background-color: red; }
.parent { position: relative; height: 100px; } .son { position: absolute; top: 50%; transform: translate(0, -50%); width: 100px; height: 10px; background-color: red; }
设置父元素相对定位position:relative,子元素如下css样式:demo
position:relative
.parent { position: relative; height: 100px; } .son { position: absolute; top: 0; bottom: 0; margin: auto 0; width: 100px; height: 10px; background-color: red; }
flex: demo
.parent { height: 100px; display: flex; justify-content: center; align-items: center; }
绝对定位 + margin: demo
.parent { position: relative; height: 100px; } .son { position: absolute; top: 50%; left: 50%; margin-left: -0.5 * 宽度; margin-top: -0.5 * 高度; width: 100px; height: 10px; background-color: red; }
绝对定位 + transform : demo
.parent { position: relative; height: 100px; } .son { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); width: 100px; height: 10px; background-color: red; }
绝对定位 + margin: auto : demo
margin: auto
.parent { position: relative; height: 100px; } .son { position: absolute; top: 0; right: 0; bottom: 0; left: 0; margin: auto; width: 100px; height: 10px; background-color: red; }
table + margin:auto : demo
table
margin:auto
.ancestor { display: table; width: 100%; border: 1px solid blue; } .parent { height: 100px; display: table-cell; vertical-align: middle; border: 1px solid red; } .son { margin: auto; width: 100px; height: 10px; background-color: red; }
display:inline-block;vertical-align:middle; : demo
display:inline-block;vertical-align:middle;
.parent { height: 100px; text-align: center; border: 1px solid red; } .parent::after { content:''; height:100%; display:inline-block; vertical-align:middle; } .son { display:inline-block; vertical-align:middle; width: 100px; height: 10px; background-color: red; }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
水平居中
若是行内元素,给改元素的父级设置
text-align:center
,即可实现行内元素水平居中。demo若是块级元素, 该元素设置
margin:0 auto
。demo若子元素包含
float:left
属性, 为了让子元素水平居中, 则可让父元素宽度设置为fit-content
,并且配合margin
, 作如下设置:demo目前这个属性只支持Chrome和Firefox浏览器。
使用flex布局,可以很轻松的实现水平居中:demo
.son 将会被水平居中
**使用CSS3盒模型,父元素
display:box;box-pack:center;
没有深入了解。
基本介绍:
display:box;
是09年出来的。display: flex;
是12年出来的。地址:“Old” Flexbox and “New” Flexbox。看文章说在手机页面的时候,老机子
display:flex
会有问题的时候就需要box上场啦。通过使用决定对位,以及负值的
margin-left
来实现:demo使用CSS3中新增的
transform
属性,以及绝对定位:demo使用绝对定位,以及
left:0;right:0;margin:0 auto;
:demo垂直居中
元素是单行文本,设置line-height等于父元素的高度。demo
元素是行内块级元素, 基本思想是使用display: inline-block, vertical-align: middle和一个伪元素让内容块处于容器中央。demo
通过父元素创建一个看不见的伪类(宽度为0),高度为100%,同时设置为
inline-block
和vertical-align:middle
使元素垂直居中。这是一种很流行的方法, 也适应IE7。
可用
vertical-align
属性,而vertical-align
只有在元素为td
或者th
时,才会生效,对于其他块级元素,例如div
、p
等,默认情况是不支持的。为了使用vertical-align
,我们需要设置父元素display:table-cell;vertical-align:middle
。demo使用flex布局,
align-items: center;
: demo使用CSS3盒模型 demo
通过使用决定对位,以及负值的
margin-top
来实现:demo使用CSS3中新增的
transform
属性,以及绝对定位:demo设置父元素相对定位
position:relative
,子元素如下css样式:demo水平垂直居中
flex: demo
绝对定位 +
margin
: demo绝对定位 +
transform
: demo绝对定位 +
margin: auto
: demotable
+margin:auto
: demodisplay:inline-block;vertical-align:middle;
: demo分析
margin
设置可以完美的兼容ie6,缺点是子元素宽高固定。transform
兼容ie11+,但是自由度上是最佳的。参考
The text was updated successfully, but these errors were encountered: