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中,这样我们相当于把两个类合成一个类在用,不过可以复用的类我们就摘出去用,需要对每个卡片设置的部分就放到对应的类里。这样给我们的开发带来了极大的便利。
相关推荐
daols887 分钟前
vue 甘特图 vxe-gantt 任务条拖拽自动更新日期
javascript·vue.js·甘特图
kisbad16 分钟前
Day 036|OpenAI Agents SDK 快速开始:今天跑通第一个 Agent
java·前端·javascript
demodeom1 小时前
Win11 + RTX 5080 本地 Coding 模型测试笔记(已弃坑)
笔记
xiaominlaopodaren2 小时前
three.js地图数学基础(八):浮点精度
javascript·gis·three.js
SendTomo2 小时前
跨设备文件互传新方案
javascript·网络·webrtc·html5·p2p
__zRainy__2 小时前
Node系列 · Node基础:全局变量与全局对象
开发语言·前端·javascript
peijiping2 小时前
Agent 工具执行:并行(parallel_tool_calls)和后台(run_in_background)到底有什么区别? (学习笔记)
笔记·学习·ai agent·claude code
Oll Correct2 小时前
Adobe illustrator 案例四:吃豆人 (Pac-Man)图标绘制
笔记·ui·adobe·illustrator
sunly_2 小时前
TypeScript总结:15、类型速查
前端·javascript·typescript
Brown.alexis2 小时前
es6知识点3-自备使用
前端·javascript·es6