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.

https://i.sstatic.net/SOTdR.png

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

What are the steps for executing functions that exceed 10 seconds on Vercel?

One of the challenges I encountered with my app is that a crucial function takes approximately 20 minutes to complete when running. While this posed no issues during development, the problem arose when I deployed the app on Vercel, as the function did not ...

What is the most effective way to implement a CSS stylesheet on a particular individual element?

Just starting out in web development and I recently experimented with a CSS stylesheet on a basic HTML element. It worked great when I targeted the element by name like this: label { color: green; } However, I wondered if there was a way to apply sty ...

The CSS overflow scroller trims the excess background color

Attempting to build a website, I encountered an issue with displaying a scroll bar. Despite using the CSS property overflow: auto, I faced another problem. Let me illustrate the issue through a simple example. I have an outer div with the style overflow: ...

Having trouble accessing env variables from React Component in Next.js?

I recently set up a Next.js project and included an .env file to store environment variables used in my server.js file. However, I am facing an issue when trying to access these variables from a component. Can anyone provide guidance on how to resolve this ...

Bootstrap Model Footer Button Alignment

click hereI'm facing an issue while adding buttons to the model footer. The buttons are not aligning in line, instead they move to the next line. <div class="modal-footer"> <form method="post" action="admin_view"> ...

Incorporating both Tailwind CSS and Bootstrap in a Laravel project for a dynamic and stylish

Hey there! I've got a quick question for you. I'm currently working on my final year project and I was wondering if it would be okay to use both bootsrap and tailwind css together. I've used them separately before, but now I want to mix thin ...

How to implement loading an external script upon a page component being loaded in NextJS

I recently transferred an outdated website to Nextjs and I am having trouble getting the scripts to load consistently every time a page component is loaded. When navigating between pages using next/link component, the scripts only run the first time the ...

What is the best way to use CSS to ensure that dynamic, data-driven characters can be properly displayed within a div

I'm in the process of updating a data-centric website that relies on information from an automated database engine. In the HTML, there's a fixed-size button containing text pulled from the database. I'm looking to incorporate some CSS styles ...

Setting up CSS module class names in a Vue project using Vite js configuration

Within my vite.config.ts file, I have specified a configuration for CSS modules. return defineConfig({ ... css: { ... modules: { localsConvention: "camelCase", generateScopedName: "[name]__[local]__[hash:base64:2]" ...

What exactly is the significance of "utilizing a universal class name"?

In my current project, I am utilizing Material-UI version 3.1.2 to create the interface. Previously, when I was using Bootstrap4, I included style="overflow-y: scroll; height: 100%" in my head tag to ensure a scrollbar is always present, preventing the ap ...

triangle shape filled with a gradient that transitions between two colors

I have two triangles displayed at the bottom of my page - one on the left and one on the right. The right triangle is currently transparent but I want to add a gradient effect to it. For the triangle-bottom-right, I'd like the gradient to go from rgb ...

When the filter feGaussianBlur is applied to an SVG circle, it disappears on Safari but not on Chrome

While implementing the filter with feGaussianBlur to an SVG circle, I encountered a peculiar issue. It works perfectly on Chrome, but unfortunately, the circle disappears when viewed on Safari. https://i.sstatic.net/k916B.png https://i.sstatic.net/5Bfp9. ...

Layering one canvas on top of another

Here is the code I am working with. With a JavaScript library, I can draw on the first canvas and then merge it onto the second canvas. My question is, how can I make the first canvas stay in place or float, regardless of where I scroll on the second canv ...

Guidelines for aligning a form in the middle of the screen (positioned between the navigation bar and footer

Before asking this question, I made sure it's not a duplicate by researching previous similar issues with no success. I'm currently utilizing Bootstrap 4. You can view my app at (I'm unable to post the CSS and HTML of my React app due to S ...

Expand row size in grid for certain row and item

For my project, I am utilizing a Grid layout to display 5 items per row. Upon clicking on an item, my goal is to have the item detail enlarge by increasing the size of the HTML element. Is there a CSS trick that allows me to instruct the Grid to expand a ...

Can a button be connected to both navigate to another webpage and trigger a modal to appear on it simultaneously?

Is there a way to connect a button to a modal on a different page so that when the button is clicked, it opens another page (in a new tab with target 0) with the modal already displayed? ...

IE9 is causing a bizarre problem where the entire page is suddenly jumping. It's

UPDATE: The client has requested testing to disable the click and drag feature in IE, so replicating the bug may be difficult at this time. I apologize if this hinders the community's ability to help fix the root issue. Here's the issue: It occu ...

What is the method by which Next API Route manages multiple simultaneous requests?

Imagine I've developed an application with an API route. Let's say two users submit requests to upload a file to an S3 bucket through that API, and then my backend needs more than 2 minutes to process the file. How is this situation managed? How ...

Heading made of ribbon without increasing the scrolling space

Incorporating a unique ribbon-style heading that stretches out from the content area on both sides to create a 3D effect using an image background without relying on CSS3 techniques. I experimented with floating elements, negative margins, and relative po ...

The response from getStaticProps in Next.js is not valid

While following the Next.js documentation, I attempted to retrieve data from a local server but encountered an error message: FetchError: invalid json response body at http://localhost:3000/agency/all reason: Unexpected token < in JSON at position 0 ...