Global CSS

1. ๊ฐœ์š”

๋‚˜๋Š” ์Šคํƒ€์ผ ์ฝ”๋“œ์˜ ์ค‘๋ณต์„ ํ”ผํ•˜๊ธฐ ์œ„ํ•ด ReactJS๋กœ ๊ฐœ๋ฐœ์„ ํ•˜๋Š” ๊ฒฝ์šฐ styled-components๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•„์š”ํ•œ ์Šคํƒ€์ผ์„ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋งŒ๋“ค๊ณ  ๋งŒ๋“  ์Šคํƒ€์ผ ์ปดํฌ๋„ŒํŠธ๋ฅผ ํ•„์š”ํ•œ ๊ณณ์— importํ•˜์—ฌ ์‚ฌ์šฉํ•˜์˜€๋‹ค.

ReactJS, VueJS๊ฐ€ ์•„๋‹Œ Html, Css, Javasrcipt๋กœ๋งŒ ์›น ๊ฐœ๋ฐœ์„ ํ•˜๊ฒŒ ๋˜๋Š” ๊ฒฝ์šฐ ์Šคํƒ€์ผ๋ง์„ ์œ„ํ•ด *.css(์Šคํƒ€์ผ์‹œํŠธ)์„ ๋งŒ๋“ค๊ณ  Link style๋ฐฉ๋ฒ•์„ ํ†ตํ•ด *.html๊ณผ *.css(์Šคํƒ€์ผ์‹œํŠธ)๋ฅผ ์—ฐ๊ฒฐํ•œ๋‹ค.

๋‚ด๊ฐ€ ์ƒ๊ฐํ•˜๋Š” ์œ„์™€ ๊ฐ™์€ ๋ฐฉ๋ฒ•์˜ ๋‹จ์ ์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

  • ๋‹จ์ 

    1. ๊ฐ*.htmlํŒŒ์ผ์— ๋ชจ๋‘ ๋‹ค๋ฅธ *.css(์Šคํƒ€์ผ์‹œํŠธ)์„ ์—ฐ๊ฒฐํ•˜๋Š” ๊ฒฝ์šฐ ๊ฐ™์€ ์Šคํƒ€์ผ์˜ ์ฝ”๋“œ๋ฅผ ์ค‘๋ณตํ•˜์—ฌ ์ž‘์„ฑํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์ƒ๊ธธ ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ๋ฅผ๋“ค์–ด ๊ฐ™์€ ์Šคํƒ€์ผ์˜ ๋ฒ„ํŠผ์ด ์žˆ๊ณ  ์ด๋ฅผ index.html๊ณผ profile.html์—์„œ ํ•„์š”ํ•˜๋‹ค๋ฉด index.css์™€ profile.css์— ๊ฐ™์€ ์Šคํƒ€์ผ์˜ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด์•ผ ํ•œ๋‹ค. ์ด๋Ÿฌํ•œ ๋ฐฉ๋ฒ•์€ ๋ฒ„ํŠผ ์Šคํƒ€์ผ์„ ์ˆ˜์ •์„ ํ•ด์•ผํ•˜๋Š” ๊ฒฝ์šฐ ํ•ด๋‹น ์Šคํƒ€์ผ์˜ ์ฝ”๋“œ๊ฐ€ ์‚ฌ์šฉ๋œ ๋ชจ๋“  *.css(์Šคํƒ€์ผ์‹œํŠธ)๋ฅผ ์ฐพ์•„ ํ•˜๋‚˜ํ•˜๋‚˜ ์ˆ˜์ •ํ•ด์•ผ ํ•œ๋‹ค.

      <!-- index.html -->
      ...
      <head>
        <link rel="stylesheet" href="index.css" />
      </head>
      ...
      <!-- profile.html -->
      ...
      <head>
        <link rel="stylesheet" href="profile.css" />
      </head>
      ...
      /* index.css */
      .button {
        color: red;
      }
      /* profile.css */
      .button {
        color: red;
      }
    2. button.css, container.css, navbar.css ๋“ฑ ๊ฐ์ž ๋งก๊ณ  ์žˆ๋Š” ์—ญํ• ์˜ *.css(์Šคํƒ€์ผ์‹œํŠธ)์„ ๋งŒ๋“ค๋ฉด ์œ„์˜ ๋‹จ์ ์€ ํ•ด์†Œ๊ฐ€ ๋˜๋‚˜ ๊ฐ*.html์—์„œ ์ž‘์„ฑํ•ด์•ผ ํ•˜๋Š” <link />ํƒœ๊ทธ๊ฐ€ ๋งŽ์ด์ง„๋‹ค. ๋˜ํ•œ ์ƒˆ๋กœ์šด ์Šคํƒ€์ผ์˜ ๋ฌด์–ธ๊ฐ€๋ฅผ ์ถ”๊ฐ€ํ•˜๊ฒŒ ๋œ๋‹ค๋ฉด ํ•ด๋‹น ์Šคํƒ€์ผ์„ ์‚ฌ์šฉํ•˜๋Š” *.html์„ ๋ชจ๋‘ ์ฐพ์•„ ์ƒˆ๋กœ์šด <link />ํƒœ๊ทธ๋ฅผ ์ž‘์„ฑํ•ด์•ผ ํ•œ๋‹ค.

      /* button.css */
      div {
        color: red;
        ...;
      }
      /* container.css */
      div {
        color: red;
        ...;
      }
      /* navbar.css */
      div {
        color: red;
        ...;
      }
      <!-- index.html -->
      ...
      <head>
        <link rel="stylesheet" href="button.css" />
        <link rel="stylesheet" href="container.css" />
        <link rel="stylesheet" href="navbar.css" />
      </head>
      ...
      <!-- profile.html -->
      ...
      <head>
        <link rel="stylesheet" href="button.css" />
        <link rel="stylesheet" href="container.css" />
        <link rel="stylesheet" href="navbar.css" />
      </head>
      ...

์ด๋Ÿฌํ•œ ๋‹จ์ ์„ ํ•ด๊ฒฐํ•˜๊ณ ์ž ๋ชจ๋“  *.html์— ์—ฐ๊ฒฐํ•˜๋Š” ์˜ˆ๋ฅผ๋“ค๋ฉด global.css ๋งŒ๋“ค๊ณ  ์—ฌ๊ธฐ์— ๋ชจ๋“  *.css(์Šคํƒ€์ผ์‹œํŠธ)์„ @importํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์†Œ๊ฐœํ•œ๋‹ค.


2. global.css

.cssํŒŒ์ผ ์ด๋ฆ„์€ styles, global ๋“ฑ ์›ํ•˜๋Š” ์ด๋ฆ„์œผ๋กœ ์ž‘์„ฑํ•ด๋„ ๋˜์ง€๋งŒ ๋‚˜๋Š” ๋ชจ๋“  *.html์—์„œ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ „์—ญ์ด๋ผ๋Š” ์˜๋ฏธ๋กœ global๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

css ํด๋”๋ฅผ ๋งŒ๋“ค๊ณ  global.css๋ฅผ ๋งŒ๋“ ๋‹ค.

/* css/global.css */
body {
  font-size: 16px;
  color: rgba(20, 20, 20, 1);
}
...

3. @import

@import CSS ๊ทœ์น™์€ ๋‹ค๋ฅธ ์Šคํƒ€์ผ ์‹œํŠธ์—์„œ ์Šคํƒ€์ผ ๊ทœ์น™์„ ๊ฐ€์ ธ์˜ฌ ๋•Œ ์“ฐ์ž„

์‚ฌ์šฉ๋ฒ•

  • @import url;

url: importํ•  ์ž์›์˜ ์œ„์น˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋ฌธ์ž์—ด ๋˜๋Š” url

ํ•„์š”ํ•œ *.css(์Šคํƒ€์ผ์‹œํŠธ)๋ฅผ ๋งŒ๋“ค๊ณ  global.css์— importํ•œ๋‹ค.

/* button.css */
div {
  color: red;
  ...;
}
/* container.css */
div {
  color: red;
  ...;
}
/* navbar.css */
div {
  color: red;
  ...;
}
/* css/global.css */
@import "button.css";
@import "container.css";
@import "navbar.css";

body {
  font-size: 16px;
  color: rgba(20, 20, 20, 1);
}
...

์ดํ›„ ๋ชจ๋“  *.html์— <link rel="stylesheet" href="css/global.css" />๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค. *.html์˜ ์œ„์น˜์— ๋”ฐ๋ฅธ ๊ฒฝ๋กœ์ž‘์„ฑ์€ ํ•„์ˆ˜

<!-- index.html -->
...
<head>
  <link rel="stylesheet" href="css/global.css" />
</head>
...

4. Reset css

๋ชจ๋“  ์›น ๋ธŒ๋ผ์šฐ์ €๋Š” ๋””ํดํŠธ ์Šคํƒ€์ผ์„ ๊ฐ€์ง€๊ณ  ์žˆ์–ด CSS๊ฐ€ ์—†์–ด๋„ ์ž‘๋™ํ•จ. ํ•˜์ง€๋งŒ ์›น ๋ธŒ๋ผ์šฐ์ €์— ๋”ฐ๋ผ ๋””ํดํŠธ ์Šคํƒ€์ผ์ด ๋‹ค๋ฅด๊ณ  ์ง€์›ํ•˜๋Š” tag๋‚˜ style๋„ ์ œ๊ฐ๊ฐ์ด์„œ ์ฃผ์˜๊ฐ€ ํ•„์š”

CSS Reset๋Š” ๋ชจ๋“  ํƒœ๊ทธ์— ์ ์šฉ๋œ ์„œ์‹์— margin๊ณผ padding์„ ์ œ๊ฑฐํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์‹œ์ž‘๋˜์–ด ๋ธŒ๋ผ์šฐ์ €์˜ ๊ธฐ๋ณธ์š”์†Œ์˜ ๋””์ž์ธ์„ ๋ชจ๋‘ ์—†์• ์ž๋Š” ๊ฒƒ์œผ๋กœ ๋ฐœ์ „, ์ด๋ ‡๊ฒŒ ๋ชจ๋‘ 0์œผ๋กœ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์„ ํ†ตํ•ด์„œ ๋ธŒ๋ผ์šฐ์ €๋“ค์˜ ์„œ์‹์„ ํ•˜๋‚˜๋กœ ํ†ต์ผ

๊ฐ€์žฅ ์œ ๋ช…ํ•œ ๊ฒƒ์€ ์—๋ฆญ๋งˆ์ด์–ด์˜ CSS Reset

Reset.css ์ ์šฉํ•˜๊ธฐ

/* reset.css */
html,
body,
div,
span,
applet,
object,
iframe,
h1,
h2,
h3,
h4,
h5,
h6,
p,
blockquote,
pre,
a,
abbr,
acronym,
address,
big,
cite,
code,
del,
dfn,
em,
img,
ins,
kbd,
q,
s,
samp,
small,
strike,
strong,
sub,
sup,
tt,
var,
b,
u,
i,
center,
dl,
dt,
dd,
ol,
ul,
li,
fieldset,
form,
label,
legend,
table,
caption,
tbody,
tfoot,
thead,
tr,
th,
td,
article,
aside,
canvas,
details,
embed,
figure,
figcaption,
footer,
header,
hgroup,
menu,
nav,
output,
ruby,
section,
summary,
time,
mark,
audio,
video {
  margin: 0;
  padding: 0;
  border: 0;
  font-size: 100%;
  font: inherit;
  vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */
article,
aside,
details,
figcaption,
figure,
footer,
header,
hgroup,
menu,
nav,
section {
  display: block;
}
body {
  line-height: 1;
}
ol,
ul {
  list-style: none;
}
blockquote,
q {
  quotes: none;
}
blockquote:before,
blockquote:after,
q:before,
q:after {
  content: "";
  content: none;
}
table {
  border-collapse: collapse;
  border-spacing: 0;
}
/* css/global.css */
@import "button.css";
@import "container.css";
@import "navbar.css";
@import "reset.css";

body {
  font-size: 16px;
  color: rgba(20, 20, 20, 1);
}
...

css reset์˜ ์ •๋‹ต์€ ์—†๋‹ค๊ณ  ํ•œ๋‹ค. ๋‚ด๊ฐ€ ๋งŒ๋“ค ์‚ฌ์ดํŠธ์— ํ•„์š”ํ•œ reset๋งŒ ์ ์šฉํ•ด์„œ ๋งŒ๋“ค์–ด๋„ ๋˜๊ณ  ์œ„์™€ ๊ฐ™์ด reset.css๋ฅผ importํ•œ ๋’ค ์ถ”๊ฐ€๋กœ reset์ด ํ•„์š”ํ•œ ์Šคํƒ€์ผ์„ global.css์— ์ž‘์„ฑํ•ด๋„ ๋œ๋‹ค.


5. ๋ฒˆ๊ฑฐ๋กœ์šด ์ 

global.css๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋ฒˆ๊ฑฐ๋กœ์šด ์ ์ด๋ผ๊ธฐ ๋ณด๋‹ค๋Š” html, css, javascript๋กœ๋งŒ ์ด์šฉํ•ด์„œ ์›น์„ ๋งŒ๋“ค ๋•Œ์˜ ๋ฒˆ๊ฑฐ๋กœ์šด ์ ์€ ๋งŒ์•ฝ ์ œ๋Œ€๋กœ ๋œ ๊ธฐํš, ๋””์ž์ธ ์—†์ด ๋ฐ”๋กœ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋ฉด์„œ ์›น์„ ๋งŒ๋“ค๋‹ค ๋ณด๋ฉด ์ค‘๊ฐ„์ค‘๊ฐ„์— class, id๋ฅผ ์ˆ˜์ •, ์ถ”๊ฐ€ํ•˜์—ฌ ์ž‘์„ฑํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๊ฐ„ํ˜น ์ƒ๊ธด๋‹ค. ๊ธฐํš, ๋””์ž์ธ์ด ์—†๋”๋ผ๋„ *.html์„ ์ž‘์„ฑํ•  ๋•Œ ๋ฏธ๋ฆฌ ๋””์ž์ธ ๊นŒ์ง€ ์ƒ๊ฐํ•˜๋ฉฐ class, id๋ฅผ ๋ถ€์—ฌํ•œ๋‹ค๋ฉด ์–ด๋Š์ •๋„๋Š” ๊ดœ์ฐฎ์„์ง€ ๋ชฐ๋ผ๋„ class, id๋ฅผ ๋‚˜์ค‘์— ๋””์ž์ธ์„ ํ•  ๋•Œ ๋งŒ๋“ค๊ฒŒ ๋œ๋‹ค๋ฉด ๋ชจ๋“  *.html๋ฅผ ๋‹ค์‹œ ์—ด์–ด์„œ ์ผ์ผํžˆ class, id๋ฅผ ๋ถ€์—ฌํ•ด์•ผํ•˜๋Š” ๋ฒˆ๊ฑฐ๋กœ์›€์ด ์ƒ๊ธด๋‹ค.

๋ฌผ๋ก  ReactJS๋กœ ๊ฐœ๋ฐœ์„ ํ•˜๋Š” ๊ฒฝ์šฐ์—๋„ ์‚ฌ์ „ ๊ธฐํš, ๋””์ž์ธ์ด ์—†๋‹ค๋ฉด ์ฝ”๋“œ์™€ ์ปดํฌ๋„ŒํŠธ๋Š” ๋‚œ์žฅํŒ์ด ๋œ๋‹ค. ์ด๊ฑด ๊ฒฝํ—˜๋‹ด... ํ”ผ๊ทธ๋งˆ๋กœ ๊ธฐํš ๋ฐ ๋””์ž์ธํ•˜๋Š” ๊ณต๋ถ€, ์—ฐ์Šต๋„ ํ•˜์ž๐Ÿ˜€


์ฐธ๊ณ 

poiemaweb 2-1 CSS ๊ธฐ๋ณธ๋ฌธ๋ฒ• mdn @import 2022 CSS Reset ๋‹ค์‹œ ์จ๋ณด๊ธฐ!


๐Ÿ‘†

๐Ÿ“… 2022-07-11

Last updated