frontend-interview-questions

1. How do you approach styling in React? Compare CSS-in-JS, Styled Components, Sass, and TailwindCSS.

I choose styling tools based on the project’s complexity, team preferences, scalability, and performance needs.

Technique Pros Cons
CSS-in-JS(e.g., Emotion, styled-components) Scoped styles, dynamic theming, colocated with components Runtime overhead, harder to extract critical CSS
Styled Components Component-driven, theming support, autoscoping Bundle size, runtime parsing, toolchain complexity
Sass (SCSS) Nesting, variables, mixins, mature tooling Global scope by default, manual BEM or conventions needed
TailwindCSS Utility-first, fast prototyping, responsive by default Verbose class names, learning curve, poor separation of concerns

My default : Tailwind for fast-moving teams or design systems; Styled Components for themeable component libraries.


2. What strategies do you use to ensure a React application is responsive across different devices?


3. How would you implement a dark mode toggle in a React application?

Use a theme state (or context), toggle a class on the body or html, and style conditionally.

CSS-in-JS example : Use a ThemeProvider with light/dark themes.


4. Design a reusable button component in React with Styled Components that supports multiple variants (e.g., primary, secondary).

import styled from 'styled-components';

const Button = styled.button`
  padding: 0.5rem 1rem;
  border-radius: 4px;
  font-weight: bold;
  background: ${({ variant }) =>
    variant === 'primary' ? '#007bff' : '#6c757d'};
  color: white;
  border: none;
  cursor: pointer;

  &:hover {
    opacity: 0.9;
  }
`;

// Usage:
<Button variant="primary">Save</Button>
<Button variant="secondary">Cancel</Button>


5. What are the pros and cons of using TailwindCSS in a large React project with multiple developers?

Pros:

Cons:


6. How do you manage CSS specificity conflicts in a React app with a mix of global and component-scoped styles?


7. How do you implement a CSS animation in React that triggers based on a state change?

import "./Fade.css"; // .fade { opacity: 0; transition: opacity 0.3s } .fade.show { opacity: 1; }

const FadeComponent = ({ visible }) => (
  <div className={`fade ${visible ? "show" : ""}`}>Hello</div>
);