html页面中块级元素的居中

第一:在页面中使用 margin: 0 auto;居中;

<div>

  <p>夜屋</p>

</div>

div {
  width: 100%;
  position: relative;
  background-color: red;
}
p {
  width: 800px;
  margin: 0 auto;
  background-color: #ccc;
}

第二:在页面中使用 left 及margin-left;

<style type="text/css">
*{
  padding: 0px;
  margin: 0px;
}
.wrap{
  width:400px;
  height:300px;
  margin:10px;
  border:1px solid #000;
}
.centerDiv{
  display: block;
  width:200px;
  height:200px;
  background-color:#69F;
  top:50%;
  left:50%;
  margin:-100px 0 0 -100px;
}
</style>

<div class="wrap">
  <div class="centerDiv">上下左右居中方法2</div>
</div>

 

 

 注意:需要垂直并且水平居中的元素必须是div,其他p类等块级元素用此方法不会居中(垂直和水平居中),若是用了非div元素,显示如下;

<div class="wrap"> 
  <p class="centerDiv">上下左右居中方法2</p> 
</div>

 第三、图片的垂直居中,使用display: table-cell;

div {
  width: 289px;
  height: 1000px;
  display: table-cell;
  vertical-align: middle;
  background-color: Gray;
}

<div>
  <img src="../images/canvas.jpg" width="333" height="500">
</div>

 

 

转载于:https://www.cnblogs.com/txtyewu/p/5945880.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值