ðã¡ãããšäœ¿ãåããŠãïŒCSSã®px, em, rem, %, vwåäœã®éã
HTMLãCSSãåŠã³ããŠã®é ã¯ãšããããpxåäœã§æå®ããããšãå€ããšæããŸãããã®åŸãemãremãªã©ããããªåäœãåºãŠããŠæ··ä¹±ããããšããããããããŸããã
ããããã®åäœã«ã¯ã¡ãªãããšãã¡ãªãããããããªããã®åäœã䜿ãã®ãããã£ãããšç解ããŠäœ¿ãåããå¿ èŠããããŸãã
åäœã®å®çŸ©
CSSã«ã¯å€ãã®åäœãååšããŸããããã§ã¯ãå®éã«äœ¿ãããšã®å€ãåäœã«ã€ããŠè©³ããã¿ãŠãããŸãã
pxåäœ
ããã€ã¹ã®1ããããè¡šã絶察çãªå€ã§ãããã ããããªã³ã¿ãŒãã¹ãããªã©ã®é«è§£å床ããã€ã¹ã§ã¯1pxå ã«è€æ°ã®ããããå«ãå ŽåããããŸãã
çŸåšãã»ãŒãã¹ãŠã®ãã©ãŠã¶ã§å°æ°ç¹pxãè¡šãããšãã§ããŸãããborderãªã©äžéšããããã£ã§ã¯å°æ°ç¹pxã蚱容ããªãä»æ§ããããŸãã
emåäœ
ãã®èŠçŽ èªèº«ã®font-sizeå€ãåºæºãšããŠçžå¯Ÿçãªå€ãšãªããŸããããšãã°ãpaddingããããã£ã«æå®ããå ŽåãèããŠã¿ãŸãããã
<html>
<body>
<div class="parent">
<div class="child">...</div>
</div>
</body>
</html>
.parent {
padding: 2em;
}
.child {
padding: .5em;
}
ãŸãã.parentèŠçŽ ã®font-sizeå€ãæ±ããŸããã.parentèŠçŽ ã«ã¯font-sizeããããã£ãæå®ãããŠããªãã®ã§ãããã«äžäœã®bodyèŠçŽ ãèŠãŠã¿ãŸããããããbodyèŠçŽ ã«ãfont-sizeããããã£ãæå®ãããŠããªããããçµæçã«htmlèŠçŽ ã®font-sizeå€ãåºæºãšãªããŸãã
ã€ãŸããemåäœã¯ãã®èŠçŽ èªèº«ãšèŠªèŠçŽ ãããã®ãŒã£ãŠfont-sizeå€ãç¶æ¿ãããšããããšã§ããã
htmlèŠçŽ ã®font-sizeå€ã¯ããã©ã«ãã§ã¯16pxã§ããã¡ãªã¿ã«ããŠãŒã¶ãŒã¯ãã©ãŠã¶ã®èšå®ããæåãµã€ãºããã€ã§ãå€æŽã§ããŸããããã©ã«ãã®ç¶æ ã®å Žåã.parentèŠçŽ ã¯16pxãåºæºãšããŠããã®ã§ã
16 Ã 2 = 32px
ãpaddingå€ãšãªããŸãã
åãããã«.childèŠçŽ ãããã®ãŒã£ãŠãããšãhtmlèŠçŽ ãŸã§font-sizeããããã£ãæå®ãããŠããªãã®ã§ãhtmlèŠçŽ ã®16pxãåºæºå€ãšãªããŸãããã£ãŠã
16 Ã 0.5 = 8px
ã.childèŠçŽ ã®paddingå€ã«ãªããŸãã
ãããŸã§ã¯ãæ¯èŒçããããããã§ããããã®èŠçŽ èªèº«ã«font-sizeããããã£ãæå®ãããŠãããšè€éã«ãªã£ãŠããŸãã
body {
font-size: 1.125em;
}
.parent {
padding: 2em;
font-size: 1.5em;
}
.child {
padding: .5em;
}
åãããã«.parentèŠçŽ ããèããŠã¿ãŸããããä»åã¯.parentèŠçŽ èªèº«ã«font-sizeããããã£ãšããŠ1.5emãæå®ãããŠããŸãã1.5emèªäœãçžå¯Ÿçãªå€ã®ãããããäžäœã®èŠçŽ ããç¶æ¿ãããŠããå¯èœæ§ããããŸãã
ããã§ãäžäœã®bodyèŠçŽ ãã¿ãŠã¿ããšãfont-sizeããããã£ã«1.125emãæå®ãããŠããŸããããããäžäœã®èŠçŽ ã¯htmlèŠçŽ ãªã®ã§ãã©ãŠã¶ã®ããã©ã«ãæåãµã€ãºã§ãã16pxãåºæºãšãªããŸãã
ãã£ãŠã.parentèŠçŽ ã®font-sizeã¯ã
16 Ã 1.125 Ã 1.5 = 27px
ãšãªããŸãããªã®ã§ã
27 Ã 2 = â54px
ãpaddingããããã£ã®2emã®èšç®å€ãšãªããŸãã
ãããŠã.childèŠçŽ èªèº«ã«ã¯font-sizeããããã£ãæå®ãããŠããªãã®ã§ã芪ã®.parentèŠçŽ ã®font-sizeå€ãåºæºãšãªããŸãã.parentèŠçŽ ã®font-sizeå€ã¯å ã»ã©æ±ãã27pxã§ãã
27 Ã 0.5 = 13.5px
ãã£ãŠã13.5pxã.childèŠçŽ ã®paddingå€ãšãªããŸãã
ã¯ããã¯æžæããããããŸããããæ £ããŠããŸãã°ãã®æ§è³ªãéã«ããããããã®ãšãªã£ãŠãããŸãã
remåäœ
ã«ãŒãèŠçŽ ïŒéåžžã¯htmlèŠçŽ ïŒã®font-sizeå€ãåºæºãšããŠçžå¯Ÿçãªå€ãšãªããŸãã
body {
font-size: 1.125em;
}
.parent {
padding: 2rem;
font-size: 1.5em;
}
.child {
padding: .5rem;
}
ãã©ãŠã¶ã®ããã©ã«ãæåãµã€ãºã¯16pxãªã®ã§.parentèŠçŽ ã®paddingå€ã¯ã
16 Ã 2 = 32px
ã§ã.childèŠçŽ ã®paddingå€ã¯ã
16 Ã 0.5 = 8px
ãšãªããŸããfont-sizeããããã£ãæå®ãããŠãããã©ããã¯ãŸã£ãã圱é¿ããŸãããã€ãŸããremåäœã¯åžžã«htmlèŠçŽ ã®font-sizeå€ãåºæºãšãããããemåäœã®ããã«èŠçŽ ãå ¥ãåã«ãªã£ãŠããŠãé¢ä¿ãããŸããã
%åäœ
çžå¯Ÿçãªå€ãšãªããŸãããããããã£ã«ãã£ãŠäœãåºæºã«ãªãããå€ãã£ãŠããŸãã
ããšãã°ãwidthãmarginãpaddingããããã£ãªã芪èŠçŽ ã®æšªå¹ ã«å¯Ÿããå²åãheightããããã£ãªã芪èŠçŽ ã®é«ãã«å¯Ÿããå²åãfont-sizeããããã£ãªã芪èŠçŽ ã®æåãµã€ãºã«å¯Ÿããå²åãšãªããŸãã
font-sizeããããã£ã«é¢ããŠã¯emåäœãšåãã§ããã
ãªã®ã§ã%åäœã䜿ãå Žåã¯äœãåºæºãšããŠããããããããã£ããšã«ç¢ºèªããŠãããšå®å¿ã§ãã
vwåäœ
ãã©ãŠã¶ã®æšªå¹ ã«å¯Ÿããçžå¯Ÿçãªå€ãšãªãã1vwã¯ãã©ãŠã¶ã®æšªå¹ ã®1%ã«çžåœããŸãã
ããšãã°ããã©ãŠã¶å¹ ã1200pxã®ãšã1vwã¯12pxããã©ãŠã¶å¹ ã768pxã®ãšã1vwã¯7.68pxãè¡šãããšã«ãªããŸãã
ãã ãå šäœçãªã¬ã€ã¢ãŠããªã©ã«vwåäœã䜿ããšæå³ããªãäœçœãçãŸããããšããããŸããvwåäœã¯ã¹ã¯ããŒã«ããŒã®å¹ ãå«ãä»æ§ã®ãããwidth: 100vw;ãæå®ãããšã¹ã¯ããŒã«ããŒã®å¹ åã暪ã¹ã¯ããŒã«ãçºçããŸãã
ãªã®ã§ãå šäœçãªã¬ã€ã¢ãŠãã«ã¯%åäœã®ã»ããé©ããŠãããšãããŸãã
ãããã%åäœã¯åžžã«èŠªèŠçŽ ã«äŸåããããããã©ãŠã¶å¹ ãåºæºãšãããå Žåã«ã¯äœ¿ããªãã®ã§ããã®ç¹äŸ¿å©ã§ããã
ã€ãŸããvwåäœãš%åäœã¯ãremåäœãšemåäœã®é¢ä¿æ§ãšã»ãŒåãã§ãã
åäœã®äœ¿ãåã
è²ã ãªWebãµã€ãã調æ»ããçµæã倧ããåããŠä»¥äžã®3ã€ã«åãããããšèããŸããããã ãã%ãvwåäœã¯ã©ã®æŽŸã§ãã¬ã¹ãã³ã·ã察å¿ã§ãã䜿ãã®ã§é€å€ããŠããŸãã
pxåäœãçšãã掟
ãã¹ãŠã®CSSãpxåäœã§æå®ããå Žåã§ãã
ãã¹ãŠpxåäœã§æå®ãããšãã©ãŠã¶ã®èšå®ããæåãµã€ãºãå€æŽããŠãäžååæ ãããªãããšã«ãªã£ãŠããŸããŸãã
åãçãŸãã€ãç®ãããã¯ãªãã®ã§ãããæ®æ®µmacOSã䜿ã£ãŠãããããåãèŠã¥ãããªãšæã£ãã2æ¬æã§ãºãŒã ããã°å¥œããªéšåãæ¡å€§ã§ããŸãã
ãã®ãããã§ãã©ãŠã¶ã®æåãµã€ãºèšå®ãå€ããããšã¯ãªãã®ã§ãããWindowsã«ã¯ãã®æ©èœããªãã®ã§ãããWindowsãæ®æ®µäœ¿ãããŠããããã©ãŠã¶ã®æåãµã€ãºèšå®ã§ããŒã¹ã®æåãµã€ãºã倧ãããã€ã€ãæ¡å€§çã§Webãµã€ãã«ãã£ãŠåŸ®èª¿æŽãããšæããŸãã
ã¡ãªã¿ã«Windowsã«ãæ¡å€§é¡ãšããæ©èœããã£ãŠäœ¿ã£ãŠã¿ãã®ã§ããã䜿ãã¥ãããŠäœ¿ãæ°ã«ãªããŸããã§ããããã¯ãããã©ãã¯ãããã®ãžã§ã¹ãã£ã«ã¯æµããŸããã
ãªã®ã§ãpxåäœããã¹ãŠã«çšãããšã¢ã¯ã»ã·ããªãã£ãç¡èŠããŠããŸãå¯èœæ§ããããŸãã
äžéšemãremåäœãçšãã掟
äž»ã«ãfont-sizeããããã£ã«emãremåäœãšãã£ãçžå¯Ÿçãªå€ãæå®ããå Žåã§ãã
font-sizeããããã£ã«çžå¯Ÿçãªå€ãæå®ããŠãããšããã©ãŠã¶ã®æåãµã€ãºèšå®ãå€æŽãããéã«ã¡ãããšåæ ãããŸãã
ããšãã°ãfont-sizeããããã£ä»¥å€ã¯ãã¹ãŠpxåäœã§æå®ããå ŽåãèããŠã¿ãŸãããã
ãã©ãŠã¶ã®æåãµã€ãºèšå®ã§æåã倧ãããããšãçžå¯Ÿçãªå€ã§æå®ãããfont-sizeã ã倧ãããªãã絶察çãªå€ã§æå®ããããã®ä»ã®èŠçŽ ã¯ãã®ãŸãŸã«ãªããŸãã
ã€ãŸããå šäœçãªã¬ã€ã¢ãŠãã¯ãã®ãŸãŸã§ãèŠçŽ å ã®æåãµã€ãºã ã倧ãããªããŸãã
emãremåäœãçšãã掟
次ã«ãborder-widthããããã£ãªã©æåãµã€ãºèšå®ãå€ãã£ãŠãå€åãããããªããã®ã«ã¯pxåäœã䜿ãããã以å€ã¯emãremåäœãªã©ã®çžå¯Ÿçãªå€ã䜿ãå ŽåãèããŠã¿ãŸãããã
å ã»ã©ã®äŸãšéãç¹ã¯ãæåãµã€ãºãšãã£ããã«äœçœãæ¯äŸããŠåºãã£ãŠãããšããã§ãã
å šäœçãªã¬ã€ã¢ãŠããšãã¶ã€ã³ã®æ¯çãç¶æãã€ã€ãæåãµã€ãºãå€åãããããŸãã
ã¡ãã£ã¢ã¯ãšãªã®åäœã«ãçžå¯Ÿçãªå€ã䜿ã£ãŠããã°ãæåãµã€ãºã倧ãããªãã°ãªãã»ã©ãã¹ããã®ãã¶ã€ã³ã«è¿ã¥ããŠãããŸããããã¯ããã©ãŠã¶ã®æ¡å€§çããããå Žåãšåãæåã«ãªããŸãã
ãã®ããã«ã絶察çãªå€ãšçžå¯Ÿçãªå€ãã©ãã«äœ¿ããã§å€§ããæåãå€ãã£ãŠããŸãã
å人çãªèŠè§£
åã®æèŠã§ããããpxåäœãçšãã掟ãã¯ã¢ã¯ã»ã·ããªãã£ã®èŠ³ç¹ãããŸããªããšæã£ãŠããŸãã
ãããŠããäžéšemãremåäœã䜿ã掟ããšãemãremåäœã䜿ã掟ãã«ã€ããŠã¯éçºè ã®æå³ããŠãŒã¶å±€ã«ãã£ãŠå€ãããããã©ã¡ããæšå¥šããããŸã§ã¯èšãåããŸããã
ãã ããemãremåäœã䜿ã掟ãã¯æ¡å€§çãæ¡å€§çž®å°ããå Žåãšæåãµã€ãºèšå®ãå€æŽããå Žåã§åäœããŸã£ããåãã«ãªããŸããããã§ã¯ã2ã€ã®æ©èœãå·®å¥åããããšã¯ã§ããŸããã
äžæ¹ã§ãæåãµã€ãºèšå®ã¯é »ç¹ã«å€æŽãããã®ã§ã¯ãªãããããŸã§ããŒã¹ãšãªãæåãµã€ãºãèšå®ããŠããããã®ãã®ã ãšãèããããŸãã
æåãµã€ãºèšå®ã®å€æŽã¯æ¡å€§çã®ããã«æ軜ã«å€æŽã§ããããã«ã¯ãªã£ãŠããªãããã§ãã
ãããšããã§ã«æåãµã€ãºèšå®ãå€æŽããŠãã人ããããã°ãåããŠèšªåãããšãããã®Webãµã€ãæ¬æ¥ã®å§¿ãšãªããŸãããããã£ãå Žåã¯éã«ãã¬ã¹ãã³ã·ãã®ã¹ããã¬ã€ã¢ãŠãã«è¿ã¥ããŠãã£ãæ¹ãæåãµã€ãºãšãšãã«äœçœãã¬ã€ã¢ãŠãã調æŽãããŠèŠããããšæãããŸãã
äœçœãå¯èªæ§ã«å°ãªãããé¢ãã£ãŠãããšèããããããã§ãã
以äžããµãŸããåãªãã®èããšããŠã¯ã
ã¡ãã£ã¢ã¯ãšãª â em
font-size â em / rem
borderãªã©åžžã«èŠãç®ãå€ãããªã â px
ããä»¥å€ â em / rem
ãšãªããŸãã
åã以åã¯ãäžéšemãremåäœã䜿ã掟ãã ã£ãã®ã§ãããã¬ã€ã¢ãŠãã«ãã£ãŠã¯èŠã¥ãããªãããšããã£ãŠçŸåšã¯ãemãremåäœã䜿ã掟ãã§ãã
äžäŸãšããŠã3åå²ãããã¬ã€ã¢ãŠãã§æåãµã€ãºã ãã倧ãããªããšã1è¡ãããã®æåæ°ãæžãã瞊ã«é·ããªã£ãŠããŸããŸãã
emãšremåäœã®äœ¿ãåãã«é¢ããŠã¯çŸåšã®èŠçŽ ãç¥å èŠçŽ ã®font-sizeæå®ã«ãã£ãŠåœ±é¿ãããããªããã®ã«ã¯remåäœããã以å€ã¯emåäœãšããã°ããã§ãã
ããšãã°ããã¿ã³åšãã®äœçœã¯æåãµã€ãºã«å¿ããŠå€åããŠã»ããã®ã§emåäœããµã€ãå šäœã®æšªå¹ ã決ããã³ã³ããã§ã¯remåäœãšãã£ãããã«äœ¿ãåããŸãã
ãããŸã§âå人çæèŠâãªã®ã§ãã©ã®ããã«äœ¿ãåãããã¯èªç±ã§ãã
ãããã«ãfont-sizeããããã£ã«vwåäœãçšããã®ã¯ãããã»ããããã§ããvwåäœã¯åžžã«ãã©ãŠã¶ã®æšªå¹ ãåºæºãšãããã©ãŠã¶ã®æåãµã€ãºèšå®ãå€ããŠãåæ ãããªãããã§ãã
font-sizeããããã£ã«vwåäœã䜿ãå Žåã¯ãcalc()é¢æ°ã䜿ã£ãŠemãremåäœãªã©ã®çžå¯Ÿçãªå€ãšçµã¿åãããŸãã
ããã§ãçŽæã«åããŠããŸããããããŸããããäžå¿ãã©ãŠã¶ã®æåãµã€ãºèšå®ã¯åæ ã§ããŸãã
ãã®èšäºãæ°ã«å ¥ã£ãããµããŒããããŠã¿ãŸãããïŒ