It is encourage to build website on 'mobile-first' design to optimize the experience of mobile user! To do that we can write base CSS with mobile design first then only use media queries
to adjust the layout for wider screens.
There are 4 type of unit we used to adjust the size of element:
- Pixels (
px
): Absolute units that always appear the same size on screen. -
em
: Relative unit that 1em is equal to the parent element's font-size value. Example if the paragraph havefont-size: 20px
then if we set themargin-left: 1.5em
is equal to 30px of margin-left. -
rem
: Relative unit that 1rem is always relative to the root element. Example html havefont-size: 16px
by default, so 1rem = 16px. - Percentage (
%
): Relative value that % is based on the value of its parent element. Examplemain
have width of 720px, then if we set the childdiv
elementwidth: 80%
is equal to 576px.
Which is the best to use? It depend:
-
px
: Pixel is suggest to use onmax-width
element. Example ofdiv
max-width is set to 720px and no matter how big the screen is the max-width still fix on 720px. -
em
: EM is suggest to use onmargin
andpadding
. The margin and padding will then scale based on the element's current font size. -
rem
: REM is suggest to use onfont-sizes
element so that there is no compounding issue fromem
and the font size is known because based on root element which is 16px. -
%
: Percentage is suggest to use on width especially container and image for size flexibility based on parent element. - Beside the unit suggest above there is unitless number values too. Example of
line height
property which can be set to value1
only.1.5
is suggest to set on line height which is 1.5 times the font-size especially on paragraph.
Another thing to highlight is common breakpoints for responsive design which is:
- 480px
- 768px
- 1024px
- 1280px
Thanks for your time to finish read about my post. Feel free to share your idea and thought! 😸
let life = {
first: 'eat',
second: 'sleep',
third: 'code',
forth: 'repeat',
};
const {first, second, third, forth} = life
function capital(word) {
return word[0].toUpperCase() + word.slice(1)
}
console.log(`${capital(first)}, ${capital(second)}, ${capital(third)}, ${capital(forth)}!`);
Andrew Tan
Top comments (0)