๐ญ CSS ์ ํธ๋ฆฌํฐ ํด๋์ค ๋จ์ฉ: ์คํ์ผ ์ฌ์ฌ์ฉ์ฑ ์ ํ์ HTML ๋น๋ํ
Summary
CSS ์ ํธ๋ฆฌํฐ ํด๋์ค๋ฅผ ๋จ์ฉํ๋ฉด HTML ๊ตฌ์กฐ๊ฐ ๋ณต์กํด์ง๊ณ ์คํ์ผ ์ฌ์ฌ์ฉ์ฑ์ด ์ ํ๋ฉ๋๋ค. ์ปดํฌ๋ํธ ๊ธฐ๋ฐ ์คํ์ผ๋ง, CSS ๋ณ์ ํ์ฉ, ๊ทธ๋ฆฌ๊ณ ์ ์ ํ ์ ํธ๋ฆฌํฐ ํด๋์ค ์ฌ์ฉ์ผ๋ก ์คํ์ผ์ ํจ์จ์ ์ผ๋ก ๊ด๋ฆฌํด์ผ ํฉ๋๋ค.
Why Wrong?
CSS ์ ํธ๋ฆฌํฐ ํด๋์ค๋ ๊ฐ๊ฒฐํ๊ณ ์ฌ์ฌ์ฉ ๊ฐ๋ฅํ ์คํ์ผ์ ์ ๊ณตํ์ง๋ง, ๊ณผ๋ํ๊ฒ ์ฌ์ฉํ๋ฉด HTML ๊ตฌ์กฐ๊ฐ ๋ณต์กํด์ง๊ณ , ์คํ์ผ ์ฌ์ฌ์ฉ์ฑ์ด ๋จ์ด์ง๋ฉฐ, ํ ๋ง ๋ณ๊ฒฝ ๋ฐ ์ ์ง๋ณด์๊ฐ ์ด๋ ค์์ง๋๋ค. ํนํ tailwindcss, bootstrap๊ณผ ๊ฐ์ css ๋ผ์ด๋ธ๋ฌ๋ฆฌ, ํ๋ ์์ํฌ์์ ๋ง์ด ๋ฐ์ํฉ๋๋ค. ์ปดํฌ๋ํธ์ ์คํ์ผ์ด HTML์ ์ง์ ์ ์ผ๋ก ๊ฒฐํฉ๋์ด, ์คํ์ผ ๋ณ๊ฒฝ ์ HTML์ ์์ ํด์ผ ํ๋ ์ํฉ์ด ๋ฐ์ํ ์ ์์ต๋๋ค. ๋ํ, ์ฌ๋ฌ ๊ฐ์ ์ ํธ๋ฆฌํฐ ํด๋์ค๋ฅผ ์กฐํฉํ์ฌ ์คํ์ผ์ ์ ์ฉํ๋ฉด ์ฝ๋์ ๊ฐ๋ ์ฑ์ด ๋จ์ด์ง๊ณ , ์คํ์ผ ์์ฑ ์ถฉ๋์ด ๋ฐ์ํ ๊ฐ๋ฅ์ฑ์ด ๋์์ง๋๋ค.
How to Fix?
CSS ์ ํธ๋ฆฌํฐ ํด๋์ค๋ฅผ ์ ์ ํ ์ฌ์ฉํ๊ณ , ์ปดํฌ๋ํธ ๊ธฐ๋ฐ ์คํ์ผ๋ง ๋ฐฉ์์ ๋์ ํ์ฌ ์คํ์ผ ์ฌ์ฌ์ฉ์ฑ์ ๋์ฌ์ผ ํฉ๋๋ค. ์์ฃผ ์ฌ์ฉ๋๋ ์คํ์ผ ์กฐํฉ์ ์ปค์คํ CSS ํด๋์ค๋ CSS-in-JS ์ปดํฌ๋ํธ๋ก ์ถ์ถํ์ฌ ์ฌ์ฌ์ฉ์ฑ์ ๋์ด๊ณ , HTML ๊ตฌ์กฐ๋ฅผ ๊ฐ๊ฒฐํ๊ฒ ์ ์งํด์ผ ํฉ๋๋ค. ๋ํ, ํ ๋ง ๋ณ๊ฒฝ ๋ฐ ์ ์ง๋ณด์๋ฅผ ์ฉ์ดํ๊ฒ ํ๊ธฐ ์ํด CSS ๋ณ์(Custom Properties)๋ฅผ ์ ๊ทน์ ์ผ๋ก ํ์ฉํด์ผ ํฉ๋๋ค.tailwindcss, bootstrap๊ณผ ๊ฐ์ css ๋ผ์ด๋ธ๋ฌ๋ฆฌ, ํ๋ ์์ํฌ์ ํ ๋ง ์ปค์คํ ๊ธฐ๋ฅ์ ์ ๊ทน ํ์ฉํฉ๋๋ค.
Before Code (Bad)
// ๋ฌธ์ ๊ฐ ์๋ ์ฝ๋
<button className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded">
Submit
</button>
<div className="w-full max-w-sm bg-white rounded-lg border border-gray-200 shadow-md">
<div className="flex justify-end px-4 pt-4">
<button id="dropdownButton" data-dropdown-toggle="dropdown" className="inline-block text-gray-500 dark:bg-gray-800 dark:hover:bg-gray-700 dark:hover:text-white" >
<svg className="w-5 h-5" aria-hidden="true" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg">
<path d="M6 10a2 2 0 11-4 0 2 2 0 014 0zM12 10a2 2 0 11-4 0 2 2 0 014 0zM16 12a2 2 0 100-4 2 2 0 000 4z"/>
</svg>
</button>
</div>
<div className="flex flex-col items-center pb-10">
<img className="mb-3 w-24 h-24 rounded-full shadow-lg" src="/docs/images/people/profile-picture-3.jpg" alt="Bonnie image"/>
<h5 className="mb-1 text-xl font-medium text-gray-900 dark:text-white">Bonnie Green</h5>
<span className="text-sm text-gray-500 dark:text-gray-400">Visual Designer</span>
<div className="flex mt-4 space-x-3 md:mt-6">
<a href="#" className="inline-flex items-center py-2 px-4 text-sm font-medium text-center text-white bg-blue-700 rounded-lg hover:bg-blue-800 focus:ring-4 focus:outline-none focus:ring-blue-300 dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800">Add friend</a>
<a href="#" className="inline-flex items-center py-2 px-4 text-sm font-medium text-center text-gray-900 bg-white rounded-lg border border-gray-300 hover:bg-gray-100 focus:ring-4 focus:outline-none focus:ring-gray-200 dark:bg-gray-800 dark:text-white dark:border-gray-600 dark:hover:bg-gray-700 dark:hover:border-gray-700 dark:focus:ring-gray-700">Message</a>
</div>
</div>
</div>
After Code (Good)
// ์์ ๋ ์ฝ๋ (CSS Modules ์ฌ์ฉ)
import styles from './Button.module.css';
<button className={styles.primaryButton}>Submit</button>
// Button.module.css
.primaryButton {
background-color: #3490dc;
color: white;
font-weight: bold;
padding: 0.5rem 1rem;
border-radius: 0.25rem;
}
.primaryButton:hover {
background-color: #2779bd;
}
//์์ ๋ ์ฝ๋ (tailwindcss์ @apply ์ฌ์ฉ)
<button className="custom-button">Submit</button>
<style>
.custom-button {
@apply bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded;
}
</style>
//์์ ๋ ์ฝ๋ (์ปดํฌ๋ํธ ๊ธฐ๋ฐ ์คํ์ผ๋ง)
import styled from 'styled-components';
const StyledButton = styled.button`
background-color: #3490dc;
color: white;
font-weight: bold;
padding: 0.5rem 1rem;
border-radius: 0.25rem;
&:hover {
background-color: #2779bd;
}
`;
<StyledButton>Submit</StyledButton>