CSS中使用定义在html属性中的变量基础

--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>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值