Photo by Chris Linnett on Unsplash
Tailwind Tutorial: Laying it Out with Flex and Grid
A comprehensive guide to using Flex and Grid with Tailwind.
Here are ten exercises you can try with Tailwind CSS layout using Flexbox and ddgrid. I have all the problems listed here before you reach the solution for you to try them out before reading my solutions! Good luck and comment successes below.
Exercises
Create a simple two-column layout using flexbox, with a sidebar on the left and main content on the right.
Build a responsive three-column layout using grid, with equal-width columns on desktop and mobile views, but stacked on smaller screens.
Use flexbox to create a horizontal navigation menu with evenly spaced items, and add a dropdown menu for smaller screens.
Build a vertical timeline using flexbox, with each event in a separate box and connecting lines between them.
Create a responsive grid of cards using grid, with each card showing an image and some text.
Build a footer with links and social icons using flexbox, with the links aligned to the left and the icons to the right.
Use grid to create a complex layout with a header, two sidebars, and a main content area.
Create a responsive grid of product listings using grid, with each listing showing an image, title, and price.
Build a sticky navbar using flexbox, which stays fixed to the top of the page as the user scrolls down.
Use grid to create a responsive gallery layout with images of varying sizes, which aligns them in a way that minimizes whitespace.
Solutions
Here are the code examples for each of the 10 exercises I suggested earlier, using Tailwind CSS and either flexbox or grid.
Here are the Tailwind classes and their equivalent CSS styles for each code example:
Exercise 1: Simple two-column layout using flexbox
<div class="flex">
<div class="w-1/4 bg-gray-200">
<!-- Sidebar content -->
</div>
<div class="w-3/4 bg-gray-100">
<!-- Main content -->
</div>
</div>
Tailwind classes used:
flex
: sets the display property of the parent element to flexw-1/4
: sets the width of the sidebar to 25% of the parent element's widthbg-gray-200
: sets the background color of the sidebar to gray-200w-3/4
: sets the width of the main content to 75% of the parent element's widthbg-gray-100
: sets the background color of the main content to gray-100
Equivalent CSS styles:
.flex {
display: flex;
}
.w-1/4 {
width: 25%;
}
.bg-gray-200 {
background-color: #EDF2F7;
}
.w-3/4 {
width: 75%;
}
.bg-gray-100 {
background-color: #F7FAFC;
}
Exercise 2: Responsive three-column layout using grid
<div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3">
<div class="bg-gray-100">
<!-- Column 1 content -->
</div>
<div class="bg-gray-200">
<!-- Column 2 content -->
</div>
<div class="bg-gray-300">
<!-- Column 3 content -->
</div>
</div>
Tailwind classes used:
grid
: sets the display property of the parent element to gridgrid-cols-1
: sets the parent element to have one column by defaultsm:grid-cols-2
: sets the parent element to have two columns on small screens and uplg:grid-cols-3
: sets the parent element to have three columns on large screens and upbg-gray-100
: sets the background color of the first child element to gray-100bg-gray-200
: sets the background color of the second child element to gray-200bg-gray-300
: sets the background color of the third child element to gray-300
Equivalent CSS styles:
.grid {
display: grid;
}
.grid-cols-1 {
grid-template-columns: repeat(1, minmax(0, 1fr));
}
.sm\:grid-cols-2 {
grid-template-columns: repeat(2, minmax(0, 1fr));
}
.lg\:grid-cols-3 {
grid-template-columns: repeat(3, minmax(0, 1fr));
}
.bg-gray-100 {
background-color: #F7FAFC;
}
.bg-gray-200 {
background-color: #EDF2F7;
}
.bg-gray-300 {
background-color: #E2E8F0;
}
Exercise 3: Horizontal navigation menu with dropdown using flexbox
<nav class="flex justify-between items-center">
<ul class="flex">
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</ul>
<ul class="hidden sm:flex">
<li><a href="#">Dropdown</a></li>
<li><a href="#">Menu</a></li>
<li><a href="#">Items</a></li>
</ul>
</nav>
In this example, we're using flexbox to create a horizontal navigation menu with a dropdown menu that's visible only on small screens and up.
The
flex
class on the parentnav
sets its display property toflex
.The
justify-between
class on the parentnav
sets the horizontal alignment of its children to be distributed evenly with space between.The
items-center
class on the parentnav
sets the vertical alignment of its children to be centered.The
flex
class on the first childul
sets its display property toflex
.The
hidden
andsm:flex
classes on the second childul
sets it to be hidden on small screens and visible on larger screens.The
li
anda
elements create the menu items and dropdown items respectively.
Tailwind classes used:
flex
: sets the display property of the element to flexjustify-between
: aligns the children elements horizontally and distributes the space evenly with space betweenitems-center
: aligns the children elements vertically and centers themhidden
: sets the element to be hiddensm:flex
: sets the element to be visible on small screens and up
Equivalent CSS styles:
nav {
display: flex;
justify-content: space-between;
align-items: center;
}
nav ul:first-child {
display: flex;
}
nav ul:last-child {
display: none;
}
@media (min-width: 640px) {
nav ul:last-child {
display: flex;
}
}
Exercise 4: Vertical timeline using flexbox
<div class="flex flex-col">
<div class="flex items-center">
<div class="h-1 bg-gray-500 flex-1"></div>
<div class="w-6 h-6 bg-gray-500 rounded-full"></div>
<div class="h-1 bg-gray-500 flex-1"></div>
</div>
<div class="flex items-center mt-8">
<div class="h-1 bg-gray-500 flex-1"></div>
<div class="w-6 h-6 bg-gray-500 rounded-full"></div>
<div class="h-1 bg-gray-500 flex-1"></div>
</div>
<!-- More events here -->
</div>
In this example, we're using flexbox to create a vertical timeline with dots and horizontal lines.
The
flex
class on the parentdiv
sets its display property toflex
.The
flex-col
class on the parentdiv
sets its flex direction to column.The
flex
class on the childdivs
sets their display property toflex
.The
items-center
class on the childdivs
sets their vertical alignment to be centered.The `h
flex
: sets the display property of the element to flexflex-col
: sets the flex-direction property of the element to columnitems-center
: aligns the children elements vertically and centers themh-1
: sets the height of the element to 1 remw-6
: sets the width of the element to 1.5 rembg-gray-500
: sets the background color of the element to gray-500flex-1
: sets the flex-grow, flex-shrink, and flex-basis properties of the element to 1
Equivalent CSS styles:
div {
display: flex;
flex-direction: column;
}
div > div {
display: flex;
align-items: center;
}
div > div > div:first-child, div > div > div:last-child {
flex-grow: 1;
flex-shrink: 1;
flex-basis: 0;
height: 1rem;
background-color: #718096;
}
div > div > div:nth-child(2) {
width: 1.5rem;
height: 1.5rem;
background-color: #718096;
border-radius: 50%;
}
Exercise 5: Vertical centering of a single element using flexbox
<div class="flex items-center justify-center h-screen">
<h1 class="text-3xl font-bold text-gray-800">Hello, world!</h1>
</div>
In this example, we're using flexbox to center a single element vertically and horizontally in the viewport.
The
flex
class on the parentdiv
sets its display property toflex
.The
items-center
class on the parentdiv
sets the vertical alignment of its children to be centered.The
justify-center
class on the parentdiv
sets the horizontal alignment of its children to be centered.The
h-screen
class on the parentdiv
sets its height to the full viewport height.
Tailwind classes used:
flex
: sets the display property of the element to flexitems-center
: aligns the children elements vertically and centers themjustify-center
: aligns the children elements horizontally and centers themh-screen
: sets the height of the element to the full viewport height
Equivalent CSS styles:
div {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
}
h1 {
font-size: 3rem;
font-weight: bold;
color: #2D3748;
}
Exercise 6: Footer with links and social icons using flexbox
<footer class="flex justify-between items-center">
<ul class="flex">
<li><a href="#">Link 1</a></li>
<li><a href="#">Link 2</a></li>
<li><a href="#">Link 3</a></li>
</ul>
<div class="flex">
<a href="#" class="mr-4">
<i class="fab fa-facebook"></i>
</a>
<a href="#">
<i class="fab fa-twitter"></i>
</a>
</div>
</footer>
In this example, we're using flexbox to create a footer with two sections: a list of links on the left and social icons on the right.
Tailwind classes used:
The
flex
class on the parentfooter
(and anywhere) sets its display property toflex
The
justify-between
class on the parentfooter
sets the horizontal alignment of its children to be distributed evenly with space between.The
items-center
class on the parentfooter
sets the vertical alignment of its children to be centered.The
mr-4
class on the first childa
sets its margin-right to 1rem
Equivalent CSS styles:
footer {
display: flex;
justify-content: space-between;
align-items: center;
}
.flex {
display: flex;
}
li {
margin-right: 1rem;
}
a {
color: #4A5568;
}
i {
font-size: 1.5rem;
}
.fab.fa-facebook {
color: #4267B2;
}
.fab.fa-twitter {
color: #1DA1F2;
}
Exercise 7: Image grid with captions using grid
<div class="grid grid-cols-2 md:grid-cols-3 gap-4">
<div class="relative">
<img src="https://via.placeholder.com/300x300" alt="Image 1">
<p class="absolute bottom-0 left-0 right-0 p-4 bg-gray-800 text-white">Caption 1</p>
</div>
<div class="relative">
<img src="https://via.placeholder.com/300x300" alt="Image 2">
<p class="absolute bottom-0 left-0 right-0 p-4 bg-gray-800 text-white">Caption 2</p>
</div>
<div class="relative hidden md:block">
<img src="https://via.placeholder.com/300x300" alt="Image 3">
<p class="absolute bottom-0 left-0 right-0 p-4 bg-gray-800 text-white">Caption 3</p>
</div>
<!-- More images and captions here -->
</div>
In this example, we're using grid to create an image grid with captions. On smaller screens, we show two images per row and stack them on top of each other on larger screens.
Tailwind classes used:
grid
: sets the display property of the element to gridgrid-cols-2
: sets the number of columns in the grid to 2md:grid-cols-3
: sets the number of columns in the grid to 3 on medium screens and upgap-4
: sets the gap between grid cells to be 1remrelative
: sets the position property of the element to relativeabsolute
: sets the position property of the element to absolutebottom-0
: sets the bottom property of the element to 0left-0
: sets the left property of the element to 0right-0
: sets the right property of the element to 0p-4
: sets the padding of the element to 1rem
Equivalent CSS styles:
div {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
grid-gap: 1rem;
}
@media (min-width: 768px) {
div {
grid-template-columns: repeat(3, minmax(0, 1fr));
}
}
div > div {
position: relative;
}
div > div > img {
display: block;
max-width: 100%;
height: auto;
}
div > div > p {
position: absolute;
bottom: 0;
left: 0;
right: 0;
padding: 1rem;
background-color: #2D3748;
color: #FFF;
}
Exercise 8: Sticky sidebar using grid
code<div class="grid grid-cols-12 gap-4">
<main class="col-span-8">
<h1 class="text-3xl font-bold mb-4">Main content</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut rutrum, felis vel bibendum faucibus, turpis velit facilisis odio, sit amet efficitur quam nisi sit amet nunc. </p>
</main>
<aside class="col-span-4 sticky top-4">
<h2 class="text-lg font-bold mb-4">Sidebar</h2>
<ul>
<li><a href="#">Link 1</a></li>
<li><a href="#">Link 2</a></li>
<li><a href="#">Link 3</a></li>
</ul>
</aside>
</div>
In this example, we're using grid to create a two-column layout with a sticky sidebar on the right.
Tailwind classes used:
grid
: sets the display property of the element to gridgrid-cols-12
: sets the number of columns in the grid to 12gap-4
: sets the gap between grid cells to be 1remcol-span-8
: sets the element to span 8 columnscol-span-4
: sets the element to span 4 columnssticky
: sets the position of the element to stickytop-4
: sets the top property of the element to 1rem
Equivalent CSS styles:
.grid {
display: grid;
}
.grid-cols-12{
grid-template-columns: repeat(12, minmax(0, 1fr));
}
.gap-4{
grid-gap: 1rem;
}
.col-span-8 {
grid-column: span 8;
}
.col-span-4 {
grid-column: span 4;
}
.sticky {
position: sticky;
}
.top-4 {
top: 1rem;
}
ul {
margin: 0;
padding: 0;
list-style: none;
}
a {
color: #4A5568;
}
h1 {
font-size: 3rem;
font-weight: bold;
margin-bottom: 1rem;
}
h2 {
font-size: 1.5rem;
font-weight: bold;
margin-bottom: 1rem;
}
These are just some examples of what you can do with Tailwind CSS and flexbox or grid. Feel free to experiment and come up with your own layouts!
I hope this helps you on your journey!