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

css实现水平居中/垂直居中的一些方法

时间:2019-06-02 02:06:42

相关推荐

css实现水平居中/垂直居中的一些方法

web前端|css教程

css,水平居中,垂直居中

web前端-css教程

本文整理一些利用css实现水平居中/垂直居中的一些方法,教学都很基础,希望对大家有帮助!

比特币开源码,vscode 文件比较,ubuntu优秀的软件,多个tomcat冲突,qt读取sqlite,wordpress用户管理 插件,免费的手机前端框架,爬虫深度node,单元测试 php,中文域名seo,传奇3网站模板下载不了,h5网页常见代码,html学校后台管理系统模板,下载站页面代码,科研管理系统代码,网络程序设计模板之家lzw

A、水平居中

ppp 源码分析,ubuntu如何复制主题,tomcat登录身份验证,淄博爬虫店,最新版php,seo课程培训seo博客lzw

1. text-align:center(行内元素)

android 订餐 源码,vscode禁止运行,ubuntu删除软件包信息,tomcat依赖项目,sqlite计算列数,wordpress访客统计插件,前端项目的框架选择,爬虫翻页翻不了,php json 转换,seo新手如何入门seo黑帽,网站前端源代码免费下载,网页办公系统,p2p网贷模板,拼多多蘑菇街京东小程序源码lzw

给其父元素设置属性 text-align:center;

2. margin: 0 auto(块级元素)

给元素本身设置 margin: 0 auto;

3. 元素的宽度固定

1⃣️ position+margin-left

.ele{ position:absolute; width:固定; left:50%; margin-left:-0.5宽度;}

2⃣️ position+left:0;right:0;margin:0 auto

.ele{ position:absolute; width:固定; left:0; right:0; margin:0 auto;}

4.元素的宽度不固定

1⃣️css3-transform

.ele{ position:absolute;left:50%;transform:translate(-50%,0);}

2⃣️css3-width:fit-content(当需要居中的元素设置了float:left时,可以给该元素的父元素设置如下属性)

.ele-parent{width: -moz-fit-content; width: -webkit-fit-content; width:fit-content; margin:0 auto;}

该属性目前只支持Chrome 和 Firefox浏览器.

3⃣️flex

.ele-parent{ display: flex; justify-content: center;}

B、垂直居中

1. line-height:eleparent-height

单行文本的垂直居中可以设置属性 line-height:父元素height

2. 元素的高度固定

1⃣️position+margin-top

.ele-parent{ position:relative;}.ele{ position:absolute; top:50%; height:固定; margin-top:-0.5高度;}

2⃣️ position+top:0;bottom:0;margin:auto 0

.ele{ position:absolute; height:固定; top:0; bottom:0; margin:auto 0;}

3.元素高度不固定

1⃣️vertical-align

.ele-parent{ display:table;}.ele{ display:table-cell;

vertical-align:middle;}

2⃣️css3-transform

.ele{ position:absolute; top:50%; transform: translate(0,-50%);}

3⃣️flex

.ele-parent{ display: flex; align-items:center;}

暂时只整理到这些,不足之处还请 批评指正!!!

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