【前端学习笔记】盒子模型

盒子模型图解

在这里插入图片描述

内边距

  1. 只设置一个值,4个方向一样
    padding: 30px;
  2. 设置两个值,上下, 左右
    padding: 20px, 30px;
  3. 3个值,上,左右,下
    padding: 20px, 30px,40px;
  4. 四个值:上,右,下,左
    padding: 20px, 30px,40px, 50px;

内边距的特点

  1. 背景色蔓延到内边距

  2. 可以设置单一方向
    padding-top,padding-bottom,padding-left,padding-right

  3. *{
    padding: 0;
    }
    清掉所有的内边距

  4. 内边距不支持负数

边框

代码示例:

<!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>
        .box1{
            width:100px;
            height: 100px;
            background-color: yellow;
            border: 10px solid red;
        }
    </style>
</head>
<body>
    <div class="box1"></div>
</body>
</html>

样式属性:
solid :实线
double:双实线
dashed:虚线
dotted:点状虚线

背景色也能蔓延到边框

单向的边框:

.box2{
            width: 100px;
            height: 100px;
            background: blue;
            border-top: 5px solid red;
        }

外边距

例如:margin: 50px;
margin-top, margin-bottom,margin-left, margin-right

外边距特性

  1. 背景色无法蔓延
  2. *{margin:0}
  3. 支持设置负数
  4. 屏幕居中:margin:0 auto;横向居中

特性问题:

  1. 兄弟关系,两个合租垂直外边距与水外边距问题
    • 垂直方向上,外边距取最大值
    • 水平方向,外边距会进行合并处理
    • 代码示例:
<!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>
        .box1,.box2,.box3,.box4{
            width: 200px;
            height: 200px;
        }
        .box1{
            background-color: red;
            margin-bottom: 100px;
        }
        .box2{
            margin-top: 50px;
            background-color: yellow;
        }
        .box3{
            float: left;
            background-color: blue;
            margin-right: 100px;
        }
        .box4{
            float: left;
            background-color: green;
            margin-left: 100px;
        }
    </style>
</head>
<body>
    <div class="box1"></div>
    <div class="box2"></div>
    <div class="box3"></div>
    <div class="box4"></div>
</body>
</html>

2 . 父子关系,给子加外边距,但作用于父元素身上了,怎么解决
- 子margin-top与负的padding-top相同,注意高度计算
- 给父盒子设置边框
- 设置浮动
- overflow:hidden。 构建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>
        *{
            margin: 0;
            padding: 0;
        }
        img{
            display: block;
        }
        #box{
            width: 241px;
            height: 335px ;
            /* background-color: yellow; */
            margin: 0 auto;
            padding: 11px 11px 18px
        }
        .title{
            width: 235px;
            height: 19px;
            /* background-color: red; */
            padding-left: 4px;
            border-left: 2px solid #254282;
            color: #254282;
            line-height: 19px;
            font-size: 14px;
            font-weight: bold;
            margin-bottom: 12px;
        }
        .bigpic{
            width: 241px;
            height: 170px;
            margin-bottom: 16px;
        }
        .bigpic p{
            width: 241px;
            height:26px;
            background: #f6f7f8;
            font-size: 12px;
            text-align: center;
            line-height: 26px;
        }
        .smallpic{
            width: 241px;
            height: 120px;
        }
        .smallpic p{
            font-size: 12px;
            width:112px;
            background-color: #f6f7f8;
            line-height: 18px;
        }
        .left{
            float: left;
        }
        .right{
            float: right;
        }
    </style>
</head>
<body>
    <div id="box">
        <div class="title">女人图片</div>
        <div class="bigpic">
            <img src="../../img/big.png" alt="">
            <p>大姐利用“异业联盟”经营美容院”</p>
        </div>
        <div class="smallpic">
            <div class="left">
                <img src="../../img/small1.png" alt="">
                <p>无效化妆,无效穿搭≠内容焦虑</p>
            </div>
            <div class="right">
                <img src="../../img/small2.png" alt="">
                <p>小个子不要怕!"高人一等"的穿搭给你</p>
            </div>
        </div>
    </div>
</body>
</html>

效果图如下:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值