Open In App

Customizing Breakpoints in Tailwind CSS: A Step-by-Step Guide

Improve
Improve
Like Article
Like
Save
Share
Report

Tailwind CSS is a utility-first CSS framework that provides a set of pre-defined CSS classes that can be used to quickly style HTML elements. It emphasizes low specificity, and highly composable classes, and encourages consistent design. It is designed to be highly customizable and can be easily integrated with other CSS and JavaScript frameworks. One of the key features of Tailwind is its built-in responsive design support, which allows you to control how your layout and design elements change at different screen sizes.

Breakpoints in Tailwind CSS are specific screen widths at which the layout or design of a website can change. They are used to ensure that the website looks and behaves well on different screen sizes and devices.

For example, a website may have a two-column layout on larger screens but switch to a single-column layout on smaller screens to make the content more readable on mobile devices. These different layouts are achieved by using different CSS styles at different breakpoints.

Breakpoints in Tailwind CSS are defined in the configuration file and can be customized as needed. They are useful because they allow developers to create responsive designs that adapt to the user’s device and screen size, providing a better user experience. Additionally, they allow developers to take advantage of the entire viewport, which can be especially useful when targeting different screen sizes, and ensure that the design is optimized for those screens.

To add tailwind-CSS to your project you can refer to this article: How to setup Tailwind-CSS with Vite?

Default breakpoints in Tailwind-CSS are: The default breakpoints in Tailwind CSS are:

  • sm: (576px) – This breakpoint is used for screens with a width of at least 576px, such as small laptops and tablets in portrait mode.
  • md: (768px) – This breakpoint is used for screens with a width of at least 768px, such as tablets in landscape mode and larger laptops.
  • lg: (992px) – This breakpoint is used for screens with a width of at least 992px, such as large desktop monitors.
  • xl: (1200px) – This breakpoint is used for screens with a width of at least 1200px, such as extra-large desktop monitors.

Here is a step-by-step guide to customize breakpoints in Tailwind CSS and override default breakpoints:

Step 1: Installing Tailwind-CSS

First, you will need to install Tailwind CSS in your project. You can do this by referring to this article ‘Setup tailwind-CSS with Vite‘.

Step 2: Open tailwind.config.js file

After installing tailwind css, open the tailwind.config.js file in the root of your project. This file contains the default configuration for Tailwind CSS, including the default breakpoints.

Step 3: Changing the default breakpoints

To change the default breakpoint values, you will need to modify the screens object in the configuration file. For example, to change the default medium breakpoint from 768px to 1024px, you would update the md property in the screens object like this:

Javascript




module.exports = {
      screens: {
        'sm': '640px',
        'md': '1024px',
        'lg': '1280px',
        'xl': '1920px',
      },
}


Step 4: Add new custom breakpoints

If you want to add new custom breakpoints, you can simply add new properties to the screens object, for example, if you want to add a breakpoint called ‘2xl’ at 1440px:

Javascript




module.exports = {
     screens: {
           'md': '1024px',
           'lg': '1280px',
           'xl': '1920px',
           '2xl': '2140px',
    },
     // ...


Finally, you will need to update the responsive utilities in your CSS that reference the breakpoints you’ve changed. By doing this, you ensure that your layout will adjust correctly at your custom breakpoints.

Example 1: In this example, we are using ‘md’ breakpoint and the value of the breakpoint ‘md’ is set to ‘1024px’ which means the text background will change from green-600 to green-900.

HTML




<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content=
        "width=device-width, initial-scale=1.0">
    <title>Welcome to GFG</title>
    <link rel="stylesheet" href="style.css">
</head>
  
<body>
    <div class="flex justify-center 
        items-center min-h-screen">
        <div class="bg-green-600 p-[53px] 
            sm:bg-green-900 text-3xl 
            rounded-[20px] shadow-2xl">
            <p>Hello Geeks</p>
        </div>
    </div>
</body>
  
</html>


Output: The output background color will change when the screen width exceeds 1024 pixels as shown in the image:

 

Example 2: In this example, we are using ‘xl‘ breakpoint, and the value of the breakpoint ‘xl‘ is set to ‘1920px‘ which means the text ‘GeeksforGeeks Learning’ is only visible when the screen size is more than 1920 pixels.

HTML




<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content=
        "width=device-width, initial-scale=1.0">
    <title>Welcome to GFG!</title>
    <link rel="stylesheet" href="style.css">
</head>
  
<body>
    <div class="flex justify-center 
            items-center min-h-screen ">
        <div class="bg-green-700 hidden 
            xl:block p-8 text-3xl 
            rounded-2xl shadow-2xl">
            GeeksforGeeks Learning!
        </div>
    </div>
</body>
  
</html>


Output:

 

Explanation: The output will be visible only when the screen width exceeds 1920 pixels.

Conclusion: It is important to keep in mind that when you are customizing the breakpoints, you should also update the utilities that are related to those breakpoints, or your layouts and designs might not work as expected. You can either customize the default breakpoints or you can create new custom breakpoints. By following these steps, you will be able to customize your breakpoints in Tailwind CSS and create a custom responsive design that works perfectly for your project.



Last Updated : 31 Jan, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads