当前位置:首页 > CSS

css制作日历

2026-02-26 21:24:13CSS

使用CSS制作日历的基本方法

日历的布局可以通过CSS Grid或Flexbox实现。以下是一个简单的日历结构示例:

<div class="calendar">
  <div class="header">2023年10月</div>
  <div class="weekdays">
    <div>日</div>
    <div>一</div>
    <div>二</div>
    <div>三</div>
    <div>四</div>
    <div>五</div>
    <div>六</div>
  </div>
  <div class="days">
    <!-- 日期单元格 -->
    <div>1</div>
    <div>2</div>
    <!-- 更多日期... -->
  </div>
</div>

CSS样式设置

日历容器使用Grid布局:

.calendar {
  width: 300px;
  border: 1px solid #ddd;
  border-radius: 8px;
  overflow: hidden;
  font-family: Arial, sans-serif;
}

星期标题行样式:

css制作日历

.weekdays {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  background: #f5f5f5;
  text-align: center;
  font-weight: bold;
  padding: 8px 0;
}

日期单元格布局:

.days {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  grid-gap: 1px;
  background: #eee;
}

.days div {
  padding: 10px;
  text-align: center;
  background: white;
  min-height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
}

添加交互效果

悬停效果:

css制作日历

.days div:hover {
  background: #e6f7ff;
  cursor: pointer;
}

当前日期高亮:

.days .today {
  background: #1890ff;
  color: white;
  border-radius: 50%;
}

响应式设计

适应不同屏幕尺寸:

@media (max-width: 600px) {
  .calendar {
    width: 100%;
  }

  .days div {
    padding: 5px;
    min-height: 30px;
  }
}

完整示例代码

结合HTML和CSS的完整日历实现:

<!DOCTYPE html>
<html>
<head>
<style>
.calendar {
  width: 300px;
  border: 1px solid #ddd;
  border-radius: 8px;
  overflow: hidden;
  font-family: Arial, sans-serif;
}

.header {
  background: #1890ff;
  color: white;
  text-align: center;
  padding: 10px;
  font-size: 18px;
}

.weekdays {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  background: #f5f5f5;
  text-align: center;
  font-weight: bold;
  padding: 8px 0;
}

.days {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  grid-gap: 1px;
  background: #eee;
}

.days div {
  padding: 10px;
  text-align: center;
  background: white;
  min-height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.days div:hover {
  background: #e6f7ff;
  cursor: pointer;
}

.days .today {
  background: #1890ff;
  color: white;
  border-radius: 50%;
}
</style>
</head>
<body>

<div class="calendar">
  <div class="header">2023年10月</div>
  <div class="weekdays">
    <div>日</div>
    <div>一</div>
    <div>二</div>
    <div>三</div>
    <div>四</div>
    <div>五</div>
    <div>六</div>
  </div>
  <div class="days">
    <div>1</div>
    <div>2</div>
    <div>3</div>
    <div>4</div>
    <div>5</div>
    <div>6</div>
    <div>7</div>
    <!-- 更多日期... -->
  </div>
</div>

</body>
</html>

标签: 日历css
分享给朋友:

相关文章

css制作时钟

css制作时钟

CSS 制作时钟的方法 使用纯 CSS 和少量 JavaScript 可以制作一个动态时钟。以下是实现步骤: HTML 结构 <div class="clock"> <div…

淘宝导航css制作

淘宝导航css制作

淘宝导航CSS制作 淘宝导航栏通常包含多个层级,包括主导航、子导航等。以下是实现类似淘宝导航栏的CSS制作方法。 基础HTML结构 <div class="nav-container">…

咖啡店css制作

咖啡店css制作

咖啡店网页CSS设计要点 布局与配色 咖啡店网页通常采用温暖自然的配色方案,如棕色、米色、浅绿色等大地色系。主色调可选择咖啡色(#6F4E37)作为品牌色,搭配奶油色(#FFF8E8)作为背景。使用C…

制作外置css

制作外置css

创建外置CSS文件 新建一个文本文件,将其命名为style.css。确保文件扩展名为.css,而不是.txt。使用代码编辑器(如VS Code、Sublime Text等)打开该文件进行编辑。 /*…

css制作网页

css制作网页

CSS 网页设计基础 CSS(层叠样式表)用于控制网页的视觉呈现,包括布局、颜色、字体等。以下为关键方法和示例: 基础结构 HTML 文件需链接 CSS: <link rel="styles…

css 制作表单

css 制作表单

使用CSS制作表单的基本方法 创建表单时,HTML负责结构,CSS负责样式。表单元素包括输入框、按钮、下拉菜单等,通过CSS可以统一风格并提升用户体验。 HTML结构示例 <form cla…