Using Tailwind CSS to set the margin of an element at certain breakpoints is not functioning as expected

I attempted to customize the margin of a div element at specific screen sizes by using a {screen}: prefix, but it did not have the desired effect. Here is what I tried:

<div className={'flex justify-center'}>
  <div className={'w-full my-8 sm:my-20 md:my-48'}>
    <div {...props}>
      {children}
    </div>
  </div>
</div>

Upon inspecting, I noticed that only the my-8 class seemed to be working correctly.

All classes from Tailwind, including flex for responsiveness, function as expected. However, the responsive margin (padding) does not seem to apply. My project utilizes Next.js and Tailwind CSS.

Answer №1

Apologies, but I managed to solve the issue on my own. The problem was related to the tailwind config settings.

Since this project was not created from scratch, I didn't have the opportunity to review the configuration initially.

I was able to resolve it by adjusting the margin variant as follows:

// tailwind.config.js
module.exports = {
  variants: {
    // ...
-     margin: ['hover'],
+     margin: ['responsive', 'hover'],
  }
}

Reference: https://tailwindcss.com/docs/margin#responsive-and-pseudo-class-variants

UPDATE 11/2021

In November 2021, Tailwind introduced version 3.0 with significant changes.

One of the notable changes is the removal of the variants section from your tailwind.config.js file.

Reference: https://tailwindcss.com/docs/upgrade-guide#remove-variant-configuration

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

"Enhabling tablesorter pagination to ensure that buttons always stay in sync with

I am experiencing an issue with the pagination buttons staying at the bottom of my page, even when there are only 2 entries left on the last page. Check out my table here: Is there a way to make the pagination buttons dynamically move to the top based on ...

How to troubleshoot Path Intellisense in VScode when using jsconfig.json for Next.js

In the structure of my project, it looks like this: jsconfig.json next.config.json components |_atoms |_Button.jsx |_Button.module.scss |_... |_... ... Within the jsconfig.json file, I have specified the following settings: { ...

Avoiding prop drilling when using server-side rendering in Next.js

How can I prevent prop drilling with SSR in this specific scenario? Layout.tsx: export default function RootLayout({ children, }: { children: React.ReactNode; }) { const user = await getUser(cookies().get("accessToken")?.value); return ...

Getting an error in NextJs when trying to execute a function in the API folder within pages, it is throwing the error message "'res.status is not

I'm currently working with NextJs and I'm in the process of setting up a subscription form to forward data to MailChimp. However, I've encountered an error that is showing: res.status is not a function This particular file resides within ...

The div in Bootstrap is not becoming scrollable despite using "overflow: auto"

I have a unique setup with two divs nested inside another div. The first div contains a table, while the second div holds buttons. I'm trying to make the table div scrollable by using overflow-auto, but it's not working as expected. I suspect tha ...

Is there a way to modify Style Properties in JavaScript by targeting elements with a specific class name using document.getElementsByClassName("Class_Name")?

I am seeking a solution to change the background color of multiple div boxes with the class name "flex-items" using JavaScript. Below is my current code: function changeColor(){ document.getElementsByClassName("flex-items").style.backgroundColor = "bl ...

Troubleshooting issues when utilizing flexbox in Firefox and Chrome version 48

Chrome 47 Behavior: The .scroll div scrolls correctly in Chrome 47, taking up 100% height using flex. Firefox Behavior: In Firefox, the .scroll div does not scroll properly and instead uses the content height. What is the solution that works across diffe ...

Modifying the appearance of a CSS element with jQuery: Step-by-step guide

The code I have is as follows: $('.signup-form-wrapper').css("style", "display: block"); $('.login-form-wrapper').css("style", "display: none"); I'm not sure why it's not working. The current appearance of ...

What is the best way to adjust the width of a textarea based on its content

How can I dynamically set the width of a React Semantic UI textarea based on its content? Setting min-width doesn't seem to be working. Any suggestions? <Textarea key={idx} defaultValue={formattedText} className="customInpu ...

Tips for creating a dynamic route with NextJs 14 API

Looking to start a blog with Next.js 14 and I'm working on defining a function in api/posts/[postSlug]/route.js. How do I access the postSlug parameter within this function? Here's my current function code: // api/posts/[postSlug]/route.js impor ...

How can you exhibit various images without relying on the <img> tag?

Is there a more efficient way to display over 500 images from a folder on an HTML page without the need to manually write out each img src tag? I have searched online for solutions, but most suggestions involve using PHP or "glob", which I am hesitant to ...

Storing Data in Nextjs: Ensuring Simple Data Persistence Across Route Changes

My current project involves allowing users to input images in the story route, which are then passed to the home route for display. Users can add as many images as they want, but I am facing a problem with retaining previously added images along with new o ...

How can I make my image shine when the mouse hovers over

I have recently come across a fascinating effect where an image glows up when the mouse hovers over it. This is not your typical border glow, but rather a unique enhancement of colors within the image itself. I discovered a library called Pixastic that can ...

What is the solution for getting `overflow-x` to function in place of `overflow-y`?

I have a main container with several sub-containers inside. When the main container's width exceeds its limit, I want to display a horizontal scroll bar. Instead of using the 'display: inline-block' property because it creates unwanted whit ...

The function `React.on('languageChanged')` is not effectively detecting changes in the language

I'm new to working with React and I'm looking for the best way to detect when a user changes their language preference. I am currently using next-translate to handle translations, but for some resources that come from an API, I need to update the ...

How to use getServerSideProps in Next.js

In my current scenario, I find myself within a folder in the pages directory, specifically in a file named [id].jsx. My goal is to make getServerSideProps return just the name of the page, for example /page/id123 should return id123. import Link from &a ...

Transform the client component layout into a server component structure

I am currently in the process of developing a responsive sidebar using headlessui Transition functionality. "use server"; export default function ServerLayout({ children }: { children: React.ReactNode }) { const [sidebarOpen, setSidebarOpen] = ...

A technique for horizontally centering an image while simultaneously ensuring that the bottom is pushed down, even at an unknown resolution, and maintaining center alignment if

Imagine a scenario where I have an image with unknown resolution. My goal is to horizontally center it, even if the window's width is smaller than the picture, while also adjusting the bottom of the window to match the height of this picture. Is ther ...

What is the best way to ensure that all elements inside a Grid item extend to the bottom, except for the text?

I currently have four columns within a Grid container, each structured as follows: <Grid item> <Typography>Big Title 1</Typography> <Card className={classes.stretchMe}> <CardContent> ...

"An error occured on the server: ReferenceError - 'self' is not defined" when utilizing the powerbi-client-react library in a TypeScript project

Recently, I integrated the powerbi-client-react library into my TypeScript project. However, upon running the project, I encountered the following error: Here is a snippet of my component code: import { PowerBIEmbed } from 'powerbi-client-react' ...