Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="calendar"></div>
              
            
!

CSS

              
                $day_size: 70;
$main_color: #4f93c8;

@import url(https://fonts.googleapis.com/css?family=Lato:300);

body {
  background: #ccc;
  font-family: Lato, sans-serif;
  font-weight: 300;
}

#calendar {
  width: 7 * $day_size + px;
  margin: 0 auto;
  
  h2 {
    line-height: 170px;
    margin: 0;
    font-weight: 300;
    color: #fff;
    background: $main_color;
    text-align: center;
  }
  
  table {
    width: 100%;
    margin-top: -42px;
    border-collapse: collapse;
  }
  
  .week-days {
    color: #fff;
  }
  
  .day {
    width: $day_size + px;
    height: 40px;
    text-align: center;
  }
  
  .week {
    .day {
      background: #eee;
    }
  }
  
  .other-month {
    opacity: 0.5;
  }
}
              
            
!

JS

              
                function Calendar(month, year) {
  var now = new Date();

  // labels for week days and months
  var days_labels = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
      months_labels = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];

  // test if input date is correct, instead use current month
  this.month = (isNaN(month) || month == null) ? now.getMonth() + 1 : month;
  this.year = (isNaN(year) || year == null) ? now.getFullYear() : year;

  var logical_month = this.month - 1;

  // get first day of month and first week day
  var first_day = new Date(this.year, logical_month, 1),
      first_day_weekday = first_day.getDay() == 0 ? 7 : first_day.getDay();

  // find number of days in month
  var month_length = new Date(this.year, this.month, 0).getDate(),
      previous_month_length = new Date(this.year, logical_month, 0).getDate();

  // calendar header
  var html = '<h2>' + months_labels[logical_month] + ' ' + this.year + '</h2>';

  // calendar content
  html += '<table class="calendar-table">';

  // week days labels row
  html += '<thead>';
  html += '<tr class="week-days">';
  for (var i = 0; i <= 6; i++) {
    html += '<th class="day">';
    html += days_labels[i];
    html += '</th>';
  }
  html += '</tr>';
  html += '</thead>';

  // define default day variables
  var day  = 1, // current month days
      prev = 1, // previous month days
      next = 1; // next month days

  html += '<tbody>';
  html += '<tr class="week">';
  // weeks loop (rows)
  for (var i = 0; i < 9; i++) {
    // weekdays loop (cells)
    for (var j = 1; j <= 7; j++) {
      if (day <= month_length && (i > 0 || j >= first_day_weekday)) {
        // current month
        html += '<td class="day">';
        html += day;
        html += '</td>';
        day++;
      } else {
        if (day <= month_length) {
          // previous month
          html += '<td class="day other-month">';
          html += previous_month_length - first_day_weekday + prev + 1;
          html += '</td>';
          prev++;
        } else {
          // next month
          html += '<td class="day other-month">';
          html += next;
          html += '</td>';
          next++;
        }
      }
    }

    // stop making rows if it's the end of month
    if (day > month_length) {
      html += '</tr>';
      break;
    } else {
      html += '</tr><tr class="week">';
    }
  }
  html += '</tbody>';
  html += '</table>';

  return html;
}

// document.getElementById('calendar').innerHTML = Calendar(12, 2015); 
document.getElementById('calendar').innerHTML = Calendar();
              
            
!
999px

Console