09-盒子模型

news/2024/10/5 7:28:20

01 认识盒子模型

image image

02 盒子模型的四边

image-20240610002032670

03 盒子边框

image-20240610002158962

04 盒子内边距-padding

通常用于设置边框和内容之间的间距

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>.box {/* 设置一个红色的实体边框 */border: 1px solid red;display: inline-block;/* 设置内边距 */padding-top: 10px;padding-right: 20px;padding-bottom: 30px;padding-left: 40px;}</style>
</head>
<body><div class="box">div元素</div>
</body>
</html>

image-20240610002339328

4.1 padding的省略写法

image-20240610002415769

05 box-sizing: border-box

盒子有了宽度和高度,如果设置padding会影响盒子的大小
为了保持盒子的大小不变
第1种方案:改变盒子原来设置的宽高,但是不推荐用这种方法
第2种方法(推荐):设置box-sizing: border-box,这样盒子的大小始终都是原来设置的大小,当还需要设置外边框和内边距的时候,不会改变盒子设置的大小,它会把盒子的内容往里压

06 盒子外边距-margin

用于设置盒子和盒子之间的距离

基本使用

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>/* 临时解决方案,解决盒子之间的间距 */body {font-size: 0;}.box {display: inline-block;background-color: aqua;width: 30px;height: 30px;margin-right: 20px;}.container {display: inline-block;background-color: green;width: 30px;height: 30px;margin-left: 20px;}</style>
</head>
<body><div class="box">div元素1</div><div class="container">div元素2</div>
</body>
</html>

image-20240610003042203

07 margin和padding的对比

image-20240610003124747

类似这种情况,要控制里面的盒子虽然margin和padding都可以实现,但是个人更加推荐使用padding,因为它相对于外面这个盒子来说,是一个内容,所以更加推荐使用padding来实现

08 margin的上下传递现象

现象演示

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>.box {background-color: red;width: 300px;height: 300px;}.container {background-color: green;width: 100px;height: 100px;margin-top: 50px;}</style>
</head>
<body><div class="box"><div class="container"></div></div>
</body>
</html>

码中明确了里面的盒子margin-top为50px,但是看到的现象却是如下面

image-20240610003240607

这种现象就是传递的现象

09 解决上下传递现象

image-20240610003412160

9.1 父元素设置border(不推荐)

外层会有一层边框即使把这个边框设置为透明的

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>.box {background-color: red;width: 300px;height: 300px;border: 1px solid transparent;}.container {background-color: green;width: 100px;height: 100px;margin-top: 50px;}</style>
</head>
<body><div class="box"><div class="container"></div></div>
</body>
</html>

9.2 触发BFC

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>.box {background-color: red;width: 300px;height: 300px;overflow: auto;}.container {background-color: green;width: 100px;height: 100px;margin-top: 50px;}</style>
</head>
<body><div class="box"><div class="container"></div></div>
</body>
</html>

image-20240610003626226

9.3 使用padding(推荐使用)

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>.box {background-color: red;width: 300px;height: 300px;box-sizing: border-box;padding-top: 50px;}.container {background-color: green;width: 100px;height: 100px;}</style>
</head>
<body><div class="box"><div class="container"></div></div>
</body>
</html>

10 margin上下折叠现象

image-20240610003744337

10.1 2个兄弟元素之间的折叠

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>.box {background-color: red;width: 100px;height: 100px;margin-bottom: 30px;}.container {background-color: green;width: 100px;height: 100px;margin-top: 50px;}</style>
</head>
<body><div class="box"></div><div class="container"></div>
</body>
</html>

image-20240610003828198

11 盒子阴影-box-shadow

image-20240610003952743

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hjln.cn/news/44206.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

试了下ocr

pdf能看了,拓展的驱动下,想着是否可以ORC呢,识别到文字内容更有帮助。 按网搜的顺序,开始是用pytesseract,pip安装顺利,但运行不了,提示找不到pytesseract,按网上的帮助下载win安装包,选上中文包,再试,可以运行了,就是中文基本识别不了,也不知哪里改善,只得作罢。…

fastjson1

@目录前言分析复制文件清空文件出现问题和分析问题解决分析问题再次出现问题再次分析最终结果读取文件分析poc拓宽场景极限环境poc优化修改再次优化poc的分析写入文件SafeFileOutputStream写文件java8无依赖读文件在commons-io库下的写入文件原因利用链分析组合poc出现问题和分…

解决运行loadRunner报错无法进行代理的错误

选择第二个,不设置代理,可以实现回放不会报错,但是今日运行遇到错误,无法实现全部的录制脚本回访完毕,卡住打开网址处的脚本。直接运行完毕,不会报错。

LiteDb

目录封装的代码相关参考本文记录LiteDb的使用,建议初学者使用时先根据官方的文档进行学习。LiteDb官网LiteDb DocLiteDb Package LiteDb API 封装的代码LiteDbWrapper.cs/// <summary>/// 官网:http://www.litedb.org//// GitHub:https://github.com/mbdavid?tab=repo…

winform窗体关闭之前弹出确认框

需要使用到窗体的 FormClosing 事件 private void FrmMain_FormClosing(object sender, FormClosingEventArgs e) {DialogResult dialogResult = MessageBox.Show("是否确认关闭窗口", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);if (…

Oh My Posh 安装与使用

Oh My Posh 安装与使用 目录Oh My Posh 安装与使用IntroductionInstallationThemes配置使用主题安装字体建议FAQ本文介绍 Windows Terminal. 集成 Oh My Posh . Introduction Oh My Posh 介绍。 Introduction | Oh My PoshInstallation Windows Terminal 安装 Oh My Posh. Wind…

Canny边缘检测算法

一、概念 Canny算法是一种经典的图像处理算法,用于图像中的边缘检测,其实现的步骤包括:高斯滤波; 计算梯度和方向:对平滑后的图像使用Sobel算子计算水平方向和竖直方向的一阶导数,然后计算该点的梯度大小和方向;非极大值抑制:对梯度图上的每个像素,根据其梯度方向,确…

【数据库】mybatis生成java代码之AutoGenerator配置

哈喽,大家好,我是木头左,AI改变生活!本文将详细解释 MyBatis 生成 Java 代码的过程,包括全局配置、数据源配置和策略配置。 1. 全局配置 首先,我们需要创建一个 GlobalConfig 对象,用于配置 MyBatis 的全局设置。以下是一些常用的全局配置选项:setOutputDir(String out…