[CSS] ν˜•μ œμš”μ†Œλ‘œ 갯수 μ•Œμ•„λ‚΄κΈ°

2021. 3. 18. 18:15·🐣codeLab

2021.03.18

μš”μ†Œλ₯Ό νΌμ„ΌνŠΈλ‘œν•˜μ—¬ μ μ‘ν˜•μ΄λ‚˜ λ°˜μ‘ν˜•μ—μ„œλ„ 자유둭게 μ“Έ 수 μžˆλŠ” ν…œν”Œλ¦Ώμ΄λ‹€.

이해λ₯Ό ν™•μ‹€νžˆ ν•˜κ³ μž λ‹€μ‹œ μ •λ¦¬ν•˜κ²Œ λ˜μ—ˆλ‹€.

 

λ³Έ 기법은 λ‹€μ–‘ν•˜κ²Œ μ‘μš©ν•  수 μžˆλ‹€. 원본 λ¬Έμ„œμ—μ„œλŠ” li μš”μ†Œ κ°―μˆ˜μ— 따라 블둝 μ•ˆμ˜ 배경색을 λ‹€μ–‘ν•˜κ²Œ λ§€κΈ°λŠ” 것을 μ˜ˆμ‹œλ‘œ λ“€μ—ˆλ‹€.

flexboxλ₯Ό μ‚¬μš©ν•  수 μ—†λŠ” ν™˜κ²½μ΄λΌ κ°€μ •ν•œλ‹€.

 

λ‹€μŒ μ½”λ“œμ—μ„œλŠ” float: left λ₯Ό μ‚¬μš©ν•˜μ—¬ li μš”μ†Œλ“€μ„ λ„μš΄ μƒνƒœμ΄λ‹€.

li {
    float: left;
}

/* liκ°€ ν•˜λ‚˜μΌ λ•Œ */
li:first-child:nth-last-child(1) {
    width: 100%;
}

/* liκ°€ λ‘κ°œμΌ λ•Œ */
li:first-child:nth-last-child(2),
li:first-child:nth-last-child(2) ~ li {
    width: 50%;
}

/* liκ°€ 3개일 λ•Œ */
li:first-child:nth-last-child(3),
li:first-child:nth-last-child(3) ~ li {
    width: 33.3333%;
}

/* liκ°€ 4개일 λ•Œ */d d
li:first-child:nth-last-child(4),
li:first-child:nth-last-child(4) ~ li {
    width: 25%;
}
  • li:first-child:nth-last-child()
    • li쀑에 첫 번째 μžμ‹μ„ 선택, nth-last-child() λ₯Ό κ·Έ 뒀에 λ°”λ‘œ λΆ™μ—¬
      리슀트 μ•ˆμ˜ μžμ‹ μš”μ†Œ κ°―μˆ˜μ— λ”°λ₯Έ μŠ€νƒ€μΌ λΆ„κΈ°λ¬Έ 역할을 ν•œλ‹€.
    • 이 쑰건은 적어도 n개의 μžμ‹μš”μ†Œκ°€ μ‘΄μž¬ν•  λ•Œ 참이 λœλ‹€.
  • li:first-child:nth-last-child() ~ li
    • λ‚˜λ¨Έμ§€ ν˜•μ œ μš”μ†Œλ“€μ„ 선택, 같은 CSS λ₯Ό μ μš©ν•œλ‹€.

 

이λ₯Ό κ°„λž΅ν™”ν•˜μ—¬ μžμ‹ μš”μ†Œμ˜ 갯수λ₯Ό n개라 ν–ˆμ„ λ•Œ μ½”λ“œλŠ” λ‹€μŒκ³Ό κ°™λ‹€.

li:first-child:nth-last-child(n),
li:first-child:nth-last-child(n) ~ li {
    width: 1 / n * 100%;
}

 

 

β“λ§Œμ•½μ— 3개의 셀렉터에 margin 이 μžˆλ‹€λ©΄ μ–΄λ–»κ²Œ ν•΄μ•Ό 될까?

/*
 * 쑰건1. width, margin 값이 지정됨.
 * 쑰건2. 1개, 2개 일 λ•Œλ„ width κ°’ λ³€κ²½.
 * width: 348px; margin: 28px; inner(λ ˆμ΄μ•„μ›ƒ κΈ°μ€€κ°’): 1100px;
*/

li:first-child:nth-last-child(1),
li:first-child:nth-last-child(1) ~ li {
	width: 100%;
    margin: 0;
}

li:first-child:nth-last-child(2),
li:first-child:nth-last-child(2) ~ li {
    width: 48.6%;
    margin: 2.6%;
}

li:first-child:nth-last-child(3),
li:first-child:nth-last-child(3) ~ li {
    /* 348/1100*100 = 31.636363.. */
    width: 31.6%;

    /* 28/1100*100 = 2.545454.. */
    margin: 2.6%;
}

λŒ€μ²΄λ‘œ 1~3κΉŒμ§€ μ¨μ€˜μ•Ό 후에 μˆ˜μ •ν•  λ•Œ μ’‹λ‹€.

 

 

Reference

https://serisepomme.tistory.com/entry/CSS%EB%A1%9C-%EC%9E%90%EC%8B%9D-%EC%9A%94%EC%86%8C-%EA%B0%AF%EC%88%98-%EC%95%8C%EC%95%84%EB%82%B4%EA%B8%B0

μ €μž‘μžν‘œμ‹œ λΉ„μ˜λ¦¬ 동일쑰건 (μƒˆμ°½μ—΄λ¦Ό)
'🐣codeLab' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€
  • [SVG] svgλ₯Ό μ΄μš©ν•΄μ„œ μ°¨νŠΈλ§Œλ“€κΈ°
  • 2021.04.16 // fullpageScroll.js plugIn
  • var flag = false ν•΄μ£ΌλŠ” 이유
  • rollingSlide λ§Œλ“€κΈ°
jmjm
jmjm
  • jmjm
    /* jmjmjm */
    jmjm
  • 전체
    였늘
    μ–΄μ œ
    • λΆ„λ₯˜ 전체보기 (125)
      • πŸ‘©πŸ»‍πŸ’»STUDY (87)
        • HTML (9)
        • CSS (25)
        • SASS (4)
        • JavaScript (16)
        • jQuery (9)
        • Publishing (8)
        • Git (11)
        • React (3)
        • Vue (0)
        • μ›Ήμ ‘κ·Όμ„± (1)
        • Gulp (1)
      • 🐣codeLab (27)
      • 🌈etc (8)
      • πŸ₯° 일상 (2)
      • πŸŒŸμ½μ–΄λ³΄κΈ° (1)
  • λΈ”λ‘œκ·Έ 메뉴

    • ν™ˆ
    • νƒœκ·Έ
  • 링크

  • 곡지사항

  • 인기 κΈ€

  • νƒœκ·Έ

    javascript htmlcollection
    μžλ°”μŠ€ν¬λ¦½νŠΈ slidedown
    μžλ°”μŠ€ν¬λ¦½νŠΈ ν‘œν˜„μ‹λ¬Έ
    CSS μ„ νƒμž
    javascript slideup
    css κ°€μƒμ„ νƒμž
    css flex
    μ›Ήμ ‘κ·Όμ„±
    javascript μ»¬λ ‰μ…˜
    javascript htmlcollection nodelist
    sticky ν™œμš©
    javascript nodelist
    μžλ°”μŠ€ν¬λ¦½νŠΈ ν‘œν˜„μ‹ λ¬Έ
    sass
    sticky codepen
    μžλ°”μŠ€ν¬λ¦½νŠΈ slideup
    react import export
    아코디언메뉴
    html collection
    μ ‘κ·Όμ„± μ£Όμ˜μ‚¬ν•­
    vscode 단좕킀
    ν˜•μ œμΈμ ‘μž μ•ˆλ˜λŠ” 이유
    sass compiler
    sticky jsfiddle
    css ν˜•μ œμΈμ ‘μž μ•ˆλΌμš”
    ν˜•μ œμΈμ ‘μž display none
    javascript slidedown
    μžλ°”μŠ€ν¬λ¦½νŠΈ μ»¬λ ‰μ…˜
    HTML Form
    ν˜•μ œμΈμ ‘μž
  • 졜근 λŒ“κΈ€

  • 졜근 κΈ€

  • hELLOΒ· Designed Byμ •μƒμš°.v4.10.3
jmjm
[CSS] ν˜•μ œμš”μ†Œλ‘œ 갯수 μ•Œμ•„λ‚΄κΈ°
μƒλ‹¨μœΌλ‘œ

ν‹°μŠ€ν† λ¦¬νˆ΄λ°”