CalendarTutorials

how to create a calendar in html and CSS

how to create a calendar in html and CSS

how to create a calendar in html and CSS

Before We Start

Every developer needs clean and modern elements to develop their websites, It contains pictures and a user interface element within a website. The most important one and the first section that the user sees when the user enters a website. how to create a calendar in html and CSS

So if you are a person who wishes to develop a website, you have to know how to create that properly.

how to create a calendar in html and CSS

We are here to solve your problem. In this article we discuss how to create these elements. Before we start please read the below articles which easier you to understand the process, if you are new to development.

Step 1 – Add HTML

It’s too easy and simple. Just copy and paste below code in your HTML editor between <body> </body> tag.

<div class="container">
  <div class="curr-month"><b>november</b></div>
  <div class="all-days">
    <ul>
      <li>sun</li>
      <li>mon</li>
      <li>tue</li>
      <li>wed</li>
      <li>thu</li>
      <li>fri</li>
      <li>sat</li>
    </ul>
  </div>
  <div class="all-date">
    <ul>
    </ul>
  </div>
</div>

Step 2 – Add CSS

Copy and paste below code in your HTML editor between <style></style> tag.

<style>
body {
  background-color: #74ebd5;
  background-image: linear-gradient(90deg, #74ebd5 0%, #9face6 100%);
}

* {
  margin: 0px;
  padding: 0px;
}

.container {
  margin: 0 auto;
  height: 487px;
  width: 421px;
  background-color: #000;
  box-shadow: 0 10px 20px 5px rgba(0, 0, 0, 0.2);
  font-family: "Raleway", sans-serif;
  text-transform: uppercase;
}

.curr-month {
  width: 100%;
  height: 80px;
  background-image: linear-gradient(to top, #3498db 70px, #02548b 70px);
  color: #fff;
  font-size: 40px;
  text-align: center;
  line-height: 80px;
}

.all-days {
  width: 100%;
  height: 40px;
  background-color: #fff;
  float: left;
}

ul {
  list-style: none;
  display: block;
  height: 30px;
  width: 421px;
  margin: 0 auto;
  float: left;
}

ul li {
  float: left;
  width: 60px;
  text-align: center;
  /*padding:10px 0px;*/

  line-height: 41px;
}

.all-date {
  width: 100%;
  height: 305px;
  float: left;
}

.all-date li {
  height: 60px;
  width: 59px;
  line-height: 60px;
  border-top: 1px solid #fff;
  border-left: 1px solid #fff;
  background-color: #ccc;
}

.b-bottom {
  border-bottom: 1px solid #fff;
}

.b-right {
  border-right: 1px solid #fff;
}

.all-date li.monthdate:hover {
  background-color: #02548b;
  color: #fff;
}

</style>

Step 3 – Add JavaScript

We use some external JavaScript link to this code. No need to worry about this, copy and paste below code between <head></head> tag.

<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
</head>

And also copy and paste below JavaScript code between <script></script> tag.

<script>
var curDate = new Date().getDate();
var curMonth = new Date().getMonth();
var curYear = new Date().getFullYear();
var startDay = new Date(curYear, curMonth, 1).getDay();
var months = [
  "January",
  "February",
  "March",
  "April",
  "May",
  "June",
  "July",
  "August",
  "September",
  "October",
  "November",
  "December"
];
var noofdays = [
  "31",
  "29",
  "31",
  "30",
  "31",
  "30",
  "31",
  "31",
  "30",
  "31",
  "30",
  "31"
];
var prevMonth = noofdays[curMonth - 1];
if (curMonth == 11) {
  prevMonth = noofdays[0];
} else if (curMonth == 0) {
  prevMonth = noofdays[11];
}
var totalDays = noofdays[curMonth];
var counter = 0;
var precounter = prevMonth - (startDay - 1);
var rightbox = 6;
var flag = true;

jQuery(".curr-month b").text(months[curMonth]);
for (var i = 0; i < 42; i++) {
  if (i >= startDay) {
    counter++;
    if (counter > totalDays) {
      counter = 1;
      flag = false;
    }
    if (flag == true) {
      jQuery(".all-date ul").append(
        '<li class="monthdate">' + counter + "</li>"
      );
    } else {
      jQuery(".all-date ul").append(
        '<li style="opacity:.8">' + counter + "</li>"
      );
    }
  } else {
    jQuery(".all-date ul").append(
      '<li style="opacity:.8">' + precounter + "</li>"
    );
    precounter++;
  }

  if (i == rightbox) {
    jQuery(jQuery(".all-date ul li")[rightbox]).addClass("b-right");
    rightbox = rightbox + 7;
  }

  if (i > 34) {
    jQuery(jQuery(".all-date ul li")[i]).addClass("b-bottom");
  }

  if (
    jQuery(jQuery(".all-date ul li")[i]).text() == curDate &&
    jQuery(jQuery(".all-date ul li")[i]).css("opacity") == 1
  ) {
    jQuery(jQuery(".all-date ul li")[i]).css({
      "background-color": "#02548b",
      color: "#fff"
    });
  }
}

</script>

At the end we will have something like this. You can change fonts, colors, backgrounds and all things that you want. Enjoy it.

Your Output

how to create a calendar in html and CSS

Video Tutorial

Please watch this video to better understand this tutorial and don’t forget to subscribe to our channel.


Help others to find out about this article on Social Media sites. If you have any doubt or any problem, don’t hesitate to contact us. Thereafter we will be able to help you and also make sure you bookmark our site on your browser.

Related Articles

Leave a Reply

Your email address will not be published.

Back to top button