最全css与bootstrap的水平垂直居中
2020-06-09 05:02
826 查看
一. 水平居中
1.块元素在已知宽度的情况下为其自身设置margin:auto(可以用block转为块元素)
.Wrap{ border: 1px black solid; width: 100px;height: 100px; margin: auto; }
2.内联元素可以为其父元素设置text-align:center;(可以用table-cell,inline,inline-block将块元素转为内联元素)
.Wrap{ border: 1px black solid; text-align: center; }
- margin和width
前提:已知 width值 | margin-left: auto;margin-right: auto; |
---|
.wrap{ width: 100px;height: 100px; margin-left: auto; margin-right: auto; border: 1px aqua solid; }
- 绝对定位和left:0;right:0;
设置了绝对定位后,margin:auto就失效了,此时只需加上left: 0;right: 0;即可居中 |
---|
.wrap{ width: 100px; height: 100px; border: 1px red solid; position: absolute; left: 0;right: 0; margin: auto; }
- 绝对定位和margin-left:-(宽度值/2)
适合固定宽度元素,当浏览器宽度小于元素宽度时与margin:auto;有明显不同 |
---|
.wrap{ width: 100px; height: 100px; border: 1px red solid; position: absolute; left:50%; margin-left: -50px; }
- 绝对定位和transform
宽度未定的情况下水平居中,IE9以下不能使用 |
---|
.wrap{ border: 1px red solid; position: absolute; left:50%; transform: translateX(-50%); }
- flex布局
/* html*/ <div class="wrap"> <div class="box"></div> </div> /* css*/ .wrap{ width: 300px; height: 300px; border: 1px red solid; display: flex; justify-content: center; } .box{ width: 100px;height: 100px; background-color: blue; }
- 网格布局
/* html*/ <div class="wrap"> <div class="box"></div> </div> /* css*/ .wrap7{ width: 300px; height: 300px; border: 1px red solid; display: grid; justify-content: center; } .box1{ width: 100px;height: 100px; background-color: blue; }
二.垂直居中
- 伪类after
vertical-align使同一个父元素的元素互相垂直居中,而不是根据父元素垂直居中,所以当其中一个元素的高度跟父元素相同时,其余元素便会相对父元素垂直居中,但是vertical-align只对行内元素有效,所以子元素最好设置display: inline-block;属性 |
---|
/* html*/ <div class="box"> <div class="Wrap2"> <p>伪类实现居中</p> </div> </div> /* css*/ .box{ text-align: center; overflow: auto; width: 300px; height: 300px; border: 1px red solid; margin: auto; } .Wrap2{ display: inline-block; vertical-align: middle; } .box::after{ content: ""; display: inline-block; vertical-align: middle; height: 100%; }
2.单行元素设置line-height与height相等
/*html*/ <div class="wrap"> 垂直居中 </div> /*css*/ .wrap{ width: 100px;height: 100px; line-height: 100px; border: 1px red solid; }
3.绝对定位与bottom:0;top:0;
.wrap{ width: 100px;height: 100px; background-color: red; position: absolute; bottom:0;top:0; margin:auto; }
4.绝对定位与margin-top
.wrap{ width: 100px;height: 100px; position: absolute; top:50%; margin-top: -50px; border: 1px red solid; }
5.绝对定位与transform
.wrap{ width: 100px;height: 100px; position: absolute; top:50%; transform: translateY(-50%); border: 1px red solid; }
6.flex布局
利用flex的align-items属性实现子元素在父元素内的垂直居中,align-self也行 |
---|
/* html*/ <div class="wrap"> <div class="box"></div> </div> /* css*/ .wrap{ width: 300px; height: 300px; border: 1px red solid; display: flex; align-items: center; } .box{ width: 100px;height: 100px; background-color: blue; }
设置父元素为flex布局之后,子元素直接使用margin:auto;即可实现子元素在父元素内的垂直居中 |
---|
/* html*/ <div class="wrap"> <p>垂直居中</p> </div> /* css*/ .wrap{ width: 100px;height: 100px; border: 1px red solid; display: flex; } p{margin: auto;}
7.网格布局
利用grid的align-items属性实现子元素在父元素内的垂直居中,align-self也行 |
---|
/* html*/ <div class="wrap"> <div class="box"></div> </div> /* css*/ .wrap7{ width: 300px; height: 300px; border: 1px red solid; display: grid; align-items: center; } .box1{ width: 100px;height: 100px; background-color: blue; }
设置父元素为flex布局之后,子元素直接使用margin:auto;即可实现子元素在父元素内的垂直居中 |
---|
<div class="wrap"> <p>垂直居中</p> </div> /* css*/ .wrap{ width: 100px;height: 100px; border: 1px red solid; display: grid; } p{margin: auto;}
8.表格布局
父元素使用display: table; 子元素使用display: table-cell;vertical-align: middle;即可 |
---|
.wrap{ width: 100px;height: 100px; border: 1px red solid; display: table; text-align: center; } box{vertical-align: middle;display: table-cell;}
三.bootstrap的居中
bootstrap框架将以上水平垂直进行了封装 ,主要使用网格布局和弹性布局,能够自适应于台式机、平板电脑和手机。对应如下
bootstrap | css |
---|---|
text-center | text-align:center |
m-auto | margin:auto |
justify-content: center; | justify-content-center; |
align-items: center; | align-items-center; |
col-offset | offset |
相关文章推荐
- CSS水平垂直居中的几种方法
- CSS里面水平垂直居中的问题
- 用css水平垂直居中
- Bootstrap模态框水平垂直居中与增加拖拽功能
- 七种CSS方式让一个容器水平垂直居中
- bootstrap中dialog水平垂直居中
- 收罗CSS布局居中的N中方法(水平和垂直)
- CSS的水平居中与垂直居中
- css实现垂直水平居中
- css实现div垂直水平居中的2种常用方法
- H5新增标签、样式,手写tab切换 ,css水平垂直居中常见方法
- CSS实现水平和垂直居中
- 纯CSS实现未知高宽的图片在容器中水平和垂直居中
- css 水平垂直居中
- 七种CSS方式让一个容器水平垂直居中
- CSS制作水平垂直居中对齐
- css布局不定宽高的水平垂直居中
- 采用CSS让DIV在网页中水平垂直居中
- 探究css中各种情况下的元素的垂直和水平居中的问题
- CSS实现水平垂直居中方式