background-color是对哪一部分起作用

先看以下代码

<!DOCTYPE html>
<html lang="zh">
    <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>
    </head>
    <style>
        .box1{
            width:200px;
            height:200px;
            background-color: #bfa;
            /* padding:200px; */
            /* border:20px solid transparent;
            outline:2px solid tomato; */
        }
    </style>
    <body>
        <div class="box1"></div>
    </body>
</html>

在这里插入图片描述

设置padding后(注意.box1的高度是固定的)

<!DOCTYPE html>
<html lang="zh">
    <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>
    </head>
    <style>
        .box1{
            width:200px;
            height:200px;
            background-color: #bfa;
            padding:200px;
            /* border:20px solid transparent;
            outline:2px solid tomato; */
        }
    </style>
    <body>
        <div class="box1"></div>
    </body>
</html>

在这里插入图片描述

现在明白了,给元素设置background-color后,其内容区也会变色的原因了吧

设置border后,并且border的颜色为红色

<!DOCTYPE html>
<html lang="zh">
    <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>
    </head>
    <style>
        .box1{
            width:200px;
            height:200px;
            background-color: #bfa;
            padding:200px;
            border:20px solid red;
            /* border:20px solid transparent; */
            /* outline:2px solid tomato;  */
        }
    </style>
    <body>
        <div class="box1"></div>
    </body>
</html>

在这里插入图片描述

设置border的颜色为透明色(transparent)时:会发现,background-color 颜色默认会蔓延到border的区域

<!DOCTYPE html>
<html lang="zh">
    <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>
    </head>
    <style>
        .box1{
            width:200px;
            height:200px;
            background-color: #bfa;
            padding:200px;
            /* border:20px solid red; */
            border:20px solid transparent;
            /* outline:2px solid tomato;  */
        }
    </style>
    <body>
        <div class="box1"></div>
    </body>
</html>

在这里插入图片描述

为了更清楚地看到,默认情况下,background-color的颜色会蔓延到boder处,我们可以给.box1设置outline来观察

<!DOCTYPE html>
<html lang="zh">
    <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>
    </head>
    <style>
        .box1{
            width:200px;
            height:200px;
            background-color: #bfa;
            padding:200px;
            /* border:20px solid red; */
            border:20px solid transparent;
            outline:2px solid tomato; 
        }
    </style>
    <body>
        <div class="box1"></div>
    </body>
</html>

在这里插入图片描述

outline会给外边距上色吗?

<!DOCTYPE html>
<html lang="zh">
    <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>
    </head>
    <style>
        .box1{
            width:200px;
            height:200px;
            background-color: #bfa;
            padding:200px;
            /* border:20px solid red; */
            margin:50px;
            border:20px solid transparent;
            outline:2px solid tomato; 
        }
    </style>
    <body>
        <div class="box1"></div>
    </body>
</html>

在这里插入图片描述

从上面看出,很明显,outline最多从元素的boreder外着色

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值