padding和margin有何不同

padding is the space between the content and the border, whereas marigin is the space outside the border.

padding是内容和边框之间的区域, 而marigin是边框之外的区域

在这里插入图片描述

让我们验证一下, 快速新建一个文件, 内容结构如下

import React from 'react';
import './App.css';

export default function App() {
  return (
    <div className='outer'>
      <div className='inner'>
        content
      </div>
    </div>
  )
}

对应的css文件为

.outer {
  border: 5px solid;
}

.inner {
  border: 5px solid rgb(158, 14, 14);
  background-color: yellowgreen;
  padding: 50px 0;
  margin: 0 50px;
}

可以看到此时网页为

在这里插入图片描述

content距离上下border的距离均为50px, 但是在inner-border内, innerbackground-color都可以发挥作用

content左右,距离outer的距离均为50px, 且没有背景颜色, 证明marginborder距离outer的距离

从控制台可以很清晰的看到这些

在这里插入图片描述

但是, margin还有一个特点, 那就是margin-topmargin-bottomcollapsible(可折叠的), 举个例子, 有如下两个组件

<div className="topDiv"></div>
<div className="bottomDiv"></div>

其css样式如下:

.topDiv {
  border: 5px solid;
  min-height: 15px;
  margin-bottom: 20px;
}

.bottomDiv {
  border: 5px solid;
  min-height: 15px;
  margin-top: 50px
}

topiv中, 有一个margin-bottom设置为20px, 在bottoDiv中, 有一个margin-top设置为50px, 正常来说, 两个div的上下距离应该为20+50=70px, 但实际上, 两者之间的距离为50px

不过margin-leftmargin-right并没有这个特点!

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值