CSS中的布局

一、如何实现品字布局
  1. 每块宽度相等但没有确定值,且不超过50%
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>品字布局</title>
  <style>
    /* div2,div3 设置为 inline-block,可用 text-align: center 居中 */
    body { text-align: center; }
    .div { width: 40%; height: 300px; line-height: 300px; background-color: aqua; border: solid 1px #666; }
    /* 上面的div居中 */
    .div1 { margin: 0 auto; }
    /* 下面的两个div设置为 display: inline-block,不换行 */
    .div2 { display: inline-block; }
    .div3 { display: inline-block; }
  </style>
</head>
<body>
  <div class="div div1">1</div>
  <div class="div div2">2</div>
  <div class="div div3">3</div>
</body>
</html>
  1. 每块都有固定宽高
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>品字布局</title>
  <style>
    .div { width: 100px; height: 100px; line-height: 100px; background-color: aqua; border: solid 1px #666; text-align: center; }
    .div1 { margin: auto; }
    /* 根据固定的宽度进行定位 */
    .div2 { float: left; margin-left: 50%; position: relative; left: -100px; }
    .div3 { float: left; position: relative; left: -100px;}
  </style>
</head>
<body>
  <div class="div div1">1</div>
  <div class="div div2">2</div>
  <div class="div div3">3</div>
</body>
</html>
  1. 满屏宽度
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>品字布局</title>
  <style>
    .div { height: 300px; line-height: 300px; text-align: center; }
    .div1 { width: 100%; margin: auto; background-color: red; }
    .div2 { width: 50%; float: left; background-color: yellow; }
    .div3 { width: 50%; float: right; background-color: blue; }
  </style>
</head>
<body>
  <div class="div div1">1</div>
  <div class="div div2">2</div>
  <div class="div div3">3</div>
</body>
</html>
二、如何实现垂直水平居中
  1. margin: auto
	.outer {
      position: relative;
      width: 100%;
      height: 500px;
      background: red;
    }
    .inner {
      background: blue;
      width: 50%;
      height: 50%;
      position: absolute;
      left: 0;
      right: 0;
      top: 0;
      bottom: 0;
      margin: auto;
    }
  1. transform
	.outer {
      position: relative;
      width: 100%;
      height: 500px;
      background: red;
    }
    .inner {
      background: blue;
      width: 50%;
      height: 50%;
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(-50%, -50%)
    }
  1. flex
	.outer {
      width: 100%;
      height: 500px;
      background: red;
      display: flex;
      justify-content: center;
      align-items: center;
    }
    .inner {
      background: blue;
      width: 50%;
      height: 50%;
    }
  1. table
	/* 需要确定宽高 */
	.outer {
      width: 700px;
      height: 500px;
      background: red;
      display: table-cell;
      text-align: center;
      vertical-align: middle;
    }
    .inner {
      display: inline-block;
      background: blue;
      width: 400px;
      height: 300px;
    }
  1. grid
	/* 需要确定内部容器宽度 */
    .outer {
      background: red;
      width: 700px;
      height: 500px;
      display: grid;
      justify-content: center;
    }
    .inner {
      background: blue;
      width: 300px;
      height: 50%;
      align-self: center;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值