--name:value
方式
- 在
html
标签属性style
中定义变量名和变量值,形式如--name:value
- 在
css
中使用var(--name)
<div class="box" style="--top:200px;--left:100"></div>
<style>
.box {
width: 200px;
height: 200px;
background: #ccc;
position: absolute;
top: var(--top);
left: calc(var(--left) * 1px);
}
</style>
data-name=vlaue
方式
- 在
html
标签中定义,形式如data-name=vlaue
- 在
css
中使用attr(data-name)
<h1 data-text="悬崖上的金鱼姬">悬崖上的金鱼姬</h1>
<style>
h1::before {
content: attr(data-text);
position: absolute;
top: 0;
left: 0;
text-shadow: -5px 5px 20px #333,
-10px 10px 30px #333,
-20px 15px 40px #333,
-25px 20px 50px #333;
z-index: -1;
}
</style>
案例一:
实现效果如图所示:
代码
<body>
<div class="img-box">
<img src="./images/2000171622.jpeg" alt="" style="--i: 2; --j: 0" />
<img src="./images/2000171622.jpeg" alt="" style="--i: 1; --j: 1" />
<img src="./images/2000171622.jpeg" alt="" style="--i: 0; --j: 2" />
<img src="./images/2000171622.jpeg" alt="" style="--i: -1; --j: 3" />
<img src="./images/2000171622.jpeg" alt="" style="--i: -2; --j: 4" />
</div>
</body>
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
body {
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
background-color: #262e31;
}
.img-box {
position: relative;
width: 300px;
height: 300px;
margin: 100px auto;
transform-style: preserve-3d;
perspective: 1000px;
}
.img-box img {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
object-fit: cover;
transition: 0.5s;
z-index: calc(var(--i)+1);
opacity: calc(1- (0.2 * var(--j)));
}
.img-box:hover img {
transform: rotate(-30deg) skew(25deg) translateX(calc(-35px * var(--i)))
translateY(calc(35px * var(--i)));
box-shadow: -20px 20px 20px rgba(0, 0, 0, 0.25);
}
案例二
实现效果如图:
代码
<!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>Wooden Text Typography</title>
<style>
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
body {
display: flex;
align-items: center;
justify-content: center;
min-height: 100vh;
overflow: hidden;
background: url(../images/2000171622.jpeg);
background-size: 100% 100%;
}
h1 {
position: relative;
margin: 0;
padding: 0;
text-transform: uppercase;
text-align: center;
font-size: 10rem;
font-weight: 700;
background: url(../images/2000171622.jpeg);
background-size: 100% 100%;
background-attachment: fixed;
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
h1::before {
content: attr(data-text);
position: absolute;
top: 0;
left: 0;
text-shadow: -5px 5px 20px #333,
-10px 10px 30px #333,
-20px 15px 40px #333,
-25px 20px 50px #333;
z-index: -1;
}
</style>
</head>
<body>
<h1 data-text="悬崖上的金鱼姬">悬崖上的金鱼姬</h1>
</body>
</html>