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 class="calc-container">
    <noscript>
        <div class="calc-no_scripts--container">
            <span class="calc-no_scripts--label">This mortgage calculator requires Javascript to function correctly.</span>
        </div>
    </noscript>
    <div class="calc-column">
        <div class="calc-field">
            <label for="calc-house_price">House price</label>
            <input type="text" id="calc-house_price" class="calc-input" placeholder="&pound;130,000" />
        </div>
        <div class="calc-field">
            <label for="calc-repayment_term">Repayment Term</label>
            <select id="calc-repayment_term" class="calc-dropdown">
                <option value="5">5 years</option>
                <option value="6">6 years</option>
                <option value="7">7 years</option>
                <option value="8">8 years</option>
                <option value="9">9 years</option>
                <option value="10">10 years</option>
                <option value="11">11 years</option>
                <option value="12">12 years</option>
                <option value="13">13 years</option>
                <option value="14">14 years</option>
                <option value="15">15 years</option>
                <option value="16">16 years</option>
                <option value="17">17 years</option>
                <option value="18">18 years</option>
                <option value="19">19 years</option>
                <option value="20">20 years</option>
                <option value="21">21 years</option>
                <option value="22">22 years</option>
                <option value="23">23 years</option>
                <option value="24">24 years</option>
                <option value="25" selected>25 years</option>
                <option value="26">26 years</option>
                <option value="27">27 years</option>
                <option value="28">28 years</option>
                <option value="29">29 years</option>
                <option value="30">30 years</option>
                <option value="31">31 years</option>
                <option value="32">32 years</option>
                <option value="33">33 years</option>
                <option value="34">34 years</option>
                <option value="35">35 years</option>
            </select>
        </div>
    </div>
    <div class="calc-column">
        <div class="calc-field">
            <label for="calc-deposit">Deposit (<span id="calc-deposit_percent"></span>&percnt;)</label>
            <input type="range" id="calc-deposit" class="calc-slider" min="1" max="99" value="10" />
            <span class="calc-deposit_amount" id="calc-deposit_amount"></span>
        </div>
        <div class="calc-field">
            <div class="calc-label_container">
                <label for="calc-interest_rate">Interest Rate</label>
                <span class="calc-interest_rate--percent">&percnt;</span>
            </div>
            <input type="text" id="calc-interest_rate" class="calc-input calc-percentage_input" placeholder="1.5" />
        </div>
    </div>
    <div class="calc-column">
        <div class="calc-result_container">
            <span class="calc-result_label">Your mortgage would cost:</span>
            <span class="calc-result">&pound;<span class="calc-result" id="calc-result_value"></span></span>
            <span class="calc-result_per--month">per month</span>
        </div>
    </div>
</div>
              
            
!

CSS

              
                .calc-container {
    display: flex;
    flex-flow: row;
    position: relative;
}

.calc-column {
    display: flex;
    flex-flow: column;
    flex: 1 1 auto;
    margin-right: 40px;
}

.calc-column:last-child {
    margin-right: 0px;
    justify-content: center;
}

.calc-field {
    display: flex;
    flex-flow: column;
    height: 82px;
    position: relative;
}

label {
    margin-bottom: 10px;
    font-weight: bold;
}

input {
    font-family: 'Open Sans', sans-serif;
}

.calc-field:first-child {
    margin-bottom: 10px;
}

.calc-input, .calc-dropdown {
    height: 50px;
    padding: 8px 12px;
}

.calc-label_container {
    position: relative;
    margin-bottom: 10px;
}

.calc-interest_rate--percent {
    position: absolute;
    right: 5px;
    top: 45px;
}

.calc-result {
    font-size: 52px;
    line-height: 55px;
    font-weight: 700;
    color: #2f3437;
}

.calc-result_container {
    display: flex;
    flex-flow: column;
    text-align: center;
    padding: 25px;
    background-color: rgba(59, 211, 248, .2);
    flex: 1 1 auto;
    justify-content: center;
}

noscript {
    position: absolute;
    height: 100%;
    width: 100%;
    z-index: 1000;
}

.calc-no_scripts--container {
    height: 100%;
    background-color: rgba(0, 0, 0, .3);
    display: flex;
    justify-content: center;
    align-items: center;
}

.calc-no_scripts--label {
    background-color: white;
    padding: 15px;
}

.calc-deposit_amount {
    position: absolute;
    bottom: 0;
}

input[type=range] {
  -webkit-appearance: none;
  width: 100%;
  margin: 7px 0;
}
input[type=range]:focus {
  outline: none;
}
input[type=range]::-webkit-slider-runnable-track {
  width: 100%;
  height: 8px;
  cursor: pointer;
  box-shadow: 0px 0px 0px #000000, 0px 0px 0px #0d0d0d;
  background: rgba(213, 213, 213, 0.78);
  border-radius: 25px;
  border: 0px solid rgba(1, 1, 1, 0);
}
input[type=range]::-webkit-slider-thumb {
  box-shadow: 0px 0px 0px #000031, 0px 0px 0px #00004b;
  border: 0px solid rgba(0, 0, 30, 0);
  height: 22px;
  width: 22px;
  border-radius: 22px;
  background: #3bb3f8;
  cursor: pointer;
  -webkit-appearance: none;
  margin-top: -7px;
}
input[type=range]:focus::-webkit-slider-runnable-track {
  background: rgba(216, 216, 216, 0.78);
}
input[type=range]::-moz-range-track {
  width: 100%;
  height: 8px;
  cursor: pointer;
  box-shadow: 0px 0px 0px #000000, 0px 0px 0px #0d0d0d;
  background: rgba(213, 213, 213, 0.78);
  border-radius: 25px;
  border: 0px solid rgba(1, 1, 1, 0);
}
input[type=range]::-moz-range-thumb {
  box-shadow: 0px 0px 0px #000031, 0px 0px 0px #00004b;
  border: 0px solid rgba(0, 0, 30, 0);
  height: 22px;
  width: 22px;
  border-radius: 22px;
  background: #3bb3f8;
  cursor: pointer;
}
input[type=range]::-ms-track {
  width: 100%;
  height: 8px;
  cursor: pointer;
  background: transparent;
  border-color: transparent;
  color: transparent;
}
input[type=range]::-ms-fill-lower {
  background: rgba(210, 210, 210, 0.78);
  border: 0px solid rgba(1, 1, 1, 0);
  border-radius: 50px;
  box-shadow: 0px 0px 0px #000000, 0px 0px 0px #0d0d0d;
}
input[type=range]::-ms-fill-upper {
  background: rgba(213, 213, 213, 0.78);
  border: 0px solid rgba(1, 1, 1, 0);
  border-radius: 50px;
  box-shadow: 0px 0px 0px #000000, 0px 0px 0px #0d0d0d;
}
input[type=range]::-ms-thumb {
  box-shadow: 0px 0px 0px #000031, 0px 0px 0px #00004b;
  border: 0px solid rgba(0, 0, 30, 0);
  height: 22px;
  width: 22px;
  border-radius: 22px;
  background: #3bb3f8;
  cursor: pointer;
  height: 8px;
}
input[type=range]:focus::-ms-fill-lower {
  background: rgba(213, 213, 213, 0.78);
}
input[type=range]:focus::-ms-fill-upper {
  background: rgba(216, 216, 216, 0.78);
}

              
            
!

JS

              
                /**
 * Mortgage Calculator
 * Written by Christopher Cox (That Software Guy)
 * Copyright © 2018 Christopher Cox
 * 
 * https://thatsoftwareguy.co.uk
 */
var price = 130000;
var term = 25;
var depositPercent = 10;
var interestRate = 1.5;

function updateDepositPercent() {
    document.getElementById('calc-deposit_percent').innerText = depositPercent;
}

function updateDepositAmount() {
    document.getElementById('calc-deposit_amount').innerHTML = "&pound;" + formatCurrency(Math.round(calculateDepositAmount()));
}

function updateCalculationResult() {
    document.getElementById('calc-result_value').innerText = formatCurrency(Math.round(calculateMonthlyPayments()));
}

function calculateDepositAmount() {
    return (price * (depositPercent/100));
}

function calculateMonthlyPayments() {
    var principal = price - calculateDepositAmount();
    var months = term * 12;

    if (interestRate == 0) {
        return (principal / months);
    }

    var interest = interestRate / 1200;

    return (principal * interest / (1 - (Math.pow(1/(1 + interest), months))));
}

function formatCurrency(amount) {
    return amount.toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,').replace('.00', '');
}

function isNullOrWhitespace(input) {
    if (typeof(input) === 'undefined' || input == null) return true;

    return input.replace(/\s/g, '').length < 1;
}

function housePriceInput() {
    var value = document.getElementById("calc-house_price").value.replace(/\D/g, '');

    if (isNullOrWhitespace(value) || isNaN(value)) {
        document.getElementById('calc-house_price').value = '';

        price = 130000;
    } else {
        if (value > 10000000) value = 10000000;

        document.getElementById('calc-house_price').value = '\u00A3' + formatCurrency(parseInt(value));
        price = value;
    }

    updateCalculationResult();
    updateDepositAmount();
    resetDepositAmountPosition();
}

function interestRateInput() {
    var value = document.getElementById("calc-interest_rate").value.replace(/[^0-9.]/g, '');

    if (value === "." || isNullOrWhitespace(value) || isNaN(value)) {
        document.getElementById('calc-interest_rate').value = '';

        interestRate = 1.5;
    } else {
        var split = value.split('.');
        var newValue = 1.5;

        var majorValue = parseInt(split[0]);
        if (majorValue > 99) majorValue = 99;
        newValue = majorValue;

        var minorValue = NaN;
        if (split.length > 1) {
            minorValue = parseInt(split[1]);
        }

        if (minorValue > 99) {
            minorValue = 99;
        }

        if (minorValue > 0) {
            if (split[1].length === 1) {
                minorValue = minorValue / 10;
            } else {
                minorValue = minorValue / 100;
            }

            newValue += minorValue;
        }

        var textValue = newValue;

        if (value.indexOf('.') > -1 && new String(newValue).indexOf('.') < 0) textValue += ".";
        if (minorValue === 0) textValue += "0";

        document.getElementById('calc-interest_rate').value = textValue;
        interestRate = newValue;
    }

    updateCalculationResult();
}

window.onload = function() {
    updateDepositPercent();
    updateDepositAmount();
    updateCalculationResult();
    resetDepositAmountPosition();

    document.getElementById('calc-house_price').value = '';
    document.getElementById('calc-repayment_term').value = '25';
    document.getElementById('calc-interest_rate').value = '';
    document.getElementById('calc-deposit').value = '10';

    document.getElementById('calc-repayment_term').addEventListener('change', function() {
        term = document.getElementById('calc-repayment_term').value;
        updateCalculationResult();
    });

    document.getElementById('calc-house_price').addEventListener('input', housePriceInput);

    document.getElementById('calc-interest_rate').addEventListener('input', interestRateInput);

    var changeDeposit = function() {
        depositPercent = document.getElementById('calc-deposit').value;

        updateDepositPercent();
        updateDepositAmount();
        updateCalculationResult();

        resetDepositAmountPosition();
    }

    document.getElementById('calc-deposit').addEventListener('change', changeDeposit);
    document.getElementById('calc-deposit').addEventListener('input', changeDeposit);
}

function resetDepositAmountPosition() {
    if (depositPercent <= 50) {
        document.getElementById('calc-deposit_amount').setAttribute('style', 'left:calc(' + depositPercent + '% - calc(' + document.getElementById('calc-deposit_amount').clientWidth + 'px/2));');
            
        var left = parseInt(window.getComputedStyle(document.getElementById('calc-deposit_amount')).getPropertyValue('left').replace('px', ''));
        if (left < 0) {
            document.getElementById('calc-deposit_amount').setAttribute('style', 'left:0px');
        }
    } else {
        document.getElementById('calc-deposit_amount').setAttribute('style', 'right:calc(' + Math.abs(depositPercent - 100) + '% - calc(' + document.getElementById('calc-deposit_amount').clientWidth + 'px/2));');
            
        var right = parseInt(window.getComputedStyle(document.getElementById('calc-deposit_amount')).getPropertyValue('right').replace('px', ''));
        if (right < 0) {
            document.getElementById('calc-deposit_amount').setAttribute('style', 'right:0px');
        }
    }
}
              
            
!
999px

Console