Developers use Cascading Style Sheets (CSS) to style websites. But often, when building large websites or apps, it becomes tedious to write these rules from scratch. This is why there are multiple CSS frameworks to help make writing CSS easy, such as Bootstrap, Foundation, Bulma, Pure, Materialize, etc.
Tailwind CSS is a framework that is somewhat different from the ones previously mentioned, because it doesn’t have a default theme, and there are no built-in UI components. Tailwind is a utility-first CSS framework for rapidly building custom user interfaces. This means that if you’re looking for a framework with a menu of predesigned widgets to build your site with, Tailwind might not be the right framework for you. Instead, Tailwind provides highly composable, low-level utility classes that make it easier to build complex user interfaces without encouraging any two sites to look the same.
In this tutorial, you’ll build a landing page to showcase a smart health monitoring wristwatch (SHMW) product to customers.
The final product will look like the following:
The landing page will be divided into the following:
You can download the assets for this project at this GitHub page.
A basic understanding of CSS may be helpful, but is not required.
We’ll start by creating a new project directory, which we’ll call shmw
and create an index.html
file inside it.
To get up and running quickly with Tailwind CSS, we’ll grab the latest default configuration build via CDN (Content Delivery Network). Add the following snippet to index.html
:
In this snippet, you gave the body a white background and pulled the Source Sans Pro font from Google Fonts.
Note: Many of the features of Tailwind CSS are not available using the CDN builds. To take full advantage of Tailwind CSS features, install Tailwind via npm.
The navbar will be divided into two columns. The first column will hold the logo and the second column will hold the navigation links. Add the following code immediately after <body>
in the index.html
file:
Adding .container
sets the max-width
of an element to match the min-width
of the current breakpoint. To make the container centered, you add .mx-auto
and .px-6
to have padding on both sides (left and right). Since we want a horizontal navbar, we set the container display to flex
and specify how its items should be displayed. Each item should have an equal amount of space between them (using .justify-between
), which will push both columns to the edge. They will be vertically centered (using .items-center
). Lastly, we add padding to both the top and bottom of the container using .py-2
.
The first column holds our business logo (in this case, just text) on the navbar. For the second column, we want the links to be displayed differently on mobile and desktop. We have a div
containing a button for our mobile menu, which will only be visible on small screen devices. To achieve this, we add both .block
and .lg:hidden
, which will make the button visible on mobile devices and hidden on large screens.
Note: By default, Tailwind CSS takes a mobile-first approach, so we build it from a small screen to a larger screen.
Next, for the desktop links, we add .hidden
and .lg:block
, which we do the direct inverse of the above. For the actual links, we add .inline-flex
to make the links appear horizontal. For individual links, we give them padding on both sides. To indicate the active link (in our case, the home link), we make the text bold. For the rest of the links, we use a darker shade of gray once the links are hovered over.
The Hero section will display information about our smart health monitoring wristwatch and a call to action button for the users to take immediate action. Add this code snippet immediately after the navbar:
We start by adding padding to both the top and bottom, and then we set a background gradient. For the call to action button, we give it a white background color, make the text bold, give it some padding, and make it pill-shaped by giving it fully rounded borders. Lastly, we give it some shadow and make the text uppercase.
Now that you’ve made the hero section, you’re ready to build the features section.
In this step, you will build a section that will list out the notable features of the device.
Add the following immediately after the hero section:
The features themselves are displayed in a grid of two columns: the feature text and the accompanying image. On mobile devices, we stack on top of one another. We use flexbox to build our grid.
In this step, you will build the testimonial section that will contain cards of some of the testimonies. The card will contain the user’s testimony and the user’s name.
Add the following immediately after the features section:
First, we give the section a background and center it on the page. For the actual testimonies, we make them appear in a grid with flexbox. We want them to stack (that is, take the full width of the screen) on one another when viewed on mobile devices, hence .w-full
. Then, on larger screens, we want them to be displayed in three columns using .md:w-1/3
. For the individual cards, we give white background, rounded borders, and shadow.
The call to action section is needed so our users can take immediate action after reading the features of our product and the testimonials from the demo users. Add the following immediately after the testimonials section:
Now that you’ve created the call to action, you’re ready to build the footer.
The footer will contain extra links like a blog, privacy policy, social media, etc. Add the following immediately after the call to action section:
This code displays a bunch of links in a grid of four columns. Each column will stack on one another, and the text will be centered when viewed on small screens.
With that, you have completed your landing page. The completed index.html
file will look as follows:
In this tutorial, you built a landing page with Tailwind CSS. In addition to using the classes that Tailwind provides, you also used gradient colors to make the landing page have more depth. To learn more about Tailwind CSS, you can read the documentation on their official website.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
In order to make this properly work on mobile, you need to add the following meta tag to the header section:
<meta name=“viewport” content=“width=device-width, initial-scale=1”>
According to the screenshots shown in this post, I believe using
items-center
in the Feature section is not correct. From the picture in Step 4, the text starts at the same level as the top of an image. I think the correct class to use should beitems-start
.