Next JS and the power of media queries

I've been searching for a while now, but I just can't seem to find a solution to my problem. I'm attempting to utilize different files for my media query breakpoints in Next JS, but they don't appear to be working.

I created the files in: styles/queries/sizeone.css (the first breakpoint)

I imported them into my _app.tsx like this, and also modified the viewport tag from the default Next JS head.

import { AppProps } from 'next/app';
import Head from 'next/head'

import '../styles/globals.css'
import '../styles/normalize.css'
import '../styles/queries/sizeone.css'

function App({ Component, pageProps }: AppProps) {  
  return (
    <>
    <Head>
      <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    </Head>
    <Component {...pageProps} />
    </>
  )
}

export default App;

This is what's inside my sizeone.css file, which contains a simple media query.

@media only screen and (min-width: 1px) and (max-width: 767px){
  #hero .content .title h1 {
    text-align: center;
    color: var(--main-color);
    font-family: var(--main-font);
    font-weight: 700;
    font-size: 20px;
    margin: 0 0;
  }
}

Here's how I'm applying styles to the elements:

            <section id={styles.hero}>
                <div className="wrapper">
                    <div className={styles.content}>

                        <div className={styles.title}>
                            <h1>Creating <span className="gradient-text">remarkable</span> <br /><span className="gradient-text">experiences</span> through digital art.</h1>
                            <p>Freelancing UI/UX design & Front-end development</p>

This is the part that is shown within the sizeone.css file.

Thank you for taking the time to read my question, hopefully we can find a resolution soon :)

Answer №1

Essentially, there seems to be a mix-up between two methods of assigning CSS classes: the traditional approach and the CSS modules method. For more information on this topic, it's recommended to explore: CSS Tricks: CSS Modules

An example can illustrate better than words, so pay attention to the contrast in how CSS files are imported and classes are applied in the following two examples.

1. Traditional approach:

/* globals.css */
.big {
  font-size: 20px;
}
import '../styles/globals.css';
export default function App() {
  return <p className="big">Standard big</p>;
}

2. CSS modules approach:

/* heading.module.css */
.big {
  font-size: 20px;
}
import styles from './heading.module.css';
export default function Heading() {
  return <p className={styles.big}>CSS Modules big</p>;
}

CSS files that are imported in the _app.js file should have global scope throughout the application and CSS modules should not be used here - stick with traditional CSS.

Any other styles that are specific to a particular component should be imported in the respective component's JS/TS file, and the recommended practice for this in Next.js is using CSS modules.

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

Parallax Effect malfunctioning on iPhones

Hey there, I recently created a parallax UI using pure CSS. It seems to work fine on Android and desktop browsers, but when I tested it on iOS or iPhone, the parallax effect doesn't seem to be working correctly. I'm not sure why this is happening ...

Fixing a dropdown menu with multiple levels

I am attempting to create a dropdown menu with multiple levels. However, when I hover over the first level, the second level appears slightly above and to the left of the first level. I want the second level to appear directly below the first level in the ...

Arrange three images both vertically and horizontally at the center of the page

I am facing an issue with aligning a button and 2 images in a row on my website. Currently, this is how it looks: https://i.stack.imgur.com/yrQB6.png Here is the HTML code I am using: <div class="row text-center"> <div class="col-md-12"> ...

Tips for changing the background color depending on the value

I am creating a table displaying the results of a tournament. Each team's final placement and original seeding will be listed. I plan to include a small bubble next to each team showing how their final placement compares to their initial seeding. To v ...

Issue with Bootstrap small column vertical alignment not functioning as expected

I am working on a bootstrap layout with two columns. One column contains multiple elements and text, making it quite long, while the other column only has an image. I am trying to center this image vertically within the left column of content. Here is the ...

How can CSS be used to format an unordered list around images?

Can anyone suggest ways to improve the formatting of this HTML list when wrapping around a left-floated image? I often encounter this small problem while working on client websites: The image has a right-margin, but it doesn't seem to affect the ... ...

Can the orientation of the card reveal be customized in Materializecss?

Exploring the card-reveal feature in the materializecss framework on this page: https://codepen.io/JP_juniordeveloperaki/pen/YXRyvZ with official documentation located at: In my project, I've rearranged the <div class="card-content"> to display ...

implement an angular directive to apply a CSS element

I am utilizing AngularJS and ng-repeat to populate a dynamic list of studies. This list has the capability to toggle into child elements of each item, creating an accordion-style toggle list that can go up to three levels deep for each list item. I am curr ...

Discovering a specific string within an array of nested objects

Seeking guidance on creating a dynamic menu of teams using JavaScript/TypeScript and unsure about the approach to take. Here is an example dataset: const data = [ { 'name': 'Alex A', 'agentId': '1225& ...

Steps for positioning a RadioButtonList at the center of a table cell

How can I easily center a RadioButtonList? It used to be straightforward, but for some reason the below HTML is not working. Can you spot what's missing? <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></ ...

Tips to center a circular progress bar in Material UI

Does anyone know how to properly center-align a circular progress bar in a login form using material UI? I'm having trouble with it not being positioned correctly: screenshot {isLoading && <CircularProgress />} {user?.ema ...

housing the picture within a CSS grid

I am new to working with css grids and I have encountered an issue. The background image I want to use is larger than the size of the grid itself. How can I make the image fit within the grid without exceeding its boundaries? When I attempt to insert the ...

Leverage the power of getServerSideProps when working with Dynamic Routes

I have been working on implementing getServerSideProps in my file named [username].js which utilizes dynamic routing. Next.js requires the use of both getStaticPaths() and getStaticProps({ params }) for dynamic routing. However, there seems to be an issu ...

The margins are not being maintained by the columns

Currently, I am using Bootstrap 3.3.7 and facing an issue with the alignment of smaller columns in relation to bigger columns. To see a demo, click here. I have set a margin of 10px between the columns. For example, the first row consists of the followin ...

Steps to include a title next to a progress bar:

Is there a way to achieve something like this? https://i.sstatic.net/dhO2f.jpg I attempted to use bootstrap but I ran into an issue where the title was slightly misaligned below the progress bar. Can someone offer assistance with this matter? Apologies i ...

Setting the port for Next.js on PM2 involves configuring the ecosystem file in the project

I am currently working on a standard next js application and have the following scripts in my package.json file. "scripts": { "dev": "next dev", "build": "next build", "start": " ...

What causes the disparity in the functionality of getServerSideProps between index.js and [id].js in NextJS?

Exploring NextJS and React as part of my e-commerce site development journey has been exciting. However, I encountered a roadblock when trying to connect to an external database that requires real-time updates, making getStaticProps unsuitable for my needs ...

Learn how to use Angular2 or TypeScript to display 'unsubscribe' and 'subscribe' text on a toggle button

I'm working on a toggle button that initially displays the word subscribe on the thumb. When the toggle is disabled, I want it to show unsubscribe instead. Can someone please help me achieve this functionality? Here's the code snippet: <md-s ...

How to create nested nav menus that are optimized for touch-screen devices?

When it comes to creating responsive websites, one challenge I frequently encounter is designing a multi-level nav menu that functions effectively for touch devices. Many plugins and methods exist, but most fall short because they do not allow a second-l ...

Troubleshooting: Next JS 13/14 - Server component failing to update data upon revisiting without requiring a refresh

After attempting to retrieve the most recent liked items from a server component in next, I noticed that the data only displays when manually refreshing the page. Even when I navigate away and return, the data is not refetched automatically - however, usin ...