1. React ์ CSS ์ ์ฉ
๐บ ๋ฆฌ์กํธ์ CSS ์ ์ฉํ๋ ๋ฐฉ๋ฒ์ ์์๋ณด์!
๐ css ํ์ผ ์ด๋ฆ ๊ท์น
์ปดํฌ๋ํธ์ ์ด๋ฆ์ ๋ฐ๋์ ํฌํจํด์ ๋ง๋ฌ
EX) App.js - App.css
๐ ํ๊ทธ์ ์ง์ ์ง์ ํ๊ธฐ
- ํ๊ทธ์ ์ง์ ๋์์ธ์ ์ ์ฉ ํ ๋๋ {}๋ก ๋ฌถ์ด์ค๋๋ค
- css์์ฑ์ค - ์ ์นด๋ฉํ๊ธฐ๋ฒ์ผ๋ก ๋์ฒด๋ฉ๋๋ค.
style={{css์์ฑ: ๊ฐ, css์์ฑ: ๊ฐ}}
<p style={{color: 'white', textAlign: 'center'}}>์๋
ํ์ธ์!!</p>
๐ ์ผ๋ฐ cssํ์ผ๋ก ์ ์ฉํ๊ธฐ
- ์ผ๋ฐCSS๋ฌธ๋ฒ์ผ๋ก ๋์์ธ์ ํด์ฃผ๋ฉด ๋๊ณ import๋ก ๊ฐ์ ธ์ค๋ฉด ๋ฉ๋๋ค.
/* App.css */ .app_header { height: 50px; line-height: 50px; background-color: #000; margin: 0; padding: 0; }
//App.js import './css/App.css'; //์ผ๋ฐcssํ์ผ๋ก ๋์์ธ ......์๋ต
๐ styled-compenet css๋ชจ๋๋ก ์ ์ฉํ๊ธฐ
- ํ์ผ์ ์ปดํฌ๋ํธ๋ช .module.css ํ์์ผ๋ก ๋ง๋ญ๋๋ค.
- cssํ์ผ์ import๊ตฌ๋ฌธ์ผ๋ก ๊ฐ์ ธ์ต๋๋ค.
- ์ด ๋ฐฉ์์ ์ ํ์์ ๊ณ ์ ํ ํด์๊ฐ์ ๋ถ์ฌํจ์ผ๋ก ๋ค๋ฅธํ์ผ๊ณผ ๋์์ธ์ ์ค๋ณต์ ๋ง์์ค๋๋ค.
- :global ํค์๋๋ฅผ ์ด์ฉํด์ ์ ์ญ ์ ํ์(์ด๋ฆ) ์ ์ธ์ด ๊ฐ๋ฅํฉ๋๋ค.
/* App.module.css ํ์ผ */ .app_wrap { display: flex; flex-direction: row; justify-content: center; align-items: center; text-align: center; background-color: #fff; height: 100vh; margin: 0; padding: 0; } /* ์ ์ญ์ ํ์๋ก ์ด๋ฆ์ ํ๊ธฐ */:global .title { color: pink; font-weight: 900; }
App.js ํ์ผ
- cssํ์ผ์ ํน์ ์ด๋ฆ์ผ๋ก importํฉ๋๋ค.
- style={ํน์ ์ด๋ฆ.์ ํ์ ๋ก} ์ ์ฉํฉ๋๋ค.
- :global ํค์๋๋ ์ด๋ฆ์ผ๋ก ๋ฐ๋ก ์ฌ์ฉํ๋ฉด ๋ฉ๋๋ค.
//**App.js ํ์ผ** import styled from './css/App.module.css';//css๋ชจ๋๋ก ๋์์ธconst App = () => { return ( <Fragment> <section className={styled.app_wrap}> {/* css๋ชจ๋ app_wrap์ ์ฉ */} <p className="title">CSS๋ชจ๋๋ก๋์์ธ!</p> {/* css๋ชจ๋์ ์ ์ญ์ ํ์ ์ ์ฉ */} </section> </Fragment> ) } export default App;
๐ publicํด๋์ css๋์์ธ
[App.js]
import './App.css'; //cssํ์ผ import import styled from './App.module.css'; //module.css function App() { //1. cssํ์ผ๋ช
์ ์ปดํฌ๋ํธ์ ๋์ผํ ์ด๋ฆ์ผ๋ก ์์ฑ return ( <> <div className="app-title"> cssํ์ผ๋ก ๋์์ธํ๊ธฐ </div> <div style={{color: 'blue', border: "1px solid #777"}}> ์ง์ ๋์์ธํ๊ธฐ </div> <div className={styled.app_wrap}> <p className={styled.title}>๋ชจ๋๋ก ๋์์ธํ๊ธฐ</p> <p className="content">global์์ฑ</p> </div> </> ) } export default App;
[App.css]
.app-title { border : 1px solid red; color : red; }
[App.module.css]
.app_wrap { border : 1px solid green; color : green; } .title { border : 1px solid #41664b; color : #41664b; } /* ์ ์ญ์ ํ์๋ก ์ด๋ฆ์ ํ๊ธฐ */:global .content { color: pink; font-weight: 900; }
