28
Creating an online budget tool 5/5
In this article I am going to add styles to my form so that it looks visually appealing with final version looking like this:
There is still a lot of work to be performed to complete the styling phase but I want to demonstrate the techniques that I used first.
The page is styled using bootstrap and is based of the bootstrap starter form:
To style the form I have inserted a link to bootstrap styles into the page header
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<title>My Budget</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Noto+Serif&display=swap" rel="stylesheet">
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
Note that I have also brought in a google font. These are customised fonts that you can use to make your site look a lot better.
I have added a navbar to the top of the page:
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container-fluid">
<a class="navbar-brand" href="#">My Budget</a>
</div>
</nav>
And I have enclosed the budget table in a container. A container is used to centre a group of elements on the page. If you want to have full width for a group of elements you use container-fluid.
<div class="container bg-white mt-3 rounded">
<div class="my-budget">
<table id="budgetTable" class="table">
<thead>
<tr>
<th>Item</th>
<th>Amount</th>
<th>Action</th>
</tr>
<tr>
<td><input placeholder="Enter item" type="text" autocomplete="off" id="newItem" class="form-control"></td>
<td><input placeholder="Enter amount" type="number" autocomplete="off" id="newAmount" class="form-control"></td>
<td><button id="addButton" type="button" class="btn btn-link">Add</button></td>
</tr>
</thead>
<tbody>
</tbody>
<tfoot>
</tfoot>
</table>
</div>
</div>
I have also moved the new item form into the table so that it appears as the first row and styled the buttons using the btn btn-link class. Note as well that the container is making use of bg-white mt-3 and rounded classes which let you style an element without needing to write your own custom css.
In order to use bootstrap properly you need to include some javascript which I have placed at the bottom of the page.
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
Finally I have added a small bit of css into the styles.css file to control the page fonts and backgrounds.
body {
font-family: 'Noto Serif', serif;
background: #D3D3D3;
}
And I fixed a bug where the disabled attribute was not being cleared when the user clicked cancel.
const cancelEdit = () => {
id = 'budgetTable';
document.getElementById('newItem').disabled = false;
document.getElementById('newAmount').disabled = false;
document.getElementById('addButton').disabled = false;
document.getElementById(id).tBodies[0].innerHTML = renderRows(budgetItems);
}
What I have shown is how easy it is to add bootstrap to a form and then to style that form so that it looks nice. The code for this iteration can be found here:
https://github.com/muncey/MyBudgetFrontEnd/tree/style-form
28