๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
  • ์žฅ์›์ต ๊ธฐ์ˆ ๋ธ”๋กœ๊ทธ
๐Ÿ“บ Front End/- React, Next.js

[Styled-Components] styled-reset ํŒจํ‚ค์ง€๋กœ styled-components resetํ•˜๊ธฐ

by Wonit 2020. 10. 7.

ํ•ด๋‹น ๋ธ”๋กœ๊ทธ์— css๋ฅผ ์ดˆ๊ธฐํ™” ํ•ด์•ผํ•˜๋Š” ์ด์œ ์— ๋Œ€ํ•ด์„œ๋Š” CSS ์ž‘์—…์‹œ์— ๊ผญ ๊ฑฐ์ณ์•ผํ•˜๋Š” ํ•„์ˆ˜ ๊ณผ์ • ์—์„œ ๋‹ค๋ค˜๋‹ค.

 

์ด์ œ ์˜ค๋Š˜์€ React Styled-Components์—์„œ๋„ CSS-Reset์„ ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด์ž.

Our Goals

์ตœ์ข…์ ์œผ๋กœ ์šฐ๋ฆฌ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ™”๋ฉด์„ ๊ตฌ์„ฑํ•  ๊ฒƒ์ด๋‹ค.

๊ธฐ๋ณธ์ ์œผ๋กœ ์ƒ๊ธฐ๋Š” margin๊ณผ padding์— ์˜ํ•ด ์ƒ๊ฒจ๋‚œ ์—ฌ๊ธฐ ๋นจ๊ฐ„ ๊ฐ„๊ฒฉ๋“ค์„ ์—†์•จ ๊ฒƒ์ด๋‹ค.

๊ทธ๋ฆฌ๊ณ  ์ด ์ž‘์—…์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ณผ์ •์œผ๋กœ ์ˆ˜ํ–‰๋œ๋‹ค.

  • styled-components ์„ค์น˜ํ•˜๊ธฐ
  • styled-reset ํŒจํ‚ค์ง€ ์„ค์น˜ํ•˜๊ธฐ
  • ๊ธฐ๋ณธ์ ์ธ ์ปดํฌ๋„ŒํŠธ ์ƒ์„ฑํ•˜๊ธฐ
  • GlobalStyles.js ์ƒ์„ฑํ•˜๊ธฐ
  • App.js์— GlobalStyles import ํ•˜๊ธฐ

 

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

์‚ฌ์‹ค ์ด ๊ฒ€์ƒ‰์„ ํ†ตํ•ด ๋ธ”๋กœ๊ทธ๋กœ ๋“ค์–ด์˜จ ์‚ฌ๋žŒ์€ ๊ฑฐ์˜ ์ค€๋น„๊ฐ€ ๋˜์žˆ๊ฒ ์ง€๋งŒ, ํ˜น์‹œ ๋ชจ๋ฅผ ๋…์ž๋ฅผ ์œ„ํ•ด ์„ค๋ช…ํ•˜๊ฒ ๋‹ค.

 

styled-components๊ฐ€ ์žˆ์–ด์•ผ css-reset์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ styled-components ํŒจํ‚ค์ง€๋ฅผ ์„ค์น˜ํ•˜์ž.

 

styled-reset ํŒจํ‚ค์ง€ ์„ค์น˜ํ•˜๊ธฐ

์ด์ œ styled-components์˜ css reset๋ฅผ ์œ„ํ•ด styled-reset ํŒจํ‚ค์ง€๋ฅผ ์„ค์น˜ํ•˜์ž

๋ณด์ž.

export const App = () => {
  return (
    <>
      <RootContainer>
        <TextField>Box1</TextField>
        <TextField>Box2</TextField>
      </RootContainer>
    </>
  );
}

const RootContainer = styled.div`
  display: flex;
  flex-direction: row;
`;

const TextField = styled.span`
  display: flex;
  width: 150px;
  height: 150px;
  background-color: wheat;
  justify-content: center;
  align-items: center;
`;

export default App;

 

GlobalStyles.js ์ƒ์„ฑํ•˜๊ธฐ

styled-components๋Š” ๊ฐ๊ธฐ ๋‹ค๋ฅธ ์ปดํฌ๋„ŒํŠธ๋“ค์—๊ฒŒ ์Šคํƒ€์ผ๋ง์˜ ์˜ํ–ฅ์„ ์ฃผ์ง€ ์•Š๊ธฐ ์œ„ํ•ด Local๋กœ ๋™์ž‘ํ•˜๋Š”๋ฐ, ์šฐ๋ฆฌ๋Š” ๋ชจ๋“  HTML Elements, Components์— ๋™์ผํ•˜๊ฒŒ ์ดˆ๊ธฐํ™” ์ž‘์—…์ด ์ ์šฉ๋˜์–ด์•ผ ํ•˜๋ฏ€๋กœ Global์„ค์ •์„ ํ•  ์ˆ˜ ์žˆ๋Š” GlobalStyles.js๋ผ๋Š” ํŒŒ์ผ์„ ์ƒ์„ฑํ•  ๊ฒƒ์ด๋‹ค.

์ด ๋ถ€๋ถ„์ด ์•„๋งˆ ๊ฐ€์žฅ ์ค‘์š”ํ•œ ํ•ต์‹ฌ์ด์ง€ ์•Š์„๊นŒ ์‹ถ๋‹ค

import { createGlobalStyle } from "styled-components";
import reset from "styled-reset";  // style-reset ํŒจํ‚ค์ง€

const GlobalStyles = createGlobalStyle` 
    ${reset}
    a{
        text-decoration: none;
        color: inherit;
    }
    *{
        box-sizing: border-box;
    }
    body {
        font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
        font-size: 14px;
        background-color: rgba(20, 20, 20, 1);
        color: white;
        padding-top: 50px;
    }
`;

export default GlobalStyles;

์—ฌ๊ธฐ์„œ createGlobalStyle์„ styled-componenst์—์„œ ๊ฐ€์ ธ์˜ค๊ฒŒ ๋˜๋Š”๋ฐ, ์•„๊นŒ ์œ„์—์„œ ๋ง ํ–ˆ๋˜ Local์„ค์ •์„ Global์„ค์ •์œผ๋กœ ์ ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•˜๋Š” ์š”์†Œ์ด๋‹ค.

 

App.js์— GlobalStyles import ํ•˜๊ธฐ

import GlobalStyles from "./GlobalStyles";

export const App = () => {
  return (
    <>
      <GlobalStyles />
      <RootContainer>
        <TextField>Box1</TextField>
        <TextField>Box2</TextField>
      </RootContainer>
    </>
  );
}

const RootContainer = styled.div`
  display: flex;
  flex-direction: row;
`;

const TextField = styled.span`
  display: flex;
  width: 150px;
  height: 150px;
  background-color: wheat;
  justify-content: center;
  align-items: center;
`;

export default App;

 

๊ทธ๋Ÿผ ๋ชจ๋“  ๋ฌธ์ œ๊ฐ€ ํ•ด๊ฒฐ๋˜๊ณ  ์šฐ๋ฆฌ๊ฐ€ ์˜ˆ์ƒํ•œ ๋Œ€๋กœ CSS-Reset์„ React Styled-Components์—์„œ ๋™์ž‘ํ•˜๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

๋Œ“๊ธ€