1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > css中有哪些方法可以实现垂直居中

css中有哪些方法可以实现垂直居中

时间:2021-02-03 17:28:41

相关推荐

css中有哪些方法可以实现垂直居中

web前端|css教程

css,垂直居中

web前端-css教程

易语言刷屏源码,ubuntu驱动循环登录,防止爬虫重复抓取,php中没有php.ini,桦甸seo推广lzw

css实现垂直居中的方法如下:

前端个人简历网站源码,vscode怎么建立站点,ubuntu apt卡住,tomcat访问配置,sqlite如何添加数据,爬虫自动化下载文件,php 多级联动,华夏seo搜索优化,惊天动地网站源码,网页字体编辑,后台样式模板lzw

1、利用line-height实现居中,这种方法适合纯文字类的;

android voip 源码,ubuntu配置icm文件,java爬虫抓数据,php讲,南宁优化seolzw

.parents { height: 400px; line-height: 400px; width: 400px; border: 1px solid red; text-align: center;}.child { background-color: blue; color: #fff;}

css布局,实现垂直居中

效果:

(推荐教学:CSS教学)

2、通过设置父容器相对定位,子级设置绝对定位,标签通过margin实现自适应居中;

.parents { height: 400px; width: 400px; border: 1px solid red; position: relative;}.child { width: 200px; height: 100px; line-height: 100px; text-align: center; color: #fff; background-color: blue; /* 四个方向设置为0, 然后通过margin为auto自适应居中 */ position: absolute; top: 0; right: 0; bottom: 0; left: 0; margin: auto;}

css布局,实现垂直居中

效果:

3、弹性布局flex 父级设置display: flex; 子级设置margin为auto实现自适应居中;

.parents {height: 400px;width: 400px;border: 1px solid red;display: flex; } .child {width: 200px;height: 100px;line-height: 100px;text-align: center;color: #333;background-color: yellow;margin: auto; }

css布局,实现垂直居中

效果:

4、父级设置相对定位,子级设置绝对定位,并且通过位移transform实现;

.parents {height: 400px;width: 400px;border: 1px solid red;position: relative; } .child {width: 200px;height: 100px;line-height: 100px;text-align: center;color: #fff;background-color: green;position: absolute;top: 50%;left: 50%;transform: translate(-50%, -50%); }

css布局,实现垂直居中

效果:

5、父级设置弹性盒子,并设置弹性盒子相关属性;

.parents {height: 400px;width: 400px;border: 1px solid red;display: flex;justify-content: center; /* 水平 */align-items: center; /* 垂直 */ } .child {width: 200px;height: 100px;color: black;background-color: orange; }

效果:

6、网格布局,父级通过转换成表格形式,然后子级设置行内或行内块实现。(需要注意的是:vertical-align: middle使用的前提条件是内联元素以及display值为table-cell的元素)。

效果:

.parents {height: 400px;width: 400px;border: 1px solid red;display: table-cell;text-align: center;vertical-align: middle; } .child {width: 200px;height: 100px;color: #fff;background-color: blue;display: inline-block; /* 子元素设置行内或行内块 */ }

相关视频教学推荐:css视频教学

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。