最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

CSS实现水平垂直同时居中的5种思路-小火柴的蓝色理想

来源:动视网 责编:小采 时间:2020-11-27 16:44:02
文档

CSS实现水平垂直同时居中的5种思路-小火柴的蓝色理想

CSS实现水平垂直同时居中的5种思路-小火柴的蓝色理想:× 目录 [1]水平对齐+行高 [2]水平+垂直对齐 [3]margin+垂直对齐[4]absolute[5]flex 前面的话 水平居中和垂直居中已经单独介绍过,本文将介绍水平垂直同时居中的5种思路 思路一: text-align + line-height实现单行文本水平垂直居
推荐度:
导读CSS实现水平垂直同时居中的5种思路-小火柴的蓝色理想:× 目录 [1]水平对齐+行高 [2]水平+垂直对齐 [3]margin+垂直对齐[4]absolute[5]flex 前面的话 水平居中和垂直居中已经单独介绍过,本文将介绍水平垂直同时居中的5种思路 思路一: text-align + line-height实现单行文本水平垂直居
 ×
目录
[1]水平对齐+行高 [2]水平+垂直对齐 [3]margin+垂直对齐[4]absolute[5]flex

前面的话

  水平居中和垂直居中已经单独介绍过,本文将介绍水平垂直同时居中的5种思路

思路一: text-align + line-height实现单行文本水平垂直居中

测试文字

思路二: text-align + vertical-align

【1】在父元素设置text-align和vertical-align,并将父元素设置为table-cell元素,子元素设置为inline-block元素

  [注意]若兼容IE7-浏览器,将结构改为

结构来实现table-cell的效果;用display:inline;zoom:1;来实现inline-block的效果

测试文字

【2】若子元素是图像,可不使用table-cell,而是其父元素用行高替代高度,且字体大小设为0。子元素本身设置vertical-align:middle

思路三: margin + vertical-align

  要想在父元素中设置vertical-align,须设置为table-cell元素;要想让margin:0 auto实现水平居中的块元素内容撑开宽度,须设置为table元素。而table元素是可以嵌套在tabel-cell元素里面的,就像一个单元格里可以嵌套一个表格

  [注意]若兼容IE7-浏览器,需将结构改为

结构

测试文字

思路四: 使用absolute

【1】利用绝对定位元素的盒模型特性,在偏移属性为确定值的基础上,设置margin:auto

测试文字

【2】利用绝对定位元素的偏移属性和translate()函数的自身偏移达到水平垂直居中的效果

  [注意]IE9-浏览器不支持

测试文字

【3】在子元素宽高已知的情况下,可以配合margin负值达到水平垂直居中效果

测试文字

思路五: 使用flex

  [注意]IE9-浏览器不支持

【1】在伸缩项目上使用margin:auto

测试文字

【2】在伸缩容器上使用主轴对齐justify-content和侧轴对齐align-items

测试文字
// 0){ return; } if(select[i].getBoundingClientRect().top <= 0 && select[i+1]){ if(select[i+1].getBoundingClientRect().top > 0){ change(oCon.children[i+2]) } }else{ change(oCon.children[select.length+1]) } } } document.body.onmousewheel = wheel; document.body.addEventListener('DOMMouseScroll',wheel,false); var oCon = document.getElementById("content"); var close = oCon.getElementsByTagName('span')[0]; close.onclick = function(){ if(this.innerHTML == '显示目录'){ this.innerHTML = '×'; this.style.background = ''; oCon.style.border = '2px solid #ccc'; oCon.style.width = ''; oCon.style.height = ''; oCon.style.overflow = ''; oCon.style.lineHeight = '30px'; }else{ this.innerHTML = '显示目录'; this.style.background = '#3399ff'; oCon.style.border = 'none'; oCon.style.width = '60px'; oCon.style.height = '30px'; oCon.style.overflow = 'hidden'; oCon.style.lineHeight = ''; } } for(var i = 2; i < oCon.children.length; i++){ oCon.children[i].onmouseover = function(){ this.style.color = '#3399ff'; } oCon.children[i].onmouseout = function(){ this.style.color = 'inherit'; if(this.mark){ this.style.color = '#3399ff'; } } oCon.children[i].onclick = function(){ change(this); } } function change(_this){ for(var i = 2; i < oCon.children.length; i++){ oCon.children[i].mark = false; oCon.children[i].style.color = 'inherit'; oCon.children[i].style.textDecoration = 'none'; oCon.children[i].style.borderColor = 'transparent'; } _this.mark = true; _this.style.color = '#3399ff'; _this.style.textDecoration = 'underline'; _this.style.borderColor = '#2175bc'; } // ]]>

文档

CSS实现水平垂直同时居中的5种思路-小火柴的蓝色理想

CSS实现水平垂直同时居中的5种思路-小火柴的蓝色理想:× 目录 [1]水平对齐+行高 [2]水平+垂直对齐 [3]margin+垂直对齐[4]absolute[5]flex 前面的话 水平居中和垂直居中已经单独介绍过,本文将介绍水平垂直同时居中的5种思路 思路一: text-align + line-height实现单行文本水平垂直居
推荐度:
标签: 实现的 垂直 思路
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top