CSS笔记(二)类名复用

这里我通过两张不同位置的卡片来实现效果

  • 代码
html 复制代码
<!DOCTYPE html>
<html>
    <head>
        <style>
            /*设置画布*/
            body{
                /* 方便排列与对齐*/
                display: flex; 
                /*画布布满整个窗口*/
                height: 100vh;
                /*水平居中*/
                justify-content: center;

                /*垂直居中*/
                align-items: center;
                /* 设置比较暗的背景色*/
                background-color: rgba(47,48,49,0.9);
            }
            /*设置活动区域*/
            .container{
                display: flex;
                justify-content: center;
                align-items: center;
                /*给子元素提供相对描点*/
                position: relative;
                width: 500px;
                height: 500px;
            }
            .card{
                
                /*表示该元素会以相对锚点改变位置*/
                position: absolute;
                width: 150px;
                height: 200px;
                display: flex;
                justify-content: center;
                align-items: center;
                font-size: 30px;
                font-weight: bold;
                background-color: #00ff44;
                
                /*设置阴影,切记参数之间不要有逗号*/
                box-shadow: 0px 8px 8px rgba(0,0,0,0.8);
            }
            .card1{
                left: 0;
                top:0;
            }
            .card2{
                left:300;
                top:100p;
            }
        </style>
    </head>
    <body>
        <div class="container">
            <div class="card card1">Card1</div>
            <div class="card card2 ">Card2</div>
        </div>
    </body>
</html>
  • 效果:

  • 显然这里我们让卡片元素具有了两个类cardcard1,我们把相对锚点的位置信息放到card1中,这样我们相当于把两个类合成一个类在用,不过可以复用的类我们就摘出去用,需要对每个卡片设置的部分就放到对应的类里。这样给我们的开发带来了极大的便利。
相关推荐
Zhan8611245 分钟前
数据接口的序列号机制与丢包检测:西班牙行情数据IBEX指数实时行情接入笔记
大数据·数据结构·笔记·区块链
dsyyyyy11017 小时前
JavaScript变量
开发语言·javascript·ecmascript
kyriewen8 小时前
手写 Promise.all、race、any:不到 30 行代码,解决并发异步的所有姿势
前端·javascript·面试
闪闪发亮的小星星8 小时前
开普勒三大定律
笔记
胡志辉的博客9 小时前
深入浅出理解浏览器事件循环:从一道输出题讲到 Chrome 源码
前端·javascript·chrome·chromium·event loop
代码不加糖9 小时前
js中不会冒泡的事件有哪些?
前端·javascript·vue.js
自传.10 小时前
尚硅谷 Vibe Coding|第一章 AI 编程基础理论 学习笔记
笔记·学习·尚硅谷·vibe coding
懂懂tty10 小时前
Vue2与Vue3之间API差异
前端·javascript·vue.js
小二·11 小时前
Next.js 15 全栈开发实战
开发语言·javascript·ecmascript
chase。11 小时前
【学习笔记】SimpleVLA-RL:通过强化学习扩展 VLA 训练
笔记·学习