å€æããããã«ãCSSã¯ããŸããŸãªæ°ã®èŠçŽ ã§æ©èœãããããé çãäžèŠãªjsã³ãŒããåãé€ãããšãã§ããŸãã
åçã³ã³ãã³ã
ã¬ã¹ãã³ã·ããã¶ã€ã³ã¯éåžžã1ã€ã®å€æ°-ã¹ããŒã¹ã«é¢é£ä»ããããŠããŸãã ã°ãªããã«å¿çããŠãã¹ããããšããäžå®éã®ã³ã³ãã³ããååŸããã©ãã ãã®ã¹ããŒã¹ãå¿ èŠããã©ã®ããã«èŠããã©ãã«åãŸãããã©ãã«åãŸããªããã確èªããŸãã ã€ãŸããã³ã³ãã³ãã¯å®æ°ãšèŠãªãããã¹ããŒã¹ã¯å€æ°ãšèŠãªãããŸãã
ã¡ãã£ã¢ã¯ãšãªã¯ã¬ã¹ãã³ã·ããã¶ã€ã³ã®åºç€ã§ããããã«ãããå¢çç·ãæç»ããã©ã®ã°ãªãããå¥ã®ã°ãªããã«å€æŽãããããæž¡ãããšãã§ããŸãã ãã ããèŠçŽ ã®é 眮ãšãã®åšå²ã®ã¹ããŒã¹ã¯ãç»é¢ãµã€ãºã ãã§ãªããã³ã³ãã³ãèªäœã«ãã£ãŠã圱é¿ãåããå¯èœæ§ããããŸãã
ãµã€ã蚪åè ãããŸããŸãªç»é¢ãµã€ãºã®å€ãã®ããã€ã¹ã䜿çšã§ããããã«ãã³ã³ãã³ã管çè ãšç·šéè ã¯ã³ã³ãã³ããè¿œå ãŸãã¯åé€ã§ããŸãã 圌ãã¯ããã®ããã«å šäœã®CMSããæã£ãŠããŸãã ãã®ãããPhotoshopã®ããŒãžãã¶ã€ã³ã®é¢é£æ§ã倱ãããŸãããç»é¢ã®å¹ ã¯åžžã«åºå®ãããã³ã³ãã³ãã¯åžžã«åºå®ãããŠããŸãã
ãã®èšäºã§ã¯ãèŠçŽ ã®æ°ïŒã«ãŠã³ãïŒã«åé¡ã®ãªãCSSãäœæããææ³ã«ã€ããŠèª¬æããŸãã ããã¯ãç¹å¥ã«èšèšãããã»ã¬ã¯ã¿ãŒã«ãã£ãŠå®çŸãããŸãã å€å žçãªåé¡ã解決ããã³ã³ãã¹ãã«ããããé©çšããŸããèŠçŽ ã®æ°ãå°ãªãå Žåãå€ãå Žåã«ããµã€ãã®æ°Žå¹³ã¡ãã¥ãŒã§èŠçŽ ã®ã¬ã€ã¢ãŠããåå²ããæ¹æ³ã§ãã ããšãã°ã6ã€ä»¥äžã®ã¡ãã¥ãŒé ç®ã䜿çšããŠãèŠçŽ ã®ã¹ã¿ã€ã«è¡šç€ºãã€ã³ã©ã€ã³ãããã¯ã§ãæ°åãå°ãã-衚瀺ïŒããŒãã«ã»ã«ã®ããã«ããæ¹æ³ã
ãã³ãã¬ãŒããjsã¯äœ¿çšããŸãããã¡ãã¥ãŒé ç®ã«ã¯ã©ã¹ãç»é²ããå¿ èŠããããŸããã CSSã»ã¬ã¯ã¿ãŒã®ã¿ã䜿çšããææ³ã¯ãã©ã®ã³ã³ãã³ãïŒHTMLïŒãšè¡šç€ºïŒCSSïŒã圹å²ãæ確ã«å®çŸ©ããŠãããã«å¿ããŠã é¢å¿ã®åé¢ã®ååã«åŸããŸãã ããŒã¯ã¢ããã¯CSSã®æ©èœã§ãããå¯èœã§ããã°CSSã®ã¿ã§ãã
ãã®ãã¢ã¯CodePenã§å ¥æã§ããèšäºå šäœã§åãäžããããŸãã
ç解ãç°¡åã«ããããã«ãHTMLã¿ã°ã®ä»£ããã«squidã®ç»åã䜿çšããŸãã ç·ã®ã€ã«ã¯ç¹å®ã®ã»ã¬ã¯ã¿ãŒã«è©²åœããèŠçŽ ã«å¯Ÿå¿ããèµ€ã®ã€ã«ã¯è©²åœããªãã»ã¬ã¯ã¿ãŒã«å¯Ÿå¿ããç°è²ã¯èŠçŽ ãååšããªãããšãæå³ããŸãã
ã¢ã«ãŠã³ã
èŠçŽ ã®æ°ã調ã¹ãã«ã¯ãããããæ°ããå¿ èŠããããŸãã
ã泚æ ããã
CSSã¯æ瀺çãªãã«ãŠã³ãAPIããæäŸããŸããããå¿
èŠã«å¿ããŠã»ã¬ã¯ã¿ãŒãçµã¿åãããããšã«ããããã®åé¡ãåé¿ã§ããŸãã
ãã£ããã³
1ã€ã«æ°ãã
åèŠçŽ ã®ã¿ã®ã»ã¬ã¯ã¿ãŒã¯ãåžžã«1ã€ã®èŠçŽ ã§ããªã¬ãŒãããŸãã æ¬è³ªçã«ãããã«ããããåèšã§æ£ç¢ºã«1ã€ã®å Žåãç¹å®ã®èŠçŽ ã®ãã¹ãŠã®åã«ã¹ã¿ã€ã«ãé©çšã§ããŸããã ããã¯ããã«ãŠã³ãããšèšè¿°ããããšãã§ããå¯äžã®åçŽãªã»ã¬ã¯ã¿ãŒã§ãïŒãã¡ãããããã«äŒŒãŠããïŒã¿ã€ãã®ã¿ïŒã
以äžã®äŸã§ã¯ãonly-of-typeã䜿çšããŠããã¹ãŠã®ãã¿ã³ã«ã¹ã¿ã€ã«ãé©çšããŸãããããã®ãã¿ã³ã¯ãé£æ¥ããå¯äžã®åãã¿ã³ã§ãã
button { font-size: 1.25em; } button:only-of-type { font-size: 2em; }
ããã§ã¯ãè¡ã®é åºãéèŠã§ããããšãç解ããããšãéèŠã§ãã ããã«ã2ã€æªæºã®éžæã®èŠ³ç¹ãããã®ã³ãŒãã確èªãããšäŸ¿å©ã§ãã
åæ§ã«ã negationã䜿çšããŠè€æ°ã®éžæãè¡ããããã«ãªããŸããã
/* "--" */ button { font-size: 2em; } button:not(:only-of-type) { font-size: 1.25em; }
Nåã®ã¢ã€ãã
1以äž2以äžã«åºã¥ããã¹ã¿ã€ã«ãé©çšããã®ã¯è³¢ãæ¹æ³ã§ãããä»»æã®æ°ã§äœæ¥ã§ããããã«ããããæè»ãªããŒã«ãå¿ èŠã§ãã ä»»æã®Nã«å¯ŸããŠãN以äžã®ãéžæã䜿çšããŸããããã«ããæ£ç¢ºã«745ããŸãã¯ãåèš6åããéžæããŸãã
ãããè¡ãããã«ãã»ã¬ã¯ã¿ãŒïŒnth-ââlast-childïŒnïŒããã ãããã«ä»»æã®æ°ããã©ã¡ãŒã¿ãŒãšããŠæž¡ããŸãã ãªã¹ãã®æåŸããéåžžã«å€ãã®èŠçŽ ãæ°ããããšãã§ããŸãã äŸïŒnth-ââlast-childïŒ6ïŒã¯ãé£æ¥ããèŠçŽ ã®äžã§æåŸãã6çªç®ã®èŠçŽ ãéžæããŸãã
nth-last-childïŒ6ïŒãšfirst-childãçµã¿åããããšããã¹ãŠãããèå³æ·±ããã®ã«ãªããŸãããã®çµæãæåŸãã6çªç®ãæåããæåã®ãã¹ãŠã®èŠçŽ ã«ãªããŸãã
li:nth-last-child(6):first-child { /* */ }
ãã®ãããªèŠçŽ ãååšããå Žåãæ£ç¢ºã«6ã€ã®èŠçŽ ãããããšãæå³ããŸãã ããã§ãç§ã¯èªåã®åã«äœåã®èŠçŽ ãèŠããããäŒããããšãã§ããCSSã³ãŒããæžããŸããã
ãã®ãæåŸãã6çªç®ãšæåããæåã®ãèŠçŽ ãåæã«äœ¿çšããŠãä»ã®5ã€ã®èŠçŽ ããã¹ãŠéžæããããšã¯ãä»ã§ãæ®ã£ãŠããŸãã ãããè¡ãã«ã¯ã å ±éã®é£æ¥ããã³ã³ãããŒã¿ãŒã䜿çšããŸãã
ãã®ã³ã³ãããŒã¿ã«æ £ããŠããªãå Žåã¯ãliïŒnth-ââlast-childïŒ6ïŒselectïŒfirst-childãliã説æããŸãããliïŒnth-ââlast-childïŒ6ïŒã®åŸã«æ¥ããã¹ãŠã®liïŒãæåã®åãã ãã®äŸã§ã¯ãèŠçŽ ãã¡ããã©6ã€ããå ŽåãèŠçŽ ã®ãã©ã³ãã¯ç·è²ã«ãªããŸãã
li:nth-last-child(6):first-child, li:nth-last-child(6):first-child ~ li { color: green; }
6以äž
6ã19ããŸãã¯653ã®åºå®éãéžæããããšã¯ããã®ãããªå¿ èŠæ§ã¯éåžžã«ãŸããªã®ã§ãããŸã圹ã«ç«ã¡ãŸããã ããã¯ã¡ãã£ã¢ã¯ãšãªãšåãã§ã-min-widthãŸãã¯max-widthã®ä»£ããã«åºå®å¹ ã䜿çšããããšã¯ããŸã䟿å©ã§ã¯ãããŸããã
@media screen and (width: 500px) { /* 500px */ }
ããã²ãŒã·ã§ã³ã¡ãã¥ãŒã§ãèŠçŽ ã®æ°ã«åºã¥ããŠã¹ã¿ã€ã«ãå¢çç·ã§åãæ¿ããŸããããšãã°ã6ã€ä»¥äžã®èŠçŽ ïŒæ£ç¢ºã«ã¯6ã€ã§ã¯ãªãïŒãããå Žåã¯ãç®çã®ã¹ã¿ã€ã«ãå€æŽããŸãã
åé¡ã¯ããã®ãããªã»ã¬ã¯ã¿ãã©ã®ããã«äœæãããã§ãã ããã¯ãã€ã¢ã¹ã®åé¡ã§ãã
ãã©ã¡ãŒã¿ãŒn + 6
ã»ã¬ã¯ã¿ãŒïŒnth-ââchildïŒïŒã¯ãã©ã¡ãŒã¿ãŒãšããŠæ°å€ã ãã§ãªããåŒãn + [number]ãã䜿çšã§ããŸãã äŸïŒnth-ââchildïŒn + 6ïŒã¯ã6çªç®ããå§ãŸããã¹ãŠã®èŠçŽ ã«é©çšãããŸãã
ãã®ã»ã¬ã¯ã¿ãŒã¯ããèªäœåŒ·åãªããŒã«ã§ãããæ°éã§æäœããããšã¯ã§ããŸããã 6åãè¶ ããèŠçŽ ãããå Žåã¯é©çšãããŸãããã5åãè¶ ããèŠçŽ ã«ã®ã¿é©çšãããŸãã
ãã®åé¡ãåé¿ããã«ã¯ãæåŸã®5ã€ãé€ããã¹ãŠã®èŠçŽ ãéžæããselectãäœæããå¿ èŠããããŸãã reverseïŒnth-ââchildïŒn + 6ïŒselectïŒnth-ââlast-childïŒn + 6ïŒã䜿çšããŠããã¹ãŠã®èŠçŽ ããæåŸãã6çªç®ããæåããæåãŸã§ãéžæã§ããŸãã
li:nth-last-child(n+6) { /* */ }
ãã®ãããªéžæã«ãããä»»æã®é·ãã®ã»ããããæåŸã®5ã€ã®èŠçŽ ãåãåãããŸããã€ãŸããèŠçŽ ã6ã€æªæºã®å Žåãéžæã«ã¯äœãå ¥ããŸããã
ãªã¹ãã«6ã€ä»¥äžã®èŠçŽ ãããå Žåãæ®ãã®5ã€ã®èŠçŽ ãéžæã«è¿œå ããå¿ èŠããããŸãã ããã¯ç°¡åã§ãã6ã€ä»¥äžã®èŠçŽ ãããå Žåãnçªç®ã®æåŸã®åïŒn + 6ïŒæ¡ä»¶ãããªã¬ãŒãããããããšçµã¿åãããããšã§å¿ èŠãªãã¹ãŠã®èŠçŽ ãéžæã§ããŸãã
ãã®ãããªçãèšé²ãåé¡ã®è§£æ±ºçã§ãã
li:nth-last-child(n+6), li:nth-last-child(n+6) ~ li { /* */ }
ãã¡ããã653.279ã§ãã£ãŠããä»»æã®æ£ã®æŽæ°ã䜿çšã§ããŸãã
LessãŸãã¯n
åã®äŸã®ããã«ãonly-of-typeã®å Žåãã»ã¬ã¯ã¿ã¯ãN以äžããšãN以äžãã®äž¡æ¹ãšããŠäœ¿çšã§ããŸãã ã©ã®ãªãã·ã§ã³ã䜿çšããŸããïŒ ã¡ã€ã³ãæ€èšããã°ãªããã«äŸåããŸãã
ãLess or Nãã®å Žåãnããã€ãã¹ã§åããæ¡ä»¶ïŒfirst-childãè¿œå ããŸãã
li:nth-last-child(-n+6):first-child, li:nth-last-child(-n+6):first-child ~ li { /* */ }
ãã®çµæãã-ãã䜿çšãããšãéžæã®æ¹åãå€ãããŸããæåãã6ã«ã«ãŠã³ããã代ããã«ãã«ãŠã³ãããŠã³ã¯æåŸãã6ã«ãªããŸãã ã©ã¡ãã®å Žåãã6çªç®ã®èŠçŽ ãéžæã«å«ãŸããŸãã
nth-child vs nth-of-type
åã®äŸã§ã¯ãnth-childïŒïŒãšnth-last-childïŒïŒã䜿çšããŸããããnth-of-typeïŒïŒãšnth-last-of-typeïŒïŒã¯äœ¿çšããŠããŸããã <li>ã¿ã°ãš<ul>ã®æ£ããåå«ã®ã¿ãéžæããŠããããã次ã®ããã«ã§ããŸããlast-childïŒïŒããã³ïŒlast-of-typeïŒïŒãåæ§ã«æå¹ã§ãã
nth-childïŒïŒãšnth-of-typeïŒïŒã®äž¡æ¹ã®éžæã°ã«ãŒãã«ã¯ãç®çã«å¿ããŠç¬èªã®å©ç¹ããããŸãã nth-childïŒïŒã¯ç¹å®ã®ã¿ã°ã«çµã³ä»ããããŠããªããããäžèšã®ææ³ã¯æ··åããåèŠçŽ ã«é©çšã§ããŸãã
<div class="container"> <p>...</p> <p>...</p> <blockquote>...</blockquote> <figure>...</figure> <p>...</p> <p>...</p> </div>
.container > :nth-last-child(n+3), .container > :nth-last-child(n+3) ~ * { /* */ }
ïŒã¿ã°ã«ã¢ã¿ãããããªãããã«ã ãŠãããŒãµã«ã»ã¬ã¯ã¿ãŒã䜿çšããŠããããšã«æ³šæããŠãã ãããïŒLast-childïŒn + 3ïŒã*ããã®å Žåããlast-childïŒn + 3ïŒ ã
äžæ¹ãnth-last-of-typeïŒïŒã®å©ç¹ã¯ãä»ã®å€ãã®é£æ¥ããèŠçŽ ã®äžãã1ã€ã®ã¿ã°ãæã€èŠçŽ ãéžæã§ããããšã§ãã ããšãã°ã<div>ããã³<blockquote>ãšåãããŒãäžã«ããå Žåã§ãã<p>ã¿ã°ã®æ°ãã«ãŠã³ãã§ããŸãã
<div class="container"> <div>...</div> <p>...</p> <p>...</p> <p>...</p> <p>...</p> <p>...</p> <p>...</p> <p>...</p> <blockquote>...</blockquote> </div>
p:nth-last-of-type(n+6), p:nth-last-of-type(n+6) ~ p { /* */ }
ãã©ãŠã¶ã®ãµããŒã
ãã®èšäºã§äœ¿çšãããŠãããã¹ãŠã®CSS2.1ããã³CSS3ã»ã¬ã¯ã¿ãŒã¯ã Internet Explorer 9以é ã ããã³ãã¹ãŠã®ææ°ã®ã¢ãã€ã«ããã³ãã¹ã¯ããããã©ãŠã¶ãŒã§ãµããŒããããŠããŸãã
Internet Explorer 8ã¯ã»ãšãã©ã®éžæãé©åã«ãµããŒãããŸãããjsã®ããªãã£ã«ã«ã€ããŠèããããšã¯åé¡ãããŸããã ãŸãã¯ã IEã®å€ãããŒãžã§ã³çšã«ç¹å¥ãªã¯ã©ã¹ãèšå®ããã»ã¬ã¯ã¿ãŒã«å²ãåœãŠãããšãã§ããŸãã ããã²ãŒã·ã§ã³ã¡ãã¥ãŒã®å Žåãã³ãŒãã¯æ¬¡ã®ããã«ãªããŸãã
nav li:nth-last-child(n+6), nav li:nth-last-child(n+6) ~ li, .lt-ie9 nav li { display: inline-block; /* .. */ }
çŸå®ã®äžçã§
ããã²ãŒã·ã§ã³ã¡ãã¥ãŒãCMSã®ãããµã€ãã«å±ããŠãããšããŸãã 誰ããããåããŠäœ¿çšãããã«ãã£ãŠãã¡ãã¥ãŒã®èŠçŽ æ°ã¯ç°ãªããŸãã 誰ãããããè€éã«ããããšããããããŒã ããšãã¢ããŠããã¯åœŒã«ãšã£ãŠååã§ããã誰ãã¯ãµã€ãã«ãããã¹ãŠã®ãã®ãã¡ãã¥ãŒã«è©°ã蟌ã¿ãããšæã£ãŠããŸãã
ã¡ãã¥ãŒã®ããŸããŸãªèŠçŽ ã®ããŸããŸãªã°ãªãããèæ ®ããã¹ã¿ã€ã«ãäœæããããšã§ãã¬ã€ã¢ãŠããããäžè²«æ§ã®ãããã®ã«ããããŸããŸãªåœ¢åŒã«é©ããŠããŸã-ã³ã³ãã³ããäœã§ããããã¹ãŠãèæ ®ããŸãã
ããã§ãšãããããŸããããã§ãCSSã®å µåšåº«ãèŠçŽ ã®æ°ãåŠçã§ããããã«ãªããŸããã
ã³ã³ãã³ãã«äŸåããªãèšèš
ã¬ã¹ãã³ã·ããã¶ã€ã³ã¯ã1ã€ã®éèŠãªåé¡ã解決ããŸããå€ãã®ç°ãªãããã€ã¹ã«åãã³ã³ãã³ããå¿«é©ã«é 眮ã§ããŸãã ç»é¢ãµã€ãºãééã£ãŠãããšããçç±ã ãã§ãç°ãªãã³ã³ãã³ãã衚瀺ããããšã¯åãå ¥ããããŸããã ãŸãããµã€ããã·ã£ãŒãã«ããã¡ãã¥ãŒé ç®ã®æ°ããã¶ã€ããŒã«æ瀺ããããšãã§ããŸããã ããã®ããã«æç»ããªãã§ãã ãããéåžžã«å€ãã®èŠçŽ ãããå Žåãã°ãªããå šäœã移åãããŸããã
ã³ã³ãã³ããã©ã®ãããªåœ¢åŒãåããç¹å®ã®ç¬éã«ã©ãã ãã«ãªããã¯èª°ãç¥ããŸããã ãŸããã¹ã¯ãªãããããã¹ãã®ããªãã³ã°ã«åžžã«äŸåã§ããããã§ã¯ãããŸãããããã¯ééã£ãŠããŸãã ã³ã³ãã³ãã®éã«äŸåããããã®ããã«å¿ èŠãªã¡ã«ããºã ãšããŒã«ãäœæããã®ã¯æ£ããããšã§ãã éçã»ã¬ã¯ã¿ãŒã¯ããã®ããã®ã¢ã€ãã¢ã®1ã€ã«ãããŸããã
Webãã¶ã€ã³ã¯åžžã«å€åæ§ãæè»æ§ãäžç¢ºå®æ§ã§ãã ãã®äžã§ãããªãã¯ãããããªããç¥ã£ãŠãããããç¥ããªãã ããã¯ããã©ãŒã ãå®çŸ©ããã®ã§ã¯ãªããäœããåãåŸããã©ãŒã ãäºæž¬ããäžçš®ã®èŠèŠçãªãã¶ã€ã³ã§ãããšããç¹ã§ãŠããŒã¯ã§ãã äžéšã®äººã«ãšã£ãŠãããã¯åãå ¥ãããããã®ã§ãããç解ã§ããªããã®ã§ãããããªãã«ãšã£ãŠãç§ã«ãšã£ãŠãææŠã§ããåã³ã§ãã