DEV Community

muncey
muncey

Posted on • Updated on

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:

image

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:

https://getbootstrap.com/docs/5.0/getting-started/introduction/

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/bootstrap@5.0.2/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>
Enter fullscreen mode Exit fullscreen mode

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.

https://fonts.google.com/

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>
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
Enter fullscreen mode Exit fullscreen mode

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;
}
Enter fullscreen mode Exit fullscreen mode

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);
}
Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)