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: 20pxthen if we set themargin-left: 1.5emis equal to 30px of margin-left. -
rem: Relative unit that 1rem is always relative to the root element. Example html havefont-size: 16pxby default, so 1rem = 16px. - Percentage (
%): Relative value that % is based on the value of its parent element. Examplemainhave width of 720px, then if we set the childdivelementwidth: 80%is equal to 576px.
Which is the best to use? It depend:
-
px: Pixel is suggest to use onmax-widthelement. Example ofdivmax-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 onmarginandpadding. The margin and padding will then scale based on the element's current font size. -
rem: REM is suggest to use onfont-sizeselement so that there is no compounding issue fromemand 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 heightproperty which can be set to value1only.1.5is 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)