Skip to main content

How to add Tailwind CSS

About 3 min

How to add Tailwind CSS

  • A web application should be responsive and attractive. We will use Tailwind CSS as CSS framework to style our news application
  • The goals for this module:
    • Add Tailwind CSS to our project
    • Add a responsive container
    • Arrange our articles in a responsive way using the CSS grid/flex
    • Style our articles

Install and add Tailwind CSS

npm install tailwindcss @tailwindcss/postcss postcss
  • This command will add the following dependency in the package.json
"@tailwindcss/postcss": "^4.1.13", // dependencies
"tailwindcss": "^4.1.13", // dependencies
"postcss": "^8.5.6", // dependencies
  • Next, we have to create a .postcssrc.json file in the root of the project with the following content:
{
  "plugins": {
    "@tailwindcss/postcss": {}
  }
}
  • Finally, open the src/styles.css file and add the following content:
@import "tailwindcss";
  • From now on we can use Tailwind classes to style our Angular application!

Using Tailwind CSS

  • Using Tailwind CSS is nothing more than adding tailwind classes to your HTML elements
  • These tailwind classes are very declarative and straightforward (if you have a good knowledge of CSS 😉)

TIP: Tailwind CSS IntelliSense in VS Code

  • Install the following extension to get IntelliSense for the tailwind classestailwind intellisense

A container

  • First we're going to create our basic layout
  • We want our navigation menu and router-outlet below each other
  • We want the router_outlet to be a container
  • Change the contents of the app.html with the following code:
<div class="flex flex-col space-y-4 min-h-screen text-black bg-gray-100">
  <app-menu-component></app-menu-component>
  <main class="container mx-auto p-4 flex-1">
    <router-outlet></router-outlet>
  </main>
</div>
  • div:
tailwind classdescription
flexflex container: display: flex;
flex-colone below each other: flex-direction: column;
space-y-4margin top/bottom
min-h-screendiv's height is minimum the viewport height: min-height: 100vh;
text-blacktext color
bg-gray-100background color
  • main:
tailwind classdescription
containerresponsive container: width: 100%;
mx-autocenter screen: margin left/right : auto;
flex-1allow a flex item to grow and shrink as needed, ignoring its initial size: flex: 1 1 0%
  • Make sure to remove (or comment out) all the CSS from the ArticleComponent (article-component.css)!
  • The result should look like this: tailwind container flex

TIP: Developer Tools

Article grid/flex

  • Our next goal is to use a grid system to position the article cards in a responsive way
  • First we will add the grid system to our home-component.html and we add some basic styling to the app-article-component:
<h1 class="text-4xl">Welcome home</h1>
<div
  class="grid grid-cols-1 md:grid-cols-1 lg:grid-cols-2 xl:grid-cols-3 gap-3 mx-auto py-6"
>
  @for (article of articles; track $index) {
  <app-article-component [article]="article"></app-article-component>
  }
</div>
tailwind classdescription
text-4xlfont-size and line-height
gridcreates a CSS grid: display: grid;
grid-cols-1mobile-first: by default there is 1 column
md:grid-cols-1medium breakpoint ( > 768px): 1 column
lg:grid-cols-2large breakpoint ( > 1024px): 2 columns
xl:grid-cols-3extra large breakpoint ( > 1280px): 3 columns
gap-3gap/size between the columns
py-6padding top/bottom
  • Result so far: tailwind article step 1

  • Now we're going to style the article component itself: article-component.html

@if(article) {
<article
  class="flex flex-col justify-between h-full bg-white border-gray-200 border-2 p-6 shadow"
>
  <h1 class="text-4xl my-3">{{article.title}}</h1>
  <h4 class="text-xl my-3">{{article.subtitle}}</h4>
  <figure class="self-center my-3">
    <img
      class="max-w-full h-auto"
      src="{{article.imageUrl}}"
      alt="{{article.imageCaption}}"
    />
    <figcaption class="italic text-center">{{article.imageCaption}}</figcaption>
  </figure>
  <p class="text-justify my-3">{{article.content}}</p>
  <footer class="italic uppercase">
    <p>Author: {{article.author}}, Published: {{article.publishDate}}</p>
  </footer>
</article>
}
tailwind classdescription
flexflex container: display: flex;
flex-colall elements within <article> below each other: flex-direction: column;
justify-betweendistribute all flex items evenly on the main axis (first item at the start, last item at the end): justify-content: space-between;
border-gray-200border color
border-2border width
shadowbox shadow
h-fullfull height: height: 100%;
self-centeritem is placed around the center on the cross axis: align-self: center;
max-w-fullresponsive image: max-width: 100%;
h-autoresponsive image: height: auto;
italicfont-style: italic;
text-centertext-align: center;
text-justifytext-align: justify;
uppercasetext-transform: uppercase;
  • End result: tailwind article step 2tailwind article step 2 mobile