CSS-div居中

前言:

css怎么将div居中显示,这个已经是老生长谈了,而且实现的方法也很多,这里就给大家列举其中一些方法。

div水平垂直居中:

flex 布局实现 (元素已知宽度):
<div class="box">        
    <div class="item">flex</div>    
</div>
.box{            
    width: 300px;            
    height: 300px;           
    background-color: #ccc;            
    display: flex;                      
    justify-content: center;            
    align-items: center;        
}        
.box .item{            
    width: 100px;            
    height: 100px;            
    background-color: blue;        
}    

在这里插入图片描述

position (元素已知宽度):
<div class="box">        
    <div class="item">position</div>    
</div>
.box{            
	width: 300px;            
 	height: 300px;            
 	background-color: #ccc;            
 	position: relative;        
}        
 .box .item{            
    width: 100px;            
    height: 100px;            
    background-color: blue;            
    position: absolute;            
    left: 50%;            
    top: 50%;            
    margin: -50px 0 0 -50px;        
}    

在这里插入图片描述

position transform (元素未知宽度):
<div class="box">        
    <div class="item">transform</div>    
</div>
.box{            
    width: 300px;            
    height: 300px;            
    background-color: #ccc;            
    position: relative;        
}        
.box .item{            
    background-color: blue;            
    position: absolute;            
    top: 50%;            
    left: 50%;            
    transform: translate(-50%, -50%);        
}

在这里插入图片描述

position(元素已知宽度)(left,right,top,bottom为0,margin:auto ):
<div class="box">
    <div class="item">position</div>
</div>
.box{            
  	width: 300px;            
    height: 300px;           
    background-color: #ccc;            
    position: relative;        
}        
.box .item{            
    width: 100px;            
    height: 100px;            
    background-color: blue;            
    position: absolute;            
    top: 0;            
    bottom: 0;            
    left: 0;            
    right: 0;            
    margin: auto;        
}    

在这里插入图片描述

table-cell 布局实现:
<div class="box">
    <div class="item">table-cell</div>
</div>
.box{            
  	width: 300px;            
    height: 300px;            
    background-color: #ccc;            
    display: table-cell;            
    vertical-align: middle;     
}        
.box .item{            
    margin-left: 100px;            
    width: 100px;            
    height: 100px;            
    background-color: blue;   
}    

在这里插入图片描述

内容(文字,图片)水平垂直居中:

  • text-align : center 给元素的父级加,可以使文本或者行级元素(例如:图片)水平居中。
  • line-height : 值为元素的高度,可以使元素的文本内容垂直居中。
  • margin: 0 auto 使用条件:父级元素宽度可有可无,子级元素必须使块元素,而且要有宽度(否则继承父级)。
  • display:table-cell 会使元素表现的类似一个表格中的单元格td,利用这个特性可以实现文字的垂直居中效果。

display:table-cell 使用注意:

  • 不要与 float:left, position : absolute, 一起使用
  • 可以实现大小不固定元素的垂直居中
  • margin 设置无效,响应 padding 设置
  • 对高度和宽度高度敏感
  • 不要使用百分比设置宽度和高度

文字水平垂直居中:
<div class="box">table-cell</div>
.box{            
   width: 300px;            
   height: 300px;            
   background-color: blue;            
   display: table-cell;            
   text-align: center;/*使元素水平居中 */            
   vertical-align: middle;/*使元素垂直居中 */        
}    

在这里插入图片描述

图片水平垂直居中:
<div class="box">    
    <img src="test.jpg" alt="">
</div>
.box{            
  width: 300px;            
    height: 300px;            
    background-color: skyblue;            
    display: table-cell;            
    text-align: center;            
    vertical-align: middle;        
}        
img{            
    /* 设置成块元素后,text-align:center 就会失效 */            
    width: 100px;            
    height: 100px;        
}    

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值