style={{ css 내용 }} 적용export default function Home() {
return (
<div style={{ color: 'red' }}>
Hi
</div>)
}
.module.css 파일 적용className을 string 이 아닌 객체의 프로퍼티 형식으로 작성해야 한다.
_app.js 파일 외 모든 파일에서 일반 .css 파일은 불러올 수 없으므로, .module.css 파일을 import 해서 사용해야 한다. // /pages/index.js
import styles from '@/styles/Home.module.css'
export default function Home() {
return (
<div className={styles.home}>
Hi
</div>)
}
/* /styles/home.module.css */
.home {
color: blue;
}
<style jsx>{ css 내용 }</style><style> 태그 안에 jsx props를 넣고 태그 사이에 {} 대괄호와 `백틱 을 넣고 그 사이에 css 내용을 작성할 수 있다.
export default function Home() {
return (
<div className='home'>
Hi
<style jsx>{`
.home {
color: blue
}
`}</style>
</div>)
}
.module.css 와 같이 해당 파일 안에서만 css style이 적용되기 때문에 서로 다른 파일에서 같은 class name을 사용하더라도 덮어씌워지지 않는다.
다른 파일에도 영향을 끼치도록 css를 적용시키려는 경우 global props를 추가한다.
// /pages/home.js
import HomeComponents from '@/components/HomeComponents'
export default function Home() {
return (
<div>
<span>Home</span>
<HomeComponents />
<style jsx global>{` // HomeComponents 의 span 태그에도 적용된다.
span {
color: blue
}
`}</style>
</div>)
}
하지만 /home 이아닌 다른 /about 페이지(/pages/about.js) 에서는 다른 페이지 이므로 style 이 적용되지 않는다.
페이지에 상관없이 전역 스타일을 적용시키려는 경우 .css 파일을 /pages/_app.js 에서 import 한다
// /pages/_app.js
import '@/styles/reset.css'
export default function App({ Component, pageProps }) {
return (
<>
<Component {...pageProps} />
</>
)
}
/* /styles/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;
}