最新文章专题视频专题问答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实现垂直水平居中的六种方法_html/css_WEB-ITnose

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

Css实现垂直水平居中的六种方法_html/css_WEB-ITnose

Css实现垂直水平居中的六种方法_html/css_WEB-ITnose:经常在项目中用到,今天总结了一下: 演示地址:http://codepen.io/anon/pen/xGdpOa 以下两个类为公共类,便于更好的显示效果,非核心代码 .common{ width: 600px;height: 180px; background-color:#56abe4; co
推荐度:
导读Css实现垂直水平居中的六种方法_html/css_WEB-ITnose:经常在项目中用到,今天总结了一下: 演示地址:http://codepen.io/anon/pen/xGdpOa 以下两个类为公共类,便于更好的显示效果,非核心代码 .common{ width: 600px;height: 180px; background-color:#56abe4; co


经常在项目中用到,今天总结了一下:

演示地址:http://codepen.io/anon/pen/xGdpOa

以下两个类为公共类,便于更好的显示效果,非核心代码

	.common{	width: 600px;height: 180px;	background-color:#56abe4;	color: #fff;	margin: 15px auto; 	border-radius: 3px;	 }	.con{	display: inline-block;	padding: 15px;	width: 160px;	height: 80px;	background-color: orange;	}

  

正文部分:

第一种方法:

/*position: absolute;top:0;right: 0;bottom: 0;left: 0;margin: auto;*/

HTML结构:

	position: absolute;	top:0;right: 0;bottom: 0;left: 0;	margin: auto;	

CSS部分:

	.block1{	position: relative;	}	.inner1{	position: absolute;	top:0;right: 0;bottom: 0;left: 0;	margin: auto;	}

 

第二种方法: 

/*display: table-cell*/

HTML结构:

 	display: table-cell;	text-align: center;	vertical-align: middle;	

CSS部分: 

.block2{	display: table-cell;	text-align: center;	vertical-align: middle;	}

  

第三种方法:

/*display: flex;*/

HTML结构:

	 display: flex; align-items: center; justify-content: center;	

CSS部分:

	.block3{	display: flex;	align-items: center;	justify-content: center;	}

  

 第四种方法:

/*负定位*/

HTML结构:

	 position: absolute;	top: 50%;	left: 50%; 并利用负定位消除元素的上下,左右偏移	

CSS部分:

	.block4{ position: relative;	}	.inner4{	position: absolute;	top: 50%;	left: 50%;	margin-top: -55px;/*80/2+15=55*/ margin-left: -95px;/*160/2+15=95*/	}

  

 第五种方法:

/*transform*/

HTML结构:

	position: absolute;	top: 50%;	left: 50%; transform:translate(-50%,-50%);	

CSS部分:

	.block5{ position: relative;	}	.inner5{	position: absolute;	top: 50%;	left: 50%; transform:translate(-50%,-50%); word-wrap: break-word;	}

  

第六种方法:(兼容性较好)

/*行内块*/

HTML结构:

	行内块:
谨记span标签与该div之间是没有空白的,否则会产生误差

CSS部分:

	.block6{ text-align:center;}	.inner6,.block6 span{	 display:inline-block;	 *display:inline;	 zoom:1; 	 vertical-align:middle;	}	.inner6{max-width:100%;max-height:100%;}	.block6 span{width:0;height:100%;}

以上几种方案存在兼容性问题,在使用时请先查询核心css的浏览器兼容情况,查询地址:http://caniuse.com/

以上。

欢迎补充~

文档

Css实现垂直水平居中的六种方法_html/css_WEB-ITnose

Css实现垂直水平居中的六种方法_html/css_WEB-ITnose:经常在项目中用到,今天总结了一下: 演示地址:http://codepen.io/anon/pen/xGdpOa 以下两个类为公共类,便于更好的显示效果,非核心代码 .common{ width: 600px;height: 180px; background-color:#56abe4; co
推荐度:
标签: 种方法 方式 cs
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top