-
Why frameworks?
- optimized css,
- consistent UI
- saves time.
- Eg : MaterialUI, Bootstrap, Base Web UI, Ant design, chakra UI
-
Can we use more than one framework ?
- It's all package can use any number of frameworks,
- But, not consistent way
-
Different ways to write css :
- Normal Native CSS - all components's styles in a single file index.css
- SCSS - Syntactical CSS - atlast it is converted to css
- Inline CSS - style attribute - pass object - {{backgroundColor : "red"}}
- Component Library - MaterialUI, Bootstrap, Base Web UI, Ant design, chakra UI
- CSS Framework - Tailwind
- Styled Components: Majorly used in react projects.
-
Important: In a
system design round of interview
, you always have to discuss that- what will you use for styling your components?
- Now, there are differnet ways, you can style your web apps.
- But, what way you will choose & you have give a a good reason for it.
- So, you should know what are the pros & cons of using them.
- Exapmle: what are pros & cons of using
Native CSS
overSCSS
orcomponent library
-
Pros & Cons of using Component librery (i.e MaterialUI, Bootstrap, etc)
-
Pros :
consistent UI
: (All the button in your app will look the same now)- save time
-
Cons :
- Bundle size -
- Loose control over design
- personal customizition is hard
-
-
When? How? & Why? to use it.
-
writing css on the go (i.e in the same file )
-
reusablity
-
less bundle size (minimal css) only includes the css classes that we have used
-
Flexible UI (Customizable)
Seting up tailwimd css in our project: refer Tailwind Docs
-
Installing
tailwind
&postcss
using npm [for parcel]npm install -D tailwindcss postcss npx tailwindcss init
-
Configure Tailwind:
-
use command
npm tailwind init
-
This will create
tailwind.config.js
file -
Now, we will be telling tailwind which all files to scan (i.e Configure your template paths)
/** @type {import('tailwindcss').Config} */ module.exports = { content: [ "./src/**/*.{html,js,ts,jsx,tsx}", ], theme: { extend: {}, }, plugins: [], }
-
-
Configure postcss:
- Create
.postcssrc
file. - Configure PostCSS:
{ "plugins""{ "tailwindcss": {} } }
- In this
.postcssrc
file, we have to tell theparcel
(i.e bundler) that we will be using tailwind. So, compile our tailwind css into normal css during the build. This is the reason, we use.postcssrc
configuration.
- Create
-
Now, in 'index.css`
- We will not be writing any css in it.
- Instead there will only three lines inside it (i.e Add the Tailwind directives to your CSS):
@tailwind base; @tailwind components; @tailwind utilities;
- Add a plugin/extension
Tailwind CSS IntelliSense
in VScode.
NOTE: Sometime
Tailwind CSS IntelliSense
extension doesn't show suggetions. In that casepress ctrl + spacebar
before typing any class.
-
Pros:
- Saves Time (faster developnment)
- Easy to debug
- Less code is shipped
- No duplicates CSS
- Bundle size is small
- Much more customisable (then other frameworks like MaterialUi, Bootstrap,...)
- Gives much more control over things
-
Cons:
- Initial learning curve: every new developer that will join our team will take some time understand & learn.
- Too much class. So, readability is compromised a little.
Note: Because of
JSX
&tailwind
we don't have to move out of our.js
file.
- Tailwind Cheatsheet: Cheatsheet 1 | Cheatsheet 2