Mastering Responsive Web Design with Tailwind CSS: A Comprehensive Guide
In this blog we will dive deep on how to create a responsive Web app using Tailwind CSS.
I will be using react along with Tailwind CSS you can use any technology with Tailwind CSS. The Tailwind CSS is the must.
First what is responsive design and why it is important?
Responsive design is an approach to web design and development that aims to make websites and web applications look and function good on all devices! A responsive web design will automatically adjust for different screen sizes and viewports.
The primary goal of responsive design is to ensure that the user experience remains consistent and effective regardless of whether someone is accessing a website on a desktop computer, laptop, tablet, smartphone, or any other device with a web browser.
Responsive web design is crucial in today’s digital landscape for several reasons:
- Improved User Experience
— Responsive websites adapt to the user’s device, screen size, and orientation.
— This ensures that visitors have a consistent and user-friendly experience, whether they are accessing the site on a desktop computer, tablet, smartphone, or any other device.
— As most of the user uses phone or tablets to access the web, it becomes so much necessary to make the website responsive i.e to adjust the website according to the user’s screen. This will provide them a premium experience.
— A positive user experience can lead to increased engagement and longer time spent on your website. - Mobile-Friendly
— With the increasing use of mobile devices to browse the internet, having a responsive design is essential.
— It allows your website to look and function well on small screens, making it accessible to a broader audience and potentially boosting mobile traffic. - SEO Benefits
— Search engines like Google prioritize mobile-friendly websites in their search results.
— A responsive design can improve your website’s search engine ranking, leading to more organic traffic. - Future-Proofing
— As new devices with various screen sizes and resolutions continue to emerge, responsive design helps future-proof your website.
— It can adapt to these changes without requiring a complete redesign each time a new device becomes popular. - Positive Brand Image
— A well-designed, responsive website reflects positively on your brand.
— It demonstrates that you care about providing a good experience for your audience, which can improve your brand’s reputation and credibility.
This are some reasons why you website should be responsive.
Before we embark on our responsive journey, let’s delve into the world of Tailwind CSS for a moment.
Tailwind CSS is a popular utility-first CSS framework that simplifies and streamlines the process of building modern and responsive web designs.
Tailwind CSS is an open source CSS framework.
Tailwind CSS has gained popularity because it allows developers to quickly build and iterate on web designs without writing custom CSS, leading to more maintainable and consistent codebases.
Tailwind CSS is a highly customizable, low-level CSS framework.
You can go to their website to know more about them their installation, their documentation and everything. The Documentation is well written and it solves pretty much every query.
You can search for the things you want that makes it a lot easier to get the things. This is the website link — https://tailwindcss.com/
Lets Start our responsive Journey with Tailwind CSS in this we will learn how to use it and what are the features provided.
I will be using a react app as discussed about. So create a react project or use a project that you are already working on. Add the tailwind in your react app by installing it using npm or your preferred package manager.
Now lets talk how to make your app responsive using tailwind CSS.
By Default Tailwind uses the mobile first breakpoint system which means you first create your mobile design and then you can target the larger display using the breakpoints.
For making a website responsive we need to use the media query i.e. '@media’
along with the min-width or max-width parameters. But as we are using the power of Tailwind CSS, to help us with the responsive design we don’t want to write the media query, the tailwind does it for us.
There 5 Breakpoints available by default
This breakpoints are min-width breakpoints i.e. it will take effect only when it crosses the threshold value in pixels.
So ‘sm’ will target the device with width more than 640px and the ‘md’ will target the device with width more than 768px and so on.
If we have define the sm
and md
breakpoints together then the value of the sm
will be taken in consideration between 640px to 767px and as the pixels value rises more then 767px the sm
value will be override by the md
value and so on.
Using the predefined Breakpoints :-
To use this breakpoints we just need to add the desired breakpoints name as a prefix followed by a ‘:’ this will add the breakpoints to that element.
<div className="sm:bg-grey-200 md:bg-grey-400">
Hello World
</div>
In Tailwind every utilities need their own breakpoints separately.
<div className="sm:bg-grey-200 p-1 sm:p-2 md:bg-grey-400">
Hello World
</div>
Breakpoints Range :-
We can create a breakpoints range i.e. from this pixel range to that pixel range, it should apply the defined or attached property.
To use the breakpoints range we just have to add max- followed by the predefined breakpoints name it could be ‘lg
’, ‘xl
’, etc. followed by the ‘:’ and then the attribute.
<div className="sm:max-lg:bg-grey-200 ">
Hello World
</div>
Customizing the Breakpoints :-
We can change the predefined breakpoints value or we can define new breakpoints of our own by defining it inside the tailwind.config.js file.
If we want to change the definition we need to do it in the theme object value.
If we want to change the whole predefined value we can override the predefined value by adding the style object inside the theme.
/** @type {import('tailwindcss').Config} */
export default {
content: ["./src/**/*.{js,ts,jsx,tsx}"],
theme: {
screens:{
tablet:"690px",
},
extend: {},
},
plugins: [],
}
By adding the screens we are telling the Tailwind to use this screen option and not the default one. After defining this we cannot use the default one because the default one has been override by the screen definition.
If we need to just update the value we don’t want the whole screen value to be overridden we can use the extend to define the value.
We will define the screens object inside the extend object and then we will add the definition that we want to add.
/** @type {import('tailwindcss').Config} */
export default {
content: ["./src/**/*.{js,ts,jsx,tsx}"],
theme: {
extend: {
screens:{
md:"800px",
},
},
},
plugins: [],
}
In the above example I have updated the value of the md
breakpoints to 800px instead of the default pixel. By using the extend we can add new value or can change the default value without effecting the whole screen default value.
Adding the max-width breakpoints Range :-
We can also add max-width breakpoints to our theme if we want to. But by default the Tailwind is programed for min-width values, to make it max-width we need to add the value in the form of object that contains the key as max
and the value as ‘1200px’ pixels.
//tailwind.config.js
/** @type {import('tailwindcss').Config} */
module.exports = {
theme: {
screens: {
'2xl': {'max': '1535px'},
// => @media (max-width: 1535px) { ... }
'xl': {'max': '1279px'},
// => @media (max-width: 1279px) { ... }
'lg': {'max': '1023px'},
// => @media (max-width: 1023px) { ... }
'md': {'max': '767px'},
// => @media (max-width: 767px) { ... }
'sm': {'max': '639px'},
// => @media (max-width: 639px) { ... }
}
}
}
In this way you can add the max-width queries to your own project.
Adding the Fixed-range Breakpoints :-
We can also define the fixed-range Breakpoints in our theme objects so that we can use it everywhere in our projects. For this we will define a object of value that will contain both min
and the max
value along with the pixel range.
//tailwind.config.js
/** @type {import('tailwindcss').Config} */
module.exports = {
theme: {
screens: {
'sm': {'min': '640px', 'max': '767px'},
// => @media (min-width: 640px and max-width: 767px) { ... }
'md': {'min': '768px', 'max': '1023px'},
// => @media (min-width: 768px and max-width: 1023px) { ... }
'lg': {'min': '1024px', 'max': '1279px'},
// => @media (min-width: 1024px and max-width: 1279px) { ... }
'xl': {'min': '1280px', 'max': '1535px'},
// => @media (min-width: 1280px and max-width: 1535px) { ... }
'2xl': {'min': '1536px'},
// => @media (min-width: 1536px) { ... }
},
}
}
Arbitrary value or custom value for min and max :-
If we don’t want to go through all the hassle of making a custom breakpoints inside the theme objects. We can make use of the min
or max
modifiers to generate a custom breakpoints on the fly using any arbitrary value.
<div class="min-[320px]:text-center max-[600px]:bg-sky-300">
<!-- ... -->
</div>
In this way we don’t need to define the breakpoints in our tailwind config file. This is useful when we don’t want this breakpoints to be used for other component.
Key Points :-
- Don’t use
sm:
to target mobile devices. - Use unprefixed utilities to target mobile, and override them at larger breakpoints.
Example:-
<div className="bg-black sm:bg-grey-200 md:bg-grey-400">
Hello World
</div>
Let the bg-black
be the mobile version and then use the breakpoints for larger screens.