3

How to Write Better Tailwind CSS Containers

 2 years ago
source link: https://dev.to/markmead/how-to-write-better-tailwind-css-containers-4m8c
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
Mark Mead

Posted on Nov 12

• Originally published at markmead.dev on Nov 12

How to Write Better Tailwind CSS Containers

When it comes to Tailwind CSS containers, a lot of people will write max-w-screen-xl mx-auto px-4 sm:px-6 lg:px-8

Or something similar. In fact, this website uses the classes above but instead writing that every time, instead I write container.

As you can see, this is much cleaner and it will keep your HTML uncluttered.

You could argue it's less "declarative" but Bootstrap has been using container for years and there's never been any issues. Also, if you're that worried, don't - the explanation to how container works in Tailwind CSS can be found in the tailwind.config.js.

A typical Tailwind CSS config will have the following:

theme: {
  extend: {}
}
Enter fullscreen modeExit fullscreen mode

Here you can override Tailwind CSS classes or extend them.

To update this to make container perform how we want, we will override the class with the following:

theme: {
  container: {
    center: true,
    padding: {
      DEFAULT: "1rem",
      sm: "1.5rem",
      lg: "2rem"
    }
  },
  extend: {}
}
Enter fullscreen modeExit fullscreen mode

What this is doing is taking the old container class from Tailwind CSS that only did max-w-screen-xl and replacing it with max-w-screen-xl mx-auto px-4 sm:px-6 lg:px-8 but we only need to write a single class name.

You can adjust the padding for each screen size that's available in Tailwind CSS.

If you opt into using a single padding class then don't write it as an object, simply do padding: "1rem".

Read up on the Tailwind CSS container class on the official docs page - Tailwind CSS - Container


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK