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>
相关推荐
会豪2 小时前
深入理解 CSS3 滤镜(filter):从基础到实战进阶
前端·css·css3
小飞大王6662 小时前
css进阶用法
前端·css
用户新2 小时前
V8引擎 精品漫游指南--Ignition篇(上) 指令 栈帧 槽位 调用约定 内存布局 基础内容
前端·javascript
Next_Tech_AI2 小时前
别用 JS 惯坏了鸿蒙
开发语言·前端·javascript·个人开发·ai编程·harmonyos
-凌凌漆-3 小时前
【vue】选项式api与组合式api
前端·javascript·vue.js
0思必得04 小时前
[Web自动化] Selenium处理文件上传和下载
前端·爬虫·python·selenium·自动化·web自动化
phltxy5 小时前
Vue3入门指南:从环境搭建到数据响应式,开启高效前端开发之旅
前端·javascript·vue.js
小飞大王6665 小时前
CSS基础知识
前端·css
Charlie_lll5 小时前
学习Three.js–风车星系
前端·three.js