CSS样式——盒子模型

盒子模型

一、什么是盒模型

每一个标签都是一个矩形,像一个盒子,所以HTML页面的布局可以理解为多个盒子组合嵌套排列而成

二、组成

1. width 宽度

2. height 高度

3.padding 内边距

(1).1-4个值
(2).按顺时针赋值
(3).从上开始

padding-top
padding-right
padding-bottom
padding-left

p{
    padding:50px,60,px,40px,30px;
    <!-- 内边距的值顺时针依此变成了50px,60px,40px,30px -->
}
/* 可以单独设置某一个,也可以按顺时针的顺序依次设 */
/* 如果不满四个则逆向 */
div{
	padding:50px,60px,30px;
	<!-- 内边距的值顺时针依此变成了50px,60px,30px,40px-->
}

4.borfer

复合属性

border-width
border-style:solid(实线);dashed(虚线);dotted(点线);double(双线)。
border-color

div{
	border:10px soild skyblue;
	/*上下左右可以单独设置,宽度,样式,颜色也可以单独设置*/
	border-reght:5px dashed red
	border-top-color: yellow
}
<!-- border和padding会增加盒模型面积-->
/*盒模型面积:*/
s=(bl+pl+width+pr+br)*(bt+pt+height+pb+bb)

圆角

div{
	/*圆角*/
	border-radius:20px
	/*若参数是50%以上,且宽高比为1:1则为圆形否则为椭圆。若参数是50%以下则是圆角矩形*/
	/*会根据不同的参数来改变图形的形状*/
	/*同理也可以设置单一角*/
	border-top-right-radius:50px;
	
}

三角形

<!-- 三角形-->
div{
	border:25px soild pink;
	border-right-color:red;
	border-top-color:blue;
	border-bottom-color;yellow;
	/*会发现颜色分割时是斜着的,这是我们将宽度高度设置为0就可以得到四个三角形*/
	weight:0
	height:0
	/*此时只需我们将其他三角形的颜色边成透明色即可得到想要的三角形*/
	也可以通过刚刚的步骤使三角形变成扇形,大家自己试一下。
}

5.margin 外边距

(1).1-4个值
(2).按顺时针赋值,从上开始

外边距指的是盒子与盒子之间的距离

padding-top
padding-right
padding-bottom
padding-left

p{
    margin:50px,60,px,40px,30px;
    <!-- 外边距的值顺时针依此变成了50px,60px,40px,30px -->
}
/* 可以单独设置某一个,也可以按顺时针的顺序依次设 */
/* 如果不满四个则逆向 */
div{
	margin:50px,60px,30px;
	<!-- 外边距的值顺时针依此变成了50px,60px,30px,40px-->
}

(3).注意

叠压现象

常态下,兄弟标签之间的上下间距以大值为准

<!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>margin</title>
</head>
<style>
    div{
        width: 100px;
        line-height: 100px;
        background-color: aqua;
        margin-bottom: 80px;
    }
    p{
        width: 100px;
        line-height: 100px;
        background-color: aquamarine;
        margin-top: 150px;
    }
</style>
<body>
    <div>CSDN</div>
    <p>CSDN</p>
</body>
</html>

在这里插入图片描述
塌陷问题

<!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>margin</title>
</head>
<style>
    body{
        background-color: pink;
    }
    .fa{
        width: 300px;
        height: 300px;
        background-color: cyan;
    }
    .son{
        width:100px;
        height: 100px;
        background-color: gold;
        /* margin-top: 50px; */
    }
</style>
<body>
    <div class="fa">
        <div class="son"></div>
    </div>
</body>
</html>

在这里插入图片描述

<!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>margin</title>
</head>
<style>
    body{
        background-color: pink;
    }
    .fa{
        width: 300px;
        height: 300px;
        background-color: cyan;
    }
    .son{
        width:100px;
        height: 100px;
        background-color: gold;
        margin-top: 50px;
        
    }
</style>
<body>
    <div class="fa">
        <div class="son"></div>
    </div>
</body>
</html>

在这里插入图片描述

原因:常态下,父级的第一个子级标签是个块标签,并使用看margin或者margin-top

解决方案:

  1. 在父级使用border(会改变盒模型面积)
  2. 在父级将margin-top改为padding-top(面积会更改需要高边高度)
    3.在父级添加 overflow:hidden(隐藏溢出)

overflow:hidden的作用

  1. 隐藏溢出
  2. 解决浮动
  3. 清除浮动

三、怪异盒模型

正常盒模型向外扩展,怪异盒模型向内压缩

<!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>test</title>
    <style>
        div{
            width: 200px;
            height: 200px;
            background-color: cyan;
            margin: 10px;

            padding: 10px;
            border: 10px solid gold;   
        }
        .d2{
            /* 怪异盒模型 */
            box-sizing: border-box;
        }
    </style>
</head>
<body>
    <div class="d1"></div>
    <div class="d2"></div>
</body>
</html>

怪异盒模型

四、浮动

定义

定义:元素脱离文档、按照指定方向发生移动,遇到父及边界环行

取值

none 不浮动
left 左浮动
right 右浮动
inherit 继承父级

作用

解决水平布局问题

<!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>浮动</title>
    <style>
        div,span{
            width: 100px;
            height: 100px;
            color: aliceblue;
            font-size: 30px;
            text-align: center;
            line-height: 100px;

            /* 浮动 */
            float: left;
        }
    </style>

</head>
<body>
    <div style="background-color: tomato;">A</div>
    <div style="background-color: greenyellow;">B</div>
    <div style="background-color: skyblue;">C</div>

    <span  style="background-color: tomato;">a</span>
    <span  style="background-color: greenyellow;">b</span>
    <span  style="background-color: skyblue">c</span>
</body>
</html>

在这里插入图片描述

特性

所有的表情都会同排显示
行标签支持所有CSS样式
默认内容撑开行高
元素脱离文档流

五、清除浮动

为什么要清除浮动

清除浮动前demo

<!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>清除浮动</title>
    <style>
        .first{
            background-color: skyblue;
        }
        .first div{
            width: 100px;
            height: 100px;
            background-color: hotpink;
            float: left;
        }
        .second{
            height: 300px;
            background-color: gold;
        }
    </style>
</head>
<body>
    <div class="first">
        <div>1</div>
        <div>2</div>
    </div>
    <div class="second"></div>
</body>
</html>

在这里插入图片描述

蓝色区域消失
元素在使用浮动后会脱离文档流,父级元素检测不到子级元素的存在
高度无法撑开

解决方案

  1. 在父级使用height (拓展性不太好)
  2. 在父级使用overflow:hidden (够直接,省掉了其他不相关的操作)
  3. 使用clear (需要添加一个空样式)

清除浮动后demo

用height时demo
<!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>清除浮动</title>
    <style>
        .first{
            background-color: skyblue;
            height: 200px;
            /* overflow: hidden; */

        }
        .first div{
            width: 100px;
            height: 100px;
            background-color: hotpink;
            float: left;
        }
        .second{
            height: 300px;
            background-color: gold;
        }
    </style>
</head>
<body>
    <div class="first">
        <div>1</div>
        <div>2</div>
        <div>1</div>
        <div>2</div>
        <div>1</div>
        <div>2</div>
        <div>1</div>
        <div>2</div>
        <!-- <section style="clear: both;"></section> -->
    </div>
    <div class="second"></div>
</body>
</html>

在这里插入图片描述

用overf:hidden;或clear时的demo

在这里插入图片描述

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值