📖 Scenario: You are building a simple Next.js app to show a welcome message. You want to explore different ways to style this message to see how styling affects the look and feel of your app.
🎯 Goal: Create a Next.js component that displays a welcome message. Apply styles using inline styles and CSS modules to understand how each styling option changes the appearance.
📋 What You'll Learn
Create a functional component called WelcomeMessage in Next.js
Add a message variable with the text 'Welcome to Next.js styling options!'
Apply inline styles to the message text in Step 3
Add a CSS module file with a class that styles the message text in Step 4
💡 Why This Matters
🌍 Real World
Understanding styling options helps you build visually appealing and maintainable web apps with Next.js.
💼 Career
Front-end developers often choose between inline styles, CSS modules, and utility-first CSS frameworks like Tailwind to style React and Next.js apps efficiently.
Progress0 / 4 steps
1
DATA SETUP: Create the Next.js component with message
Create a functional component called WelcomeMessage that returns a <div> containing a variable called message with the exact text 'Welcome to Next.js styling options!'.
NextJS
Hint
Define the component function, create the message variable, and return a <div> with the message inside.
2
CONFIGURATION: Prepare for inline styling
Inside the WelcomeMessage component, create a constant called inlineStyle that is an object with color set to 'blue' and fontWeight set to 'bold'.
NextJS
Hint
Create an object named inlineStyle with the specified CSS properties.
3
CORE LOGIC: Apply inline styles to the message
Modify the <div> in WelcomeMessage to use the inlineStyle object for its style attribute.
NextJS
Hint
Add style={inlineStyle} to the <div> tag.
4
COMPLETION: Add CSS module and apply class
Create a CSS module file named WelcomeMessage.module.css with a class message that sets color to green and font-style to italic. Import this module in the component and apply the message class to the <div> instead of inline styles.
NextJS
Hint
Create the CSS module file with the class and import it. Use className={styles.message} on the <div>.
Practice
(1/5)
1. Why is it important to choose the right styling option in a Next.js project?
easy
A. It disables responsive design.
B. It makes the website load slower.
C. It prevents the use of JavaScript.
D. It helps keep the code clean and easy to maintain.
Solution
Step 1: Understand the role of styling in code maintenance
Choosing the right styling method helps organize styles, making the code easier to read and update.
Step 2: Recognize the impact on project quality
Good styling choices improve maintainability and scalability, avoiding messy or duplicated code.
Final Answer:
It helps keep the code clean and easy to maintain. -> Option D
Quick Check:
Right styling = clean code [OK]
Hint: Right styling keeps code neat and easy to update [OK]
Common Mistakes:
Thinking styling slows the site down by default
Believing styling disables JavaScript
Assuming styling stops responsive design
2. Which of the following is the correct way to import a CSS module in a Next.js component?
easy
A. import styles from './styles.module.css';
B. import './styles.css';
C. require('./styles.module.css');
D. import styles from './styles.css';
Solution
Step 1: Identify the syntax for CSS modules in Next.js
Next.js uses the syntax import styles from './filename.module.css'; to import CSS modules.
Step 2: Compare options with correct syntax
import styles from './styles.module.css'; matches the correct import syntax for CSS modules. import './styles.css'; imports a global CSS file without module support. require('./styles.module.css'); uses CommonJS require which is not standard in Next.js ES modules. import styles from './styles.css'; imports a CSS file without the .module suffix, so it won't be treated as a module.
Final Answer:
import styles from './styles.module.css'; -> Option A
Quick Check:
CSS module import = import styles from './styles.module.css'; [OK]
Hint: CSS modules always use .module.css with import [OK]
Common Mistakes:
Using require instead of import
Importing CSS without .module suffix for modules
Confusing global CSS import with module import
3. Given this Next.js component using Tailwind CSS:
What will happen when the user hovers over the button?
medium
A. Nothing changes on hover.
B. The button text color changes to black.
C. The button background color changes to a darker blue.
D. The button becomes disabled.
Solution
Step 1: Analyze the Tailwind CSS classes used
The class hover:bg-blue-700 means the background color changes to blue-700 shade on hover.
Step 2: Understand the effect on the button
When hovered, the button's background color changes from blue-500 to blue-700, a darker blue shade.
Final Answer:
The button background color changes to a darker blue. -> Option C
Quick Check:
hover:bg-blue-700 changes background on hover [OK]
Hint: hover:bg-* changes background color on hover [OK]
Common Mistakes:
Thinking text color changes instead of background
Assuming button disables on hover
Believing no change happens on hover
4. You wrote this Next.js component with CSS modules:
import styles from './Button.module.css';
export default function Button() {
return <button className="styles.button">Press</button>;
}
Why does the button not get styled as expected?
medium
A. The CSS module file must be named Button.css, not Button.module.css
B. The className should use curly braces: className={styles.button}
C. You cannot use CSS modules in Next.js components
D. The styles object is not imported correctly
Solution
Step 1: Identify how to apply CSS module classes in JSX
CSS module classes must be used with curly braces to evaluate the JavaScript expression, like className={styles.button}.
Step 2: Check the given code for syntax errors
The code uses className="styles.button" which treats it as a string, not a variable, so styles are not applied.
Final Answer:
The className should use curly braces: className={styles.button} -> Option B
Quick Check:
Use curly braces for CSS module classes [OK]
Hint: Use curly braces for CSS module classes in JSX [OK]
Common Mistakes:
Using quotes instead of curly braces for className
Renaming CSS module files incorrectly
Thinking CSS modules are unsupported in Next.js
5. You want to create a responsive Next.js page that changes layout on small screens. Which styling approach best helps you achieve this while keeping styles scoped and easy to maintain?
hard
A. Use CSS modules combined with Tailwind CSS utility classes.
B. Use only global CSS files with media queries.
C. Use inline styles with JavaScript objects for all styles.
D. Use plain CSS files imported in _app.js without modules.
Step 2: Evaluate options for responsiveness and maintainability
Using CSS modules combined with Tailwind CSS utility classes combines scoped styles with powerful responsive utilities, making it easy to maintain and adapt layouts for different screen sizes.
Step 3: Understand drawbacks of other options
Global CSS can cause conflicts and harder maintenance. Inline styles lack media query support easily. Plain CSS without modules risks style clashes.
Final Answer:
Use CSS modules combined with Tailwind CSS utility classes. -> Option A
Quick Check:
Scoped + responsive utilities = Use CSS modules combined with Tailwind CSS utility classes. [OK]
Hint: Combine CSS modules with Tailwind for scoped responsive styles [OK]
Common Mistakes:
Relying only on global CSS causing style conflicts
Using inline styles which lack media query support
Ignoring scoped styles leading to messy CSS
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.