CSS3进阶知识:CSS3盒子模型,box-sizing:content-box和box-sizing:border-box的讲解

css3可以通过box-sizing来指定盒模型,它有两个值content-box、border-box

这样就会出现两种情况:

1.box-sizing:content-box 盒子大小为width+padding+border(以前默认的)

2.box-sizing:border-box盒子大小为width

举例说明:

html 复制代码
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .test1 {
        width: 100px;
        height: 100px;
        padding: 50px;
        margin: 50px;
        box-sizing: content-box;
        background-color: red;
        /* 上面是默认值 */
      }
      .test2 {
        width: 100px;
        height: 100px;
        padding: 50px;
        margin: 50px;
        box-sizing: border-box;
        background-color: green;
      }
    </style>
  </head>
  <body>
    <div class="test1"></div>
    <div class="test2"></div>
  </body>
</html>
相关推荐
匠心网络科技10 分钟前
JavaScript进阶-ES6 带来的高效编程新体验
开发语言·前端·javascript·学习·面试
Never_Satisfied24 分钟前
在HTML & CSS中,nth-child、nth-of-type详解
前端·css·html
睡不着的可乐1 小时前
createElement → VNode 是怎么创建的
前端·javascript·vue.js
光影少年1 小时前
前端css如何实现水平垂直居中?
前端·javascript·css
C澒1 小时前
SLDS 自营物流系统:Pickup 揽收全流程
前端·架构·系统架构·教育电商·交通物流
摸鱼的春哥1 小时前
把白领吓破防的2028预言,究竟讲了什么?
前端·javascript·后端
infiniteWei1 小时前
SKILL.md 触发机制与设计规范:避免“写了不触发”
java·前端·设计规范
慧一居士1 小时前
SVG图片介绍和使用
前端
Rysxt_1 小时前
Uniapp全局配置教程
前端·uniapp
阿珊和她的猫1 小时前
深入理解与使用 Cookie:Web 开发中的关键机制
前端·状态模式