css入门10:布局(水平,垂直;居中,对齐;)

一、水平

1、利用{margin:auto;width:50%;}来进行水平居中;

2、利用{text-align:center;}进行文字的水平居中;


二、垂直

1、利用padding-top内边距设置实现垂直居中

2、利用{height:300px;inline-height:300px;} 高度=行高来进行垂直居中。

3、利用position和transform进行垂直居中。(这种方法也必须先知道height是多少)

<html>

<head>
  <style>

  .center{
    margin: auto;
    width: 60%;
    text-align: center;
    background-color: lightgrey;
    height: 200px;
    line-height: 200px;
  }
  </style>
</head>

<body>


<div class="center">

  <p>水平居中了吗,垂直居中了吗?</p>

</div>

</body>

</html>

使用inline-height=height进行垂直居中。

<html>

<head>
  <style>
    .center {
      height: 200px;
      position: relative;
      border: 3px solid green;
    }

    .center1 {
      margin: 0;
      position: absolute;
      top: 50%;
      left: 50%;
      /* -ms-transform: translate(-50%, 50%); */
      transform: translate(-50%, -50%);
    }
  </style>
</head>

<body>

  <div class="center">
    <p class="center1">水平居中了吗,垂直居中了吗?</p>
  </div>

</body>

</html>

使用position+transform的办法进行垂直居中。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值