top of page
  • Writer's pictureRevanth Reddy Tondapu

Part 15: Layout Styling with Tailwind CSS: A Step-by-Step Guide


Layout Styling with Tailwind CSS: A Step-by-Step Guide
Layout Styling with Tailwind CSS: A Step-by-Step Guide

Now that we have set up Tailwind CSS in our project, let's dive into how to style our layout using Tailwind's utility classes. We'll walk through adding padding, organizing navigation links, and ensuring that the footer stays at the bottom of the page. This guide aims to be easy to follow, especially if you're new to Tailwind CSS.


Adding Padding to the Body

To add padding inside the body, we use Tailwind's utility classes. In React, we achieve this by setting the className prop on the body element.


Horizontal Padding

To add horizontal padding, use the px- class followed by a number. For example:

<body className="px-4">
  ...
</body>

In this case, px-4 adds 1rem (16px by default) of padding to both the left and right sides.


Vertical Padding

Similarly, to add vertical padding, use the py- class:

<body className="px-4 py-2">
  ...
</body>

Here, py-2 adds 0.5rem (8px) of padding to the top and bottom.


Styling the Main Element

Next, let's add some padding to the main element to give it some breathing room.

<main className="grow py-3">
  {children}
</main>

py-3 adds 0.75rem (12px) of padding to the top and bottom, while grow ensures that the main element takes up available space.


Organizing Navigation Links

To display navigation links in a row and add spacing between them, we use Tailwind's flexbox utilities.


Flexbox and Gap

First, set the ul element to use flexbox:

<ul className="flex gap-2">
  <li><Link href="/">Home</Link></li>
  ...
</ul>

The flex class sets the display property to flex, and gap-2 adds 0.5rem (8px) of space between each item.


Styling the Footer

The footer should be centered, smaller in font size, and separated from the rest of the page with a border.


Centering and Font Size

To center the text and make it smaller:

<footer className="border-t py-3 text-center text-xs">
  Game data and images courtesy of ...
</footer>

text-center centers the text, and text-xs makes the font size extra small. border-t adds a border at the top, and py-3 adds padding to the top and bottom.


Footer at the Bottom

To ensure that the footer stays at the bottom of the page, even when the content doesn't fill the screen, we use flexbox on the body.

<body className="flex flex-col px-4 py-2 min-h-screen">
  <header>...</header>
  <main className="grow py-3">{children}</main>
  <footer className="border-t py-3 text-center text-xs">...</footer>
</body>
  • flex and flex-col set the display property to flex and the direction to column.

  • min-h-screen ensures the body takes up the full viewport height.

  • grow makes the main element expand to fill available space, pushing the footer to the bottom.


Using Developer Tools

To inspect and understand the applied styles, you can use Chrome Developer Tools. This tool allows you to see the exact styles and layout properties applied to each element, helping you debug and refine your design.


Conclusion

By using Tailwind CSS, we've successfully styled our layout, including adding padding, organizing navigation links, and ensuring the footer stays at the bottom of the page. Tailwind's utility classes make it straightforward to apply consistent styling across your application. For more detailed instructions and examples, always refer to the official Tailwind CSS documentation.

Happy styling!

0 views0 comments

Comments


bottom of page