Published on

Make A Rich Harris With Tailwind CSS Like A Pro With The Help Of These 6 Tips

Rich Harris

What is Tailwind CSS?

Tailwind CSS is a utility-first CSS framework that focuses on creating personalized user interfaces quickly. It can gives you all the building blocks you are able to create personalized designs without having to fight to override irritating opinionated styles. Also, Tailwind CSS is a highly configurable, low-level CSS framework.

The description of Rich Harris ui component

Resizable columns for table

Why use Tailwind CSS to create a Rich Harris ui component?

  • It can make the building process of Rich Harris ui component faster and more easily.
  • Enables building complex responsive layouts and components freely.
  • Minimum lines of CSS code in Rich Harris component file.

The preview of Rich Harris ui component

Free download of the Rich Harris's source code

The source code of Rich Harris ui component

<div id="app">
  <div class="h-screen w-full bg-gray-50">
    <h1 class='text-2xl font-bold text-center'>Resizable Columns for Tailwind Table</h1>
    <h2 class='text-lg text-center'>Place cursor at beginning of 2nd or 3rd column and blue resizer will appear
</h2>
    <div class="flex w-full justify-center">
      <table class="table-fixed border border-gray-500 border-collapse w-4/5 mx-24 mt-14">
        <thead class="">
          <tr class="border-b-2 border-black">
            <th class="w-4/12 bg-gray-300 text-left px-2">Decedent Name</th>
            <th class="w-4/12 bg-gray-300 text-left px-2">Date of Death</th>
            <th class="w-4/12 bg-gray-300 text-left px-2">Age</th>
          </tr>
        </thead>
        <tbody class="divide-y divide-gray-400">
          <tr>
            <td class="text-left px-2">Jonathan Doe</td>
            <td class="px-2">09/01/1921</td>
            <td class="px-2">56</td>
          </tr>
          <tr>
            <td class="px-2">Sally Smith</td>
            <td class="px-2">09/04/1922</td>
            <td class="px-2">22</td>
          </tr>
          <tr>
            <td class="px-2">Walter Wilson</td>
            <td class="px-2">08/01/1926</td>
            <td class="px-2">70</td>
          </tr>
        </tbody>
      </table>
    </div>
  </div>
</div>

<script>

    let tables = document.getElementsByTagName("table");
      for (var i = 0; i < tables.length; i++) {
        resizableGrid(tables[i]);
      }
      function resizableGrid(table) {
        var row = table.getElementsByTagName("tr")[0],
          cols = row ? row.children : undefined;
        if (!cols) return;

        table.style.overflow = "hidden";

        var tableHeight = table.offsetHeight;

        for (var i = 0; i < cols.length; i++) {
          var div = createDiv(tableHeight);
          cols[i].appendChild(div);
          cols[i].style.position = "relative";
          setListeners(div);
        }

        function setListeners(div) {
          var pageX, curCol, nxtCol, curColWidth, nxtColWidth;

          div.addEventListener("mousedown", function (e) {
            curCol = e.target.parentElement;
            nxtCol = curCol.nextElementSibling;
            pageX = e.pageX;

            var padding = paddingDiff(curCol);

            curColWidth = curCol.offsetWidth - padding;
            if (nxtCol) nxtColWidth = nxtCol.offsetWidth - padding;
          });

          div.addEventListener("mouseover", function (e) {
            e.target.style.borderRight = "2px solid #0000ff";
          });

          div.addEventListener("mouseout", function (e) {
            e.target.style.borderRight = "";
          });

          document.addEventListener("mousemove", function (e) {
            if (curCol) {
              var diffX = e.pageX - pageX;

              if (nxtCol) nxtCol.style.width = nxtColWidth - diffX + "px";

              curCol.style.width = curColWidth + diffX + "px";
            }
          });

          document.addEventListener("mouseup", function (e) {
            curCol = undefined;
            nxtCol = undefined;
            pageX = undefined;
            nxtColWidth = undefined;
            curColWidth = undefined;
          });
        }

        function createDiv(height) {
          var div = document.createElement("div");
          div.style.top = 0;
          div.style.right = 0;
          div.style.width = "5px";
          div.style.position = "absolute";
          div.style.cursor = "col-resize";
          div.style.userSelect = "none";
          div.style.height = height + "px";
          return div;
        }

        function paddingDiff(col) {
          if (getStyleVal(col, "box-sizing") == "border-box") {
            return 0;
          }

          var padLeft = getStyleVal(col, "padding-left");
          var padRight = getStyleVal(col, "padding-right");
          return parseInt(padLeft) + parseInt(padRight);
        }

        function getStyleVal(elm, css) {
          return window.getComputedStyle(elm, null).getPropertyValue(css);
        }
      }
</script>

How to create a Rich Harris with Tailwind CSS?

Install tailwind css of verion 2.2.4

Use the script html tag to import the script of Tailwind CSS of the version 2.2.4

<script src="https://cdn.tailwindcss.com"></script>

All the unility class needed to create a Rich Harris component

  • h-screen
  • w-full
  • bg-gray-50
  • flex
  • table-fixed
  • border-gray-500
  • border-collapse
  • w-4/5
  • mx-24
  • mt-14
  • border-b-2
  • border-black
  • w-4/12
  • bg-gray-300
  • text-left
  • px-2

16 steps to create a Rich Harris component with Tailwind CSS

  1. Use h-screen to make an element span the entire height of the viewport.

  2. Use w-full to set an element to a 100% based width.

  3. Control the background color of an element to gray-50 using the bg-gray-50 utilities.

  4. Use flex to create a block-level flex container.

  5. Use the table utilities to create elements that behave like their respective table elements.

  6. Control the border color of an element to gray-500 using the border-gray-500 utilities.

  7. Control the border color of an element to collapse using the border-collapse utilities.

  8. Use w-4/5 to set an element to a fixed width(4/5).

  9. Control the horizontal margin of an element to 6rem using the mx-24 utilities.

  10. Control the margin on top side of an element to 3.5rem using the mt-14 utilities.

  11. Control the border color of an element to b-2 using the border-b-2 utilities.

  12. Control the border color of an element to black using the border-black utilities.

  13. Use w-4/12 to set an element to a fixed width(4/12).

  14. Control the background color of an element to gray-300 using the bg-gray-300 utilities.

  15. Control the text color of an element to left using the text-left utilities.

  16. Control the horizontal padding of an element to 0.5rem using the px-2 utilities.

Conclusion

The above is a step-by-step tutorial on how to use Tailwind CSS to create a Rich Harris components, learn and follow along to implement your own components.