使用 HTML 和 CSS 设计日历
要使用 HTML 和 CSS 设计日历,我们将使用 HTML 表格。我们在日常生活中使用日历来查看日期、安排任何事件等等。
在这篇文章中,我们将了解如何仅使用 HTML 和 CSS 设计日历。我们将使用 HTML 表格创建结构,并使用 CSS 属性设计日历的 UI。
使用 HTML 和 CSS 设计日历的步骤
我们将遵循以下步骤使用 HTML 和 CSS 设计日历。
使用 HTML 构建日历结构
- 我们使用了 table 标签及其元素来创建日历的结构,其中 thead 定义包含日期名称的标题部分,tbody 定义包含所有日期的表格主体。
- 每一行都使用 tr 定义,th 用于指定日期,td 用于显示日期。当前月份使用 h2 标签显示在中心,前一个和下一个按钮使用 HTML 字符实体 在 span 标签中创建。
<div class="container"> <div class="calendar"> <h2> <span>❮</span> OCTOBER , 2024 <span>❯</span> </h2> <table cellpadding="20"> <thead> <tr> <th>Sun</th> <th>Mon</th> <th>Tue</th> <th>Wed</th> <th>Thu</th> <th>Fri</th> <th>Sat</th> </tr> </thead> <tbody> <tr> <td></td> <td></td> <td>1</td> <td>2</td> <td>3</td> <td>4</td> <td>5</td> </tr> <tr></tr> <tr> <td>6</td> <td>7</td> <td>8</td> <td>9</td> <td>10</td> <td>11</td> <td>12</td> </tr> <tr> <td>13</td> <td>14</td> <td>15</td> <td>16</td> <td>17</td> <td>18</td> <td>19</td> </tr> <tr> <td>20</td> <td>21</td> <td>22</td> <td class="current">23</td> <td>24</td> <td>25</td> <td>26</td> </tr> <tr> <td>27</td> <td>28</td> <td>29</td> <td>30</td> <td>31</td> <td></td> <td></td> </tr> </tbody> </table> </div> </div>
Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.
使用 HTML 设计日历
- 我们使用 h2 作为元素 选择器 来设计当前月份、前一个和下一个按钮。我们使用 CSS text-align 属性将标题居中,并使用 justify-content 属性在标题和两个按钮之间添加均匀的间距。
- 我们使用 container 类和 CSS flexbox 属性将日历居中。CSS display 属性用于创建一个弹性容器,并使用 justify-content 和 align-items 将日历水平和垂直居中。
- calendar 类用于设置整个表格的 背景颜色 和文本 颜色,并使用 max-width 属性设置 div 的宽度。
- 我们使用 table 作为元素选择器来居中日历的日期和日期,使用 td 作为元素选择器来增加日期的 字体大小,并使用 cursor 属性将光标更改为指针。
- 我们使用 current 类来显示当前日期,我们更改了它的背景和文本颜色,并使用了 border-radius 属性来创建圆形边框。
h2 { text-align: center; color: white; display: flex; justify-content: space-around; } span { cursor: pointer; } .container { height: 100vh; display: flex; justify-content: center; align-items: center; } .calendar { color: white; background-color: #031928; max-width: fit-content; } table { text-align: center; } td { cursor: pointer; font-size: large; } .current { background-color: beige; color: #031928; border-radius: 60%; }
完整示例代码
以下是实现上述步骤以使用 HTML 和 CSS 设计日历的完整示例代码。
<!DOCTYPE html> <html> <head> <style> h2 { text-align: center; color: white; display: flex; justify-content: space-around; } span { cursor: pointer; } .container { height: 100vh; display: flex; justify-content: center; align-items: center; } .calendar { color: white; background-color: #031928; max-width: fit-content; } table { text-align: center; } td { cursor: pointer; font-size: large; } .current { background-color: beige; color: #031928; border-radius: 60%; } </style> </head> <body> <div class="container"> <div class="calendar"> <h2> <span>❮</span> OCTOBER , 2024 <span>❯</span> </h2> <table cellpadding="20"> <thead> <tr> <th>Sun</th> <th>Mon</th> <th>Tue</th> <th>Wed</th> <th>Thu</th> <th>Fri</th> <th>Sat</th> </tr> </thead> <tbody> <tr> <td></td> <td></td> <td>1</td> <td>2</td> <td>3</td> <td>4</td> <td>5</td> </tr> <tr></tr> <tr> <td>6</td> <td>7</td> <td>8</td> <td>9</td> <td>10</td> <td>11</td> <td>12</td> </tr> <tr> <td>13</td> <td>14</td> <td>15</td> <td>16</td> <td>17</td> <td>18</td> <td>19</td> </tr> <tr> <td>20</td> <td>21</td> <td>22</td> <td class="current">23</td> <td>24</td> <td>25</td> <td>26</td> </tr> <tr> <td>27</td> <td>28</td> <td>29</td> <td>30</td> <td>31</td> <td></td> <td></td> </tr> </tbody> </table> </div> </div> </body> </html>
结论
在这篇文章中,我们学习并了解了如何使用 HTML 和 CSS 设计日历。我们使用了 HTML 表格及其元素来创建日历的结构,然后应用 CSS 来设计日历。
广告