大家好,又见面了,我是你们的朋友全栈君。
box-shadow
属性可以设置一个或多个下拉阴影的框可以在同一个元素上设置多个阴影效果,并用逗号将他们分隔开。该属性可设置的值包括阴影的X轴偏移量、Y轴偏移量、模糊半径、扩散半径和颜色。
语法:
box-shadow: h-shadow v-shadow blur spread color inset;
div.box{
/* x偏移量 | y偏移量 | 阴影模糊半径 | 阴影扩散半径 | 阴影颜色 */
box-shadow: 2px 2px 2px 1px rgba(0, 0, 0, 0.2);
}
值 | 描述 |
---|---|
h-shadow | 必需的。水平阴影的位置。允许负值 |
v-shadow | 必需的。垂直阴影的位置。允许负值 |
blur | 可选。模糊距离 |
spread | 可选。阴影的大小 |
color | 可选。阴影的颜色。在CSS颜色值寻找颜色值的完整列表 |
inset | 可选。从外层的阴影(开始时)改变阴影内侧阴影 |
注意:这里 inset
参数只能设置在第一或者最后,其他位置无效!
===直接上代码===
(1)h-shadow 和 v-shadow 两个值表示阴影的偏移量
<!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>
div{
margin: 20px;
display: inline-block;
width: 100px;
height: 100px;
background-color: red;
}
.box1{
box-shadow: 10px 10px blue;
}
.box2{
box-shadow: -10px -10px blue;
}
.box3{
box-shadow: -10px 10px blue;
}
.box4{
box-shadow: 10px -10px blue;
}
</style>
</head>
<body>
<div class="box1"></div>
<div class="box2"></div>
<div class="box3"></div>
<div class="box4"></div>
</body>
</html>
结论:从上面结果可以看出,两个属性值可以取正值也可以取负值,并且方向为坐标系取值方向相同(x轴正值向右负值向左,y轴正值向下负值向上)
(2)blur 属性值表示阴影的模糊距离/半径(可选)
<!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>
div{
margin: 20px;
display: inline-block;
width: 100px;
height: 100px;
background-color: red;
}
.box1{
/* 模糊距离为5 */
box-shadow: 10px 10px 5px blue;
}
.box2{
/* blur值为0等同于没设置模糊距离,没有模糊效果 */
box-shadow: -10px -10px 0px blue;
}
.box3{
/* blur值不能为负数,为负数则阴影失效 */
box-shadow: -10px 10px -5px blue;
}
.box4{
/* blur值越大越模糊 */
box-shadow: -10px 10px 20px blue;
}
</style>
</head>
<body>
<div class="box1"></div>
<div class="box2"></div>
<div class="box3"></div>
<div class="box4"></div>
</body>
</html>
结论:如果不写该参数或者该参数为0则阴影完全实心,没有模糊效果,并且该值越大阴影越模糊
(3) spread 属性值表示设置的阴影大小(可选)
这个值可以被看作是从元素到阴影的距离
<!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>
div{
margin: 20px;
display: inline-block;
width: 100px;
height: 100px;
background-color: red;
}
.box1{
/* spread为0(默认值) */
box-shadow: 0px 0px 10px 0px blue;
}
.box2{
/* spread为5(正值会在元素的四个方向延伸阴影) */
box-shadow: 0px 0px 10px 5px blue;
}
.box3{
/* spread为-1(负值会使阴影变得比元素本身尺寸还要小) */
box-shadow: 0px 0px 10px -1px blue;
}
</style>
</head>
<body>
<div class="box1"></div>
<div class="box2"></div>
<div class="box3"></div>
</body>
</html>
结论:
(4) color 属性值指定阴影的颜色
<!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>
div{
margin: 20px;
display: inline-block;
width: 100px;
height: 100px;
background-color: red;
}
.box1{
/* color值表示阴影颜色 */
box-shadow: 0px 0px 10px 5px blue;
}
.box2{
box-shadow: 0px 0px 10px 5px green;
}
.box3{
box-shadow: 0px 0px 10px 5px orange;
}
</style>
</head>
<body>
<div class="box1"></div>
<div class="box2"></div>
<div class="box3"></div>
</body>
</html>
(5)inset 设置阴影为内侧
<!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>
div{
margin: 20px;
display: inline-block;
width: 100px;
height: 100px;
background-color: red;
}
.box1{
box-shadow: 0px 0px 10px 5px blue;
}
.box2{
/* inset设置阴影为内侧阴影 */
box-shadow: 0px 0px 10px 5px blue inset;
}
</style>
</head>
<body>
<div class="box1"></div>
<div class="box2"></div>
</body>
</html>
至此,box-shadow盒子阴影属性已经介绍完毕
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/155208.html原文链接:https://javaforall.cn