[React] const StyledComponents = styled.Love `๐Ÿ–ค`;

hyeonbinยท2023๋…„ 5์›” 7์ผ

FE-log

๋ชฉ๋ก ๋ณด๊ธฐ
1/9
post-thumbnail

๐ŸŽจ Styled-Components๋ž€?

  • CSS in JS ๊ธฐ์ˆ ์ด์ž ์ธ๊ธฐ ํ„ฐ์ง€๋Š” React ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ

    React ์Šคํƒ€์ผ ์ ์šฉ๋ฐฉ์‹
    1) CSS
    2) CSS-in-CSS (Sass, Bootstrap, TailwindCSS ๋“ฑ)
    3) CSS-in-JS (styled-components ๋“ฑ)

  • styled-components ์„ค์น˜ํ•˜๊ธฐ

   npm install styled-components
   // npm install styled-components@5


๐ŸŽจ Global Style

// App.js
import React from 'react';
import Example from './components/Example';
import normalize from 'styled-normalize';
import { createGlobalStyle } from 'styled-components';

// GlobalStyle ์ ์šฉ
const GlobalStyle = createGlobalStyle`
${normalize}

div {
  padding: 30px;
}

span {
  color: hotpink;
  font-size: 18px;
  font-weight: bold;
}

button {
  width: 60px;
  border: none;
  margin-top: 20px;
  cursor: pointer;
  background-color: pink;
}
`;

const App = () => {
  return (
    <>
      <GlobalStyle />
      <div>
        <h1>์•ˆ๋…• ํด๋ ˆ์˜คํŒŒํŠธ๋ผ</h1>
        <span>์„ธ์ƒ์—์„œ ์ œ์ผ๊ฐ€๋Š” ํฌํ…Œ์ดํ† ์นฉ</span>
        <br />
        <button>ํด๋ฆญ</button>
        <Example />
      </div>
    </>
  );
};

export default App;
// Example.jsx
import React from 'react';

const Example = () => {
  return (
    <>
      <h1>๋”ธ๊ธฐ๊ฐ€ ์ข‹์•„ ๋”ธ๊ธฐ๊ฐ€ ์ข‹์•„</h1>
      <span>๋”ธ๊ธฐ ๋”ธ๊ธฐ ๋”ธ๊ธฐ ๋”ธ๊ธฐ ์˜ˆ์—์—์—์—์—</span>
      <br />
      <button>click</button>
    </>
  );
};

export default Example;

Global Style
  • Styled-Components๊ฐ€ ์ œ๊ณตํ•˜๋Š” createGlobalStyle() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
  • Global ๋ง ๊ทธ๋Œ€๋กœ ์ „์—ญ ์ฆ‰, ๋ชจ๋“  ์ปดํฌ๋„ŒํŠธ์—์„œ ๊ณตํ†ต์œผ๋กœ ์ ์šฉ๋˜๋Š” ์Šคํƒ€์ผ์„ ์˜๋ฏธํ•œ๋‹ค.
    โ†’ ์›น ํŽ˜์ด์ง€ ๋ฐฐ๊ฒฝ์ƒ‰, ๊ธ€๊ผด ๋“ฑ ๊ณตํ†ต์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ์Šคํƒ€์ผ์— ํšจ์œจ์ ์ด๋‹ค.
  • createGlobalStyle() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด CSS ํŒŒ์ผ ์—†์ด๋„ ์ „์—ญ ์Šคํƒ€์ผ ์‰ฝ๊ฒŒ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.
  • createGlobalStyle()๋ฅผ App.js(ํ˜„์žฌ ์ตœ์ƒ์œ„ ์ปดํฌ๋„ŒํŠธ) ์ƒ๋‹จ์— ์ถ”๊ฐ€ํ•ด ์ฃผ๋ฉด, ๋ชจ๋“  ํ•˜์œ„ ์ปดํฌ๋„ŒํŠธ ์Šคํƒ€์ผ์— ์ ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ๊ธฐ๋ณธ์ ์œผ๋กœ ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๋†’๋‹ค.
  • Styled-Components ๋ฌธ๋ฒ•์—์„œ ์Šคํƒ€์ผ์„ ๊พธ๋ฉฐ์ค„ ๋•Œ tagged tamplate literal (``)์„ ์‚ฌ์šฉํ•œ๋‹ค.




๐ŸŽจ Reset CSS vs Normalize CSS


Reset & Normalize ์™œ ์ฐจ์ด๊ฐ€ ๋‚˜๋Š” ๊ฑธ๊นŒ?
  • ๋ธŒ๋ผ์šฐ์ €์—์„œ ์ œ๊ณตํ•˜๋Š” ๊ธฐ๋ณธ ์Šคํƒ€์ผ์ด ๋ธŒ๋ผ์šฐ์ € (Chrome, Safari, Edge ๋“ฑ) ๋งˆ๋‹ค ๋‹ค๋ฅด๋‹ค.
  • Reset CSS vs Normalize CSS๋Š” ํฌ๋กœ์Šค ๋ธŒ๋ผ์šฐ์ง•์„ ์œ„ํ•ด ๊ฐ ๋ธŒ๋Ÿฌ์šฐ์ €์˜ ๊ธฐ๋ณธ ์Šคํƒ€์ผ์„ ๋ฌดํšจํ™”ํ•œ๋‹ค.

Reset CSS / Normalize CSS ์„ค์น˜ํ•˜๊ธฐ

   npm i styled-reset
   npm i styled-normalize


1. Reset CSS

  • ๋ธŒ๋ผ์šฐ์ €์˜ ๋‚ด์žฅ ์Šคํƒ€์ผ์„ ์ดˆ๊ธฐํ™” ์‹œ์ผœ์ฃผ๊ธฐ์— Reset CSS์„ ์ ์šฉํ•˜๋ฉด ๊ธ‰๊ฒฉํ•˜๊ฒŒ ์›นํŽ˜์ด์ง€๊ฐ€ ๋‹ฌ๋ผ์ง„๋‹ค.
  • ๊ฒ‰์œผ๋กœ ๋ณผ ๋•Œ ํƒœ๊ทธ ๊ตฌ๋ณ„์ด ๋ถˆ๊ฐ€ํ•ด์ง„๋‹ค. Reset CSS๋ฅผ ์ ์šฉํ•œ ๋’ค, ์•„๋ฌด๋Ÿฐ ์Šคํƒ€์ผ๋„ ์ถ”๊ฐ€ํ•˜์ง€ ์•Š์œผ๋ฉด, ํ•ด๋‹น ์›นํŽ˜์ด์ง€ ์ƒ์˜ ๋ชจ๋“  HTML ์š”์†Œ๊ฐ€ ๋™์ผํ•˜๊ฒŒ ๋ณด์ธ๋‹ค.

2. Normalize CSS

  • ์–ด๋А ๋ธŒ๋ผ์šฐ์ €์—์„œ๋“  ์ผ๊ด€์ ์ธ ๋ชจ์Šต์œผ๋กœ ๋‚˜ํƒ€๋‚œ๋‹ค.
  • ๋ธŒ๋ผ์šฐ์ €์˜ ๋‚ด์žฅ ์Šคํƒ€์ผ์„ ์ตœ๋Œ€ํ•œ ์œ ์ง€ํ•˜๊ณ , ๋ธŒ๋ผ์šฐ์ € ๊ฐ„์— ์ƒ์ดํ•œ ๋ถ€๋ถ„๋งŒ ์Šคํƒ€์ผ์„ ํ†ต์ผ์‹œ์ผœ ์ค€๋‹ค.
  • ์ฝ”๋“œ์˜ ์šฐ์„ ์ˆœ์œ„ ๋“ฑ์˜ ์ธก๋ฉด์—์„œ ์ถฉ๋Œ์ด ์ผ์–ด๋‚  ๊ฐ€๋Šฅ์„ฑ์ด ์ ๋‹ค.
  • ์ผ๋ฐ˜์ ์œผ๋กœ React ํ”„๋กœ์ ํŠธ์—์„œ Normalize CSS๊ฐ€ ๊ฐ€์žฅ ๋Œ€์ค‘์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๊ณ  ์žˆ๋‹ค.

3. ์–ด๋–ค ๊ฑธ ์‚ฌ์šฉํ•ด์•ผ ํ• ๊นŒ?

Reset CSS

  • ๋ธŒ๋ผ์šฐ์ €์˜ ๋‚ด์žฅ ์Šคํƒ€์ผ์— ์ „ํ˜€ ์˜์กดํ•˜์ง€ ์•Š๊ณ  ๋ชจ๋“  HTML ์š”์†Œ๋ฅผ ํ•˜๋‚˜๋ถ€ํ„ฐ ์—ด๊นŒ์ง€ ์ง์ ‘ ์Šคํƒ€์ผ์„ ํ•  ๊ฒฝ์šฐ
  • ๊ทœ๋ชจ๊ฐ€ ์žˆ๋Š” ์žฅ๊ธฐ ํ”„๋กœ์ ํŠธ๋‚˜ ํšŒ์‚ฌ ์ „์ฒด๋ฅผ ์œ„ํ•œ ๋””์ž์ธ ์‹œ์Šคํ…œ์„ ๋งŒ๋“ค ๊ฒฝ์šฐ

Normalize CSS

  • ๋ธŒ๋ผ์šฐ์ €์˜ ๋‚ด์žฅ ์Šคํƒ€์ผ์„ ์ตœ๋Œ€ํ•œ ๋”ฐ๋ฅด๋ฉด์„œ ํ•„์š”ํ•œ ๋ถ€๋ถ„๋งŒ ์Šคํƒ€์ผ์„ ๋ณ€๊ฒฝํ•˜๊ฑฐ๋‚˜ ์ถ”๊ฐ€ํ•˜๊ณ  ์‹ถ์„ ๊ฒฝ์šฐ
  • ๋ชจ๋“  HTML ์š”์†Œ๋ฅผ ์ฒ˜์Œ๋ถ€ํ„ฐ ์Šคํƒ€์ผ ํ•  ์—ฌ๋ ฅ์ด ๋˜์ง€ ์•Š๊ณ , ์ผ์ •์ด ๋นก๋นกํ•œ ์†Œ๊ทœ๋ชจ ํ”„๋กœ์ ํŠธ์ผ ๊ฒฝ์šฐ

๊ฒฐ๋ก ์€ ๋ฌด์—‡์„ ์‚ฌ์šฉํ•˜๋“  ์ •๋‹ต์€ ์—†๋‹ค๋Š” ๊ฒƒ ์•„๋‹๊นŒ?
์ž์‹ ์˜ ์ƒํ™ฉ์— ๊ฐ€์žฅ ์ ํ•ฉํ•˜๊ณ  ํšจ์œจ์ ์ธ ๋ฐฉ๋ฒ•์„ ์„ ํƒํ•˜์ž!! ๊ทธ๊ฒƒ์ด ๊ทธ๋Œ€๊ฐ€ ๊ฐ€๋Š” ๊ธธ์ด๋…ธ๋ผ ๐ŸŽข

Reset / Normalize ์ฐธ๊ณ  ์‚ฌ์ดํŠธ




๐ŸŽจ styled-components ์ ์šฉ

import React from 'react';
import styled from 'styled-components';

const ContentDiv = styled.div`
  margin: 40px;
  padding: 30px;
  border: solid 2px;
  background-color: aliceblue;
`;

const ContentH2 = styled.h2`
  width: 200px;
  margin: 0 auto;
  text-align: center;
`;

const App2 = () => {
  return (
    <ContentDiv>
      <ContentH2>Content</ContentH2>
      <p>
        Lorem ipsum dolor sit amet consectetur adipisicing elit. Eos excepturi corrupti quo
        blanditiis! Adipisci amet corporis ipsum odio minima aliquid quisquam! Dignissimos natus
        laborum qui veritatis quaerat eaque! Nemo, ullam.
      </p>
    </ContentDiv>
  );
};

export default App2;

styled-components
  • React ์ธ๋ผ์ธ์œผ๋กœ ์Šคํƒ€์ผ์„ ์ ์šฉํ•  ๋•Œ์™€ ๋‹ค๋ฅด๊ฒŒ ๊ธฐ์กด CSS ๋ฌธ๋ฒ•์œผ๋กœ ํ”„๋กœํผํ‹ฐ ์ž‘์„ฑ ๊ฐ€๋Šฅํ•˜๋‹ค.
const ๋ณ€์ˆ˜๋ช… = styled.ํƒœ๊ทธ๋ช…`
	background-color : red;
`




๐ŸŽจ props์— ๋”ฐ๋ฅธ ์กฐ๊ฑด๋ถ€ ์Šคํƒ€์ผ

import React from 'react';
import styled from 'styled-components';

const ContentDiv = styled.div`
  margin: 40px;
  padding: 30px;
  border: solid 2px;
  background-color: aliceblue;
`;

const ContentH2 = styled.h2`
  color: ${(props) => (props.name === 'hello' ? 'hotpink' : 'black')};
  width: 200px;
  margin: 0 auto;
  text-align: center;
`;

const App2 = () => {
  return (
    <ContentDiv>
      <ContentH2 name="hello">Content</ContentH2>
      <p>
        Lorem ipsum dolor sit amet consectetur adipisicing elit. Eos excepturi corrupti quo
        blanditiis! Adipisci amet corporis ipsum odio minima aliquid quisquam! Dignissimos natus
        laborum qui veritatis quaerat eaque! Nemo, ullam.
      </p>
    </ContentDiv>
  );
};

export default App2;

props
  • props๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์กฐ๊ฑด์— ๋”ฐ๋ผ ์Šคํƒ€์ผ์„ ์ ์šฉํ•œ๋‹ค.
  • ๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด ์•ˆ์— ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ฝ”๋“œ๋ฅผ ๋„ฃ์„ ๋•Œ ์ฒ˜๋Ÿผ ${ } ์•ˆ์— ์ฝ”๋“œ๋ฅผ ์ž…๋ ฅํ•œ๋‹ค.
  • ์‚ผํ•ญ์—ฐ์‚ฐ์ž๋ฅผ ์ด์šฉํ•ด props.name์˜ ์กฐ๊ฑด์ด ์ฐธ์ด๋ฉด hotpink ๊ธ€์ž์ƒ‰์„, ๊ฑฐ์ง“์ด๋ฉด black ๊ธ€์ž์ƒ‰์„ ์ถœ๋ ฅํ•œ๋‹ค.


ํ™•์žฅํ•ด์„œ ์‚ฌ์šฉํ•˜๊ธฐ

import React from 'react';
import styled from 'styled-components';

const ContentDiv = styled.div`
  margin: 40px;
  padding: 30px;
  border: solid 2px;
  background-color: aliceblue;
`;

const ContentH2 = styled.h2`
  color: ${(props) => (props.name === 'hello' ? 'hotpink' : 'black')};
  width: 200px;
  margin: 0 auto;
  text-align: center;
`;

const Content = styled(ContentH2)`
  border: 1px solid blue;
`;

const App2 = () => {
  return (
    <ContentDiv>
      <ContentH2 name="hello">Content</ContentH2>
      <p>
        Lorem ipsum dolor sit amet consectetur adipisicing elit. Eos excepturi corrupti quo
        blanditiis! Adipisci amet corporis ipsum odio minima aliquid quisquam! Dignissimos natus
        laborum qui veritatis quaerat eaque! Nemo, ullam.
      </p>
      <Content>
        <p>Lorem ipsum dolor, sit amet consectetur adipisicing elit.</p>
      </Content>
    </ContentDiv>
  );
};

export default App2;

ํ™•์žฅํ•˜๊ธฐ
  • ContentH2์˜ ์Šคํƒ€์ผ์„ Content๋กœ ํ™•์žฅํ•ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.




๐ŸŽจ ๋‹ค์–‘ํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ ๋ฒ„ํŠผ ๋งŒ๋“ค๊ธฐ

1. Global Style ์ ์šฉ

// App3.js
import React from 'react';
import { createGlobalStyle } from 'styled-components';
import { ButtonOne } from './components/ButtonOne';
import { ButtonTwo } from './components/ButtonTwo';
import { ButtonThree } from './components/ButtonThree';
import { ButtonFour } from './components/ButtonFour';

// ๊ธ€๋กœ๋ฒŒ ์Šคํƒ€์ผ ์ ์šฉ
const GlobalStyle = createGlobalStyle`
button {
  width: 200px;
  height: 80px;
  border-radius: 10px;
  margin: 30px;
  font-size: 20px;
}
`;

const App3 = () => {
  return (
    <>
      <GlobalStyle />
      <ButtonOne>Global / Styled</ButtonOne>
      <ButtonTwo>Global / Styled</ButtonTwo>
      <ButtonThree>Global / ํ™•์žฅ</ButtonThree>
      <ButtonFour name="Four">Global / props</ButtonFour>
    </>
  );
};

export default App3;

2. ๊ธฐ๋ณธ styled-components ์ ์šฉ

// ButtonOne.jsx
import styled from 'styled-components';

// ์ผ๋ฐ˜ styled-components ์Šคํƒ€์ผ ์ ์šฉ
export const ButtonOne = styled.button`
  background-color: gold;
`;
// ButtonTwo.jsx
import styled from 'styled-components';

// ์ผ๋ฐ˜ styled-components ์Šคํƒ€์ผ ์ ์šฉ
export const ButtonTwo = styled.button`
  background-color: lightgreen;
  color: white;
  box-shadow: 5px -5px greenyellow;
  font-weight: bold;
`;

3. ButtonTwo ํ™•์žฅํ•ด์„œ ButtonThree ์ ์šฉ

// ButtonTree.jsx
import styled from 'styled-components';
import { ButtonTwo } from './ButtonTwo';

// ํ™•์žฅํ•˜๊ธฐ
export const ButtonThree = styled(ButtonTwo)`
  border: 4px solid purple;
  border-radius: 50px;
  color: purple;
`;

4. props์— ๋”ฐ๋ฅธ ์กฐ๊ฑด๋ถ€ ์Šคํƒ€์ผ๋กœ ์ ์šฉ

// ButtonFour.jsx
import styled from 'styled-components';

// props์— ๋”ฐ๋ฅธ ์กฐ๊ฑด๋ถ€ ์Šคํƒ€์ผ
export const ButtonFour = styled.button`
  background-color: ${(props) => (props.name === 'Four' ? 'powderblue' : 'red')};
  border-radius: 50px 70px;
  border: 5px dotted yellow;
`;



๐ŸŽจ Module

1. ๊ธฐ๋ณธ ์ฝ”๋“œ

[ App.js ]

import React from 'react';
import './App.css';
import Question from './components/Question';

const App = () => {
  return (
    <>
      <nav className="box">
        <ul>
          <li id="detail" className="text">
            ์ƒ์„ธ์ •๋ณด
          </li>
          <li id="qa" className="text">
            Q&A
          </li>
          <li id="review" className="text">
            Review
          </li>
        </ul>
      </nav>
      <Question />
    </>
  );
};

export default App;

[ Question.jsx ]

import React from 'react';

const Question = () => {
  return (
    <div className="box">
      <h2 className="text">Q&A</h2>
      <p>
        Lorem ipsum dolor sit amet consectetur adipisicing elit. Eos excepturi corrupti quo
        blanditiis! Adipisci amet corporis ipsum odio minima aliquid quisquam! Dignissimos natus
        laborum qui veritatis quaerat eaque! Nemo, ullam.
      </p>
    </div>
  );
};

export default Question;


CSS
  1. ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ์—์„œ importํ•œ CSS๋Š” ์ž์‹๊นŒ์ง€ ์ž๋™์œผ๋กœ ์ ์šฉ๋œ๋‹ค.
  1. ์ž์‹ ์ปดํฌ๋„ŒํŠธ์—์„œ ์ƒˆ๋กœ CSS๋ฅผ ์ ์šฉํ•˜๊ฒŒ ๋˜๋ฉด ๋ถ€๋ชจ์˜ ์Šคํƒ€์ผ์€ ์ž์‹ ์ปดํฌ๋„ŒํŠธ์— import๋œ ์Šคํƒ€์ผ์ด ๋ฎ์–ด ์“ฐ์—ฌ์ง€๊ฒŒ ๋œ๋‹ค.

=> ๋”ฐ๋ผ์„œ ๋ฆฌ์•กํŠธ์—์„œ๋„ ๊ธฐ๋ณธ์ ์œผ๋กœ CSS ์„ ํƒ์ž ์šฐ์„ ์ˆœ์œ„๊ฐ€ ์ ์šฉ๋˜๋ฉฐ, ์ปดํฌ๋„ŒํŠธ์— ์Šคํƒ€์ผ์„ ์‚ฌ์šฉํ•˜๋ฉด ๊ฒฐ๊ตญ ๋‹ค๋ฅธ ์ปดํฌ๋„ŒํŠธ ์Šคํƒ€์ผ์—๋„ ์˜ํ–ฅ์„ ๋ฏธ์นœ๋‹ค.

์•„๋ž˜์— importํ•œ CSS๋ฅผ ์‚ดํŽด๋ณด์ž!

๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ App.js๊ฐ€ importํ•œ App.css ์Šคํƒ€์ผ์ด ์•„๋‹Œ,
์ž์‹ ์ปดํฌ๋„ŒํŠธ Question.jsx๊ฐ€ importํ•œ Question.css์˜ ์Šคํƒ€์ผ์ด ์ ์šฉ๋˜์—ˆ๋‹ค.


[ App.js ]

import "./App.css";

[ App.css ]

body {
  padding: 30px;
}

div {
  margin: 40px;
  background: rgb(214, 243, 255);
}
h2 {
  width: 200px;
  margin: 0 auto;
  text-align: center;
  color: rgb(206, 74, 131);
}

[ Question.jsx ]

import './Question.css';

[ Question.css ]

div {
  background: rgb(255, 220, 226);
  margin: 30px;
  padding: 30px;
}

h2 {
  width: 200px;
  margin: 0 auto;
  text-align: center;
  color: rgb(132, 196, 153);
}

p {
  font-weight: bold;
}


๊ทธ๋Ÿฐ๋ฐ ๋งŒ์•ฝ...

App.js์— ์ผ๋ฐ˜ ํƒœ๊ทธ๊ฐ€ ์•„๋‹ˆ๋ผ ํƒœ๊ทธ์— ํด๋ž˜์Šค ์ด๋ฆ„์„ ๋„ฃ์–ด์ฃผ๊ณ ,
App.css์—์„œ ํด๋ž˜์Šค ์ด๋ฆ„์„ ์„ ํƒํ•œ ๋’ค, ์Šคํƒ€์ผ์„ ์ ์šฉํ•˜๋ฉด ๊ณผ์—ฐ ์–ด๋–ป๊ฒŒ ๋ ๊นŒ?


2. ์ผ๋ฐ˜ CSS

[ App.js ]

import "./App.css";

[ App.css ]

body {
  padding: 30px;
}

.box {
  margin: 40px;
  background: rgb(214, 243, 255);
  padding: 20px;
}

.text {
  width: 200px;
  margin: 0 auto;
  text-align: center;
  color: rgb(206, 74, 131);
  font-weight: bold;
}

p {
  font-weight: bold;
  padding: 20px;
}


[ Question.jsx ]

import './Question.css';

[ Question.css ]

.box {
  background: rgb(255, 220, 226);
  margin: 30px;
  padding: 30px;
}

.text {
  width: 200px;
  margin: 0 auto;
  text-align: center;
  color: rgb(132, 196, 153);
}

p {
  font-weight: bold;
}


์ผ๋ฐ˜์ ์ธ CSS
  1. App.js์—๋งŒ ํด๋ž˜์Šค ์ด๋ฆ„์„ ์„ ํƒํ•œ CSS ์Šคํƒ€์ผ์„ ์ ์šฉํ•˜๋‹ˆ ํ•˜๋Š˜์ƒ‰ ๋ฐฐ๊ฒฝ์ด ์ž˜ ์ ์šฉ๋œ ๊ฑธ ๋ณผ ์ˆ˜์žˆ๋‹ค.
  1. ๊ทธ๋Ÿฐ๋ฐ ์—ญ์‹œ, ์ž์‹ ์ปดํฌ๋„ŒํŠธ Question.jsx์—๋„ ํด๋ž˜์Šค ์ด๋ฆ„์„ ์„ ํƒํ•œ CSS ์Šคํƒ€์ผ์„ ์ ์šฉํ•˜๋‹ˆ ๋ฐ”๋กœ ์ž์‹ ์ปดํฌ๋„ŒํŠธ CSS ์Šคํƒ€์ผ์ด ๋ฎ์–ด ์“ฐ์—ฌ์ง€๋ฉด์„œ ๋ถ„ํ™์ƒ‰ ๋ฐฐ๊ฒฝ์ด ์ ์šฉ๋˜์—ˆ๋‹ค.

=> ๋”ฐ๋ผ์„œ ํด๋ž˜์Šค๋ฅผ ํ†ตํ•ด ์šฐ์„ ์ˆœ์œ„๋ฅผ ๋†’์—ฌ๋„ ๊ฒฐ๊ตญ ์ปดํฌ๋„ŒํŠธ๋ผ๋ฆฌ ํด๋ž˜์Šค ์ด๋ฆ„์ด ์ค‘๋ณต๋˜๋ฉด ์†Œ์šฉ ์—†์–ด์ง„๋‹ค. ํŠนํžˆ ์ปดํฌ๋„ŒํŠธ๋กœ ๋ถ„๋ฆฌํ•ด ์ž‘์—…ํ•˜๋Š” ๋ฆฌ์•กํŠธ์˜ ํŠน์„ฑ์ƒ ํด๋ž˜์Šค ์ด๋ฆ„์ด ๊ฒน์น  ์œ„ํ—˜์„ฑ์€ ๋” ๋†’๋‹ค.

์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด CSS ํŒŒ์ผ ์ด๋ฆ„์— .css๋ฅผ module.css๋กœ ๋ฐ”๊ฟ”์ฃผ์ž!

module.css๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด class๋ช…์— ๊ณ ์œ ๊ฐ’์„ ์ถ”๊ฐ€ํ•ด๋„ ๊ฒน์น˜์ง€ ์•Š๋Š”๋‹ค!


3. Module.css

[ Question.jsx ]

import React from 'react';
import styles from './Question.module.css';

const Question = () => {
  return (
    <div className={styles.box}>
      <h2 className={styles.text}>Q&A</h2>
      <p>
        Lorem ipsum dolor sit amet consectetur adipisicing elit. Eos excepturi corrupti quo
        blanditiis! Adipisci amet corporis ipsum odio minima aliquid quisquam! Dignissimos natus
        laborum qui veritatis quaerat eaque! Nemo, ullam.
      </p>
    </div>
  );
};

export default Question;


[ App.js ]

import React from 'react';
import styles from './App.module.css';
import Question from './components/Question';

const App = () => {
  return (
    <>
      <nav className={styles.box}>
        <ul>
          <li id="detail" className={styles.text}>
            ์ƒ์„ธ์ •๋ณด
          </li>
          <li id="qa" className={styles.text}>
            Q&A
          </li>
          <li id="review" className={styles.text}>
            Review
          </li>
        </ul>
      </nav>
      <Question />
    </>
  );
};

export default App;


module ์ ์šฉ
  • Question.css ํŒŒ์ผ๋ช… โ†’ Question.module.css๋กœ ๋ฐ”๊ฟ”์ค€๋‹ค.
  • Question.jsx ํด๋ž˜์Šค๋ช… โ†’ {styles.ํด๋ž˜์Šค๋ช…}์œผ๋กœ ๋ฐ”๊ฟ”์ค€๋‹ค.
  • App.css ํŒŒ์ผ๋ช… โ†’ App.module.css๋กœ ๋ฐ”๊ฟ”์ค€๋‹ค.
  • App.js ํด๋ž˜์Šค๋ช… โ†’ {styles.ํด๋ž˜์Šค๋ช…}์œผ๋กœ ๋ฐ”๊ฟ”์ค€๋‹ค.

=> ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์ž๋™์œผ๋กœ ํด๋ž˜์Šค๋ช…์ด ๊ฒน์น˜์ง€ ์•Š๋„๋ก ์ฒ˜๋ฆฌํ•ด์ค€๋‹ค.

nav์™€ div ๋ชจ๋‘ box๋ผ๋Š” style์„ ๋ฐ›๊ณ  ์žˆ์ง€๋งŒ, ํด๋ž˜์Šค๋ช…์ด ๋‹ค๋ฅด๋‹ค!



4. Module.css ์ฃผ์˜์ 

  • ์ ์šฉํ•˜๊ณ ์ž ํ•˜๋Š” ์ปดํฌ๋„ŒํŠธ ์ด๋ฆ„๊ณผ ๋ฐ˜๋“œ์‹œ ์ผ์น˜์‹œ์ผœ์ค˜์•ผ ํ•œ๋‹ค.
Example.jsx ํŒŒ์ผ โ†’ Example.module.css ํŒŒ์ผ
  • module์—์„œ ํƒœ๊ทธ๋กœ ์ง€์ •ํ•ด์ฃผ๊ณ  ์‹ถ์„ ๋•Œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ ์–‘์‹์„ ์ ์šฉํ•ด์•ผ ํ•œ๋‹ค.
// Example.modeule.css ํŒŒ์ผ
.section h1 {
    font-size: 50px;
}

// Example.jsx ํŒŒ์ผ
<div className={styles.section}>
    <h1>hello world</h1>
    <p className={styles.contents}>hello world</p>
<div/>
profile
ํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ  ๋ฏฟ๋Š” ์‚ฌ๋žŒ์€ ๊ฒฐ๊ตญ ๊ทธ๋ ‡๊ฒŒ ๋œ๋‹ค ๐Ÿ˜„๐Ÿ˜Š

0๊ฐœ์˜ ๋Œ“๊ธ€