当前位置:首页 > CSS

css制作雪花

2026-02-27 03:05:41CSS

CSS 制作雪花的方法

使用 CSS 可以创建简单的雪花效果,主要通过伪元素、动画和变换实现。以下是几种常见的方法:

基础雪花形状

通过 ::before::after 伪元素创建雪花的六边形结构:

.snowflake {
  position: relative;
  width: 10px;
  height: 10px;
  background-color: white;
  border-radius: 50%;
}

.snowflake::before, .snowflake::after {
  content: "";
  position: absolute;
  background-color: white;
}

.snowflake::before {
  width: 10px;
  height: 2px;
  top: 4px;
  left: 0;
}

.snowflake::after {
  width: 2px;
  height: 10px;
  left: 4px;
  top: 0;
}

旋转雪花分支

通过 transform 添加更多分支:

.snowflake {
  position: relative;
  width: 20px;
  height: 20px;
}

.snowflake .branch {
  position: absolute;
  width: 2px;
  height: 10px;
  background-color: white;
  top: 5px;
  left: 9px;
  transform-origin: bottom center;
}

.branch:nth-child(1) { transform: rotate(0deg); }
.branch:nth-child(2) { transform: rotate(60deg); }
.branch:nth-child(3) { transform: rotate(120deg); }
.branch:nth-child(4) { transform: rotate(180deg); }
.branch:nth-child(5) { transform: rotate(240deg); }
.branch:nth-child(6) { transform: rotate(300deg); }

添加动画效果

让雪花旋转或飘落:

@keyframes spin {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

.snowflake {
  animation: spin 3s linear infinite;
}

@keyframes fall {
  from { transform: translateY(-100px); }
  to { transform: translateY(100vh); }
}

.falling-snowflake {
  animation: fall 5s linear infinite;
}

完整示例

结合形状和动画的完整代码:

css制作雪花

<style>
  .snowflake {
    position: absolute;
    width: 20px;
    height: 20px;
    top: 0;
    left: 50%;
    animation: fall 5s linear infinite;
  }

  .branch {
    position: absolute;
    width: 2px;
    height: 8px;
    background-color: white;
    top: 6px;
    left: 9px;
    transform-origin: bottom center;
  }

  .branch:nth-child(1) { transform: rotate(0deg); }
  .branch:nth-child(2) { transform: rotate(60deg); }
  .branch:nth-child(3) { transform: rotate(120deg); }
  .branch:nth-child(4) { transform: rotate(180deg); }
  .branch:nth-child(5) { transform: rotate(240deg); }
  .branch:nth-child(6) { transform: rotate(300deg); }

  @keyframes fall {
    from { transform: translateY(-100px) rotate(0deg); }
    to { transform: translateY(100vh) rotate(360deg); }
  }
</style>

<div class="snowflake">
  <div class="branch"></div>
  <div class="branch"></div>
  <div class="branch"></div>
  <div class="branch"></div>
  <div class="branch"></div>
  <div class="branch"></div>
</div>

这些方法可以根据需要调整大小、颜色和动画效果,创造出各种风格的雪花效果。

标签: 雪花css
分享给朋友:

相关文章

纯css制作下拉菜单

纯css制作下拉菜单

纯CSS下拉菜单实现方法 通过CSS的:hover伪类和display属性可以实现纯CSS下拉菜单,无需JavaScript。以下是具体实现方式: HTML结构示例: <nav class=…

使用css制作同心圆

使用css制作同心圆

使用 CSS 制作同心圆 同心圆可以通过嵌套多个圆形元素并利用 CSS 的定位和样式属性实现。以下是几种常见方法: 方法一:使用绝对定位和多个 div 通过嵌套多个 div 元素,设置相同的圆心位置…

css制作导航条

css制作导航条

导航条基础结构 使用HTML构建导航条的基本结构,通常包含一个<nav>标签和多个<a>标签作为导航链接。示例代码如下: <nav class="navbar">…

css样式表制作

css样式表制作

CSS样式表制作基础 CSS(层叠样式表)用于控制网页的视觉呈现。通过CSS可以定义字体、颜色、布局等样式属性。 基本语法结构 选择器 { 属性: 值; 属性: 值; } 示例代码 body {…

css 制作字体

css 制作字体

自定义字体方法 使用 @font-face 规则引入自定义字体文件(如 .woff, .woff2, .ttf)。需定义字体名称和文件路径: @font-face { font-family:…

css制作扇形

css制作扇形

使用 CSS 制作扇形的方法 通过 CSS 的 border-radius 和 transform 属性可以轻松实现扇形效果。以下是几种常见的实现方式: 方法一:使用 border-radius 和…