HTML&CSS :生日蛋糕

这段代码通过HTML和CSS实现了一个静态的蛋糕图形,通过伪元素和渐变背景色模拟了蛋糕的细节和装饰。整体效果是一个具有层次感和装饰性的蛋糕。


大家复制代码时,可能会因格式转换出现错乱,导致样式失效。建议先少量复制代码进行测试,若未能解决问题,私信我,我会发送完整的压缩包给你。

演示效果

HTML&CSS

html 复制代码
<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>公众号关注:前端Hardy</title>
    <style>
        body {
            text-align: center;
            height: 100%;
            margin: 0px;
            background-color: #84CBC6;
        }

        #cheesecake {
            margin: 10px auto;
            position: relative;
            height: 510px;
            width: 510px;
        }

        #cheesecake div {
            position: absolute;
        }

        #cheesecake div::before,
        #cheesecake div::after {
            content: '';
            position: absolute;
            display: block;
        }

        .base {
            width: 170px;
            height: 142px;
            background-image: linear-gradient(#f69a9c 20px, #f7e498 20px, #f7e498 90px, #f8e182 90px, #f8e182 127px, #deac59 127px);
            bottom: 156px;
            left: 170px;
            border-top-left-radius: 15px;
            border-top-right-radius: 15px;
            border: 6px solid #414042;
        }

        .base::before {
            width: 17px;
            height: 42px;
            border-radius: 0 0 17px 17px;
            background-color: #f69a9c;
            box-shadow: 17px -17px #f69a9c, 95px -17px #f69a9c, 126px 0 #f69a9c;
            left: 13px;
            top: 19px;
        }

        .base::after {
            width: 4px;
            height: 4px;
            background-color: #deac59;
            border-radius: 4px;
            box-shadow: 10px -10px #deac59;
            right: 15px;
            bottom: 20px;
        }

        .toppings {
            width: 108px;
            height: 22px;
            border: 6px solid #414042;
            border-radius: 22px;
            background-color: white;
            top: 184px;
            left: 200px;
        }

        .toppings::before {
            width: 71px;
            height: 22px;
            border: 6px solid #414042;
            border-radius: 22px;
            background-color: white;
            top: -22px;
            left: 12px;
            z-index: -1;
        }

        .toppings::after {
            width: 22px;
            height: 22px;
            border: 6px solid #414042;
            border-radius: 22px;
            background-color: #f26d72;
            top: -44px;
            left: 36px;
            z-index: -1;
        }

        .plate {
            width: 264px;
            height: 6px;
            background-color: #414042;
            border-radius: 6px;
            bottom: 156px;
            left: 130px;
        }

        .plate::before {
            width: 235px;
            height: 10px;
            background-color: #cdfffb;
            border: 6px solid #414042;
            border-radius: 0 0 20px 20px;
            left: 8px;
        }

        .plate::after {
            width: 10px;
            height: 10px;
            border-radius: 10px;
            border: 6px solid transparent;
            border-left: 6px solid #414042;
            bottom: 195px;
            left: 128px;
            transform: rotate(45deg);
            -webkit-transform: rotate(45deg);
            -moz-transform: rotate(45deg);
            -ms-transform: rotate(45deg);
            -o-transform: rotate(45deg);
        }

        .glasee {
            width: 40px;
            height: 4px;
            background-color: white;
            border-radius: 4px;
            top: 212px;
            left: 182px;
        }

        .glasee::after {
            width: 13px;
            height: 4px;
            background-color: white;
            border-radius: 4px;
            left: 46px;
        }

        .glasee::before {
            width: 6px;
            height: 6px;
            background-color: white;
            border-radius: 6px;
            top: -55px;
            left: 69px;
        }
    </style>
</head>

<body>
    <div id="cheesecake">
        <div class="toppings"></div>
        <div class="base"></div>
        <div class="plate"></div>
        <div class="glasee"></div>
    </div>
</body>

</html>

HTML 结构

  • #cheesecake:这是整个蛋糕的容器,用于定位和布局各个部分。
  • .toppings:表示蛋糕的装饰部分。
  • .base:表示蛋糕的主体部分。
  • .plate:表示蛋糕的底座或盘子。
  • .glasee:可能是用于装饰的玻璃纸或其他装饰物。

CSS 样式

  • #cheesecake:定义蛋糕整体的容器,尺寸为510px × 510px,居中显示,用于布局内部元素。
  • #cheesecake div:设置内部所有div元素为绝对定位,便于在容器内自由布局。
  • .base:蛋糕主体部分,尺寸为170px × 142px,渐变背景模拟多层蛋糕,顶部圆角,带6px深灰色边框。
  • .base::before:在蛋糕主体上生成多个红色草莓装饰,通过box-shadow实现。
  • .base::after:在蛋糕主体右侧添加一个高光点,增强立体感。
  • .toppings:蛋糕顶部的白色装饰层,尺寸为108px × 22px,圆角边框,带6px深灰色边框。
  • .toppings::before:在装饰层上方生成一个白色延伸部分,增加层次感。
  • .toppings::after:在装饰层上方生成一个红色圆形装饰(樱桃)。
  • .plate:蛋糕底部的盘子,尺寸为264px × 6px,深灰色背景,圆角边框。
  • .plate::before:在盘子上方生成一个白色内衬,带6px深灰色边框,底部圆角。
  • .plate::after:在盘子左侧生成一个旋转45°的小三角装饰,模拟盘子的立体感。
  • .glasee:蛋糕上的玻璃纸装饰,尺寸为40px × 4px,白色背景,圆角边框。
  • .glasee::after:在玻璃纸右侧生成一个白色延伸部分。
  • .glasee::before:在玻璃纸上方生成一个白色圆形高光点,增加立体感。

---各位互联网搭子,要是这篇文章成功引起了你的注意,别犹豫,关注、点赞、评论、分享走一波,让我们把这份默契延续下去,一起在知识的海洋里乘风破浪!

相关推荐
cll_8692418912 小时前
一个好看的Wordpress博客文字css样式
前端·css·ui
三翼鸟数字化技术团队2 小时前
一种前端大屏适配方案
前端·css
muddjsv4 小时前
CSS核心语法精讲:彻底吃透规则结构、选择器与声明规范
前端·css
shandianchengzi5 小时前
【开源工具】我做了一个周深歌曲淘汰赛生成器:挑选 128 首歌 PK,选出你的年度 TOP1
html·音乐·网页·世界杯
触底反弹14 小时前
一文搞懂 Tailwind CSS 弹性布局:从原理到实战
前端·css·html
小白巨白18 小时前
玫瑰花园管理系统:AI识病+3D可视化,一套面向中小型玫瑰种植园的数字化管理工具
css·人工智能·计算机视觉·html5
两点王爷1 天前
一个快速加载面和多面数据的html测试页面(可以导出geojson数据文件)
前端·html·gis
杨超越luckly1 天前
Agent应用指南:长三角41城跨城人口流动OD数据分析
python·数据挖掘·数据分析·html·可视化
alexander0681 天前
CSS 类选择器组合
前端·css
不好听6132 天前
HTML 事件监听机制:从 DOM Level 0 到 React 合成事件
前端·react.js·html