[DAY - 6] background 다중 이미지, 선택자(first-child, last-child, nth-child(), first-of-type

2023. 3. 11. 02:43·🔥 부트캠프-웹 개발 🔥/CSS3
  • background 다중 이미지
  • 선택자(first-child, last-child, nth-child(), first-of-type)
  • ::before ,::after
  • vertical-align
  • w00p=width: 100%;
  • h100p=height: 100%

 

1) background 다중 이미지

 
background 한 줄만으로 여러 사진을 각각 다른 위치에 불러올 수 있다.

background 다중 이미지

 
부모 박스의 크기가 서로 다른 배경에 총 3가지 이미지를 조합해 만들어 보았다.

            background: 
            url(images/img1.png) 0 0 no-repeat,
            url(images/img3.png) right 0 no-repeat,
            url(images/img2.png) 0 0 repeat-x;

img1은 0 0 위치에서 부터 반복 없이 한 번만(no-repeat), img3은 x축을 오른쪽에 두고 마찬가지로 반복 없음
그리고 img2는 가운데 부분인데 repeat-x 를 사용해 x축(가로)으로 부모의 박스 크기만큼 반복되어 채워지게 했다.
 

img2를 없앤 경우

 
 

2) 선택자(first-child, last-child, nth-child(), first-of-type)

    <ul class="list">
        <li>list1</li>
        <li>list2</li>
        <li>list3</li>
        <li>list4</li>
        <li>list5</li>
    </ul>

 
만약 위의 상황에서 원하는 list만 효과를 주고 싶다면 선택자를 사용하면된다.
 
first-child {} : 첫 번째
last-child {} : 마지막
nth-child() {} : () 안에 넣은 숫자
 
*nth-child는 odd(홀수), even(짝수) 뿐만 아니라 2n+1(3, 5, 7... 번째), n+2(3, 4, 5... 번째) 등과 같이 수식도 사용 가능하다
 

        .list li:first-child {
            background: orange;
        }
        .list li:last-child {
            background: skyblue;
        }
        .list li:nth-child(3) {
            background: gray;
        }
        .list li:nth-child(4) {
            background: aqua;
        }

 
하지만 예시처럼 ul과 li로만 이루어진 것이 아닌 다른 형제 요소가 있다면 nth-child 사용이 힘들어진다.

     <div class="con-box">
        <h2>타이틀</h2>
        <h3>타이틀2</h3>
        <p>설명입니다</p>
        <p>설명입니다</p>
     </div>

 
p의 형제가 h2와 h3가 있는 경우 첫 번째 p에만 따로 효과를 위에 방식(nth-child)대로 할 경우
 

        .con-box p:nth-child(1) {
            background-color: yellow;
        }
        .con-box p:nth-child(2) {
            background-color: orange;
        }

 
nth-child(1)을 h2인 타이틀로, nth-child(2)를 h3인 타이틀2로 인식하기 때문에 생각대로 적용이 되지 않는다.
이처럼 다른 형제 요소도 있을 땐 같은 타입 중에서 고를 수 있는 nth-of-type를 사용한다.
 

        .con-box p:nth-of-type(1) {
            background: yellow;
        }
        .con-box p:nth-of-type(2) {
            background: orange;
        }

 
*first-of-type, last-of-type도 사용 가능하다.
 
 

3) ::before , ::after

 

::before ,::after 예시 . daum

 
위 사이트처럼 사이에 점 혹은 네모 박스, 세로줄이 있는 경우가 있다. 이를 구현하는 방법 중 이미 다 만들어진 틀 앞, 뒤에 새로운 요소를 추가하는 방법인 ::before ,::after를 배웠다.
 

::before ,::after

 

     <ul class="top-menu">
        <li>
            <a href="#">홈</a>
        </li>
        <li>
            <a href="#">로그인</a>
        </li>
        <li>
            <a href="#">회원가입</a>
        </li>
        <li>
            <a href="#">고객문의</a>
        </li>
        <li>
            <a href="#">사이트맵</a>
        </li>
     </ul>

 
이후 ::after를 사용하여 세로줄을 만들어 보았다.
*둘 다 inline성격을 띠며 content:""가 필수이다. 그래서 아래처럼 텍스트 없이 단순히 색만 넣고 싶더라도 공백으로 두어야 한다.
 

        .top-menu li::after {
            content:"";
            display: inline-block;
            width: 1px;
            height: 10px;
            background-color: indianred;
            margin-left: 15px;
        }

 
이렇게 되면 마지막 li인 사이트맵 뒤에도 ::after가 적용이 되는데 이를 지우려면 위에서 배웠던 :last-child를 사용하면 된다.
 

        .top-menu li:last-child::after {
            display: none;
        }

 

4) vertical-align

 
수직 가운데 정렬

        <p>
            <img src="images/GS.jpg" alt="">
            <strong>test</strong>
        </p>
        <p>
            <img src="images/GS.jpg" alt="">
            <strong>test</strong>
        </p>
        <p>
            <img src="images/GS.jpg" alt="">
            <strong>test</strong>
        </p>
        <p>
            <img src="images/GS.jpg" alt="">
            <strong>test</strong>
        </p>

 
css에서 img와 strong의 부모인 p를 박스로 잡아주고, 이미지와 텍스트를 넣었을 때, 각각 다른 p의 텍스트의 수직 정렬을 하고 싶을 때 사용하면 된다.
 
우선 nth-child를 사용해 몇 번째 이미지의 텍스트를 정렬할지 정하고 vertical-align를 적용시켰다.
 
*img를 통해 순서를 찾고 p의 위치를 변경한다는 게 잘 이해가 되지 않고 직접 적용하기에도 어려움이 많은 부분이었다.
 

        .box p:nth-child(1) img{
            vertical-align: top;
        }
        .box p:nth-child(2) img{
            vertical-align: middle;
        }
        .box p:nth-child(3) img{
            vertical-align: bottom;
        }
        .box p:nth-child(4) img{
            vertical-align: -50px;
        }

5) tips

  • w00p=width: 100%;
  • h100p=height: 100%
'🔥 부트캠프-웹 개발 🔥/CSS3' 카테고리의 다른 글
  • [DAY - 10] position: absolute, fixed, z-index
  • [DAY - 7] 내부/외부링크, visibility, float, display, float, flex, justify-content, align-c
  • [DAY - 5] 구글 폰트, border-radius, box-shadow/text-shadow, overflow, text-overflow, background
  • [DAY - 4] 다중 클래스, 컬러, 단위, line-dhight, letter-spacing, white-space, word-break, text-transform, display
Yeonhub
Yeonhub
✨ https://github.com/yeonhub 📧 lsy3237@gmail.com
  • Yeonhub
    비 전공자의 Be developer
    Yeonhub
  • 전체
    오늘
    어제
    • 전체보기 (169)
      • 🔍 Tech 🔍 (19)
        • Front-End (11)
        • Back-End (4)
        • AI (1)
        • Server (1)
        • Etc (2)
      • 💡 원티드 프리온보딩 챌린지 💡 (14)
        • PRE-ONBOARDING_AI (11월) (1)
        • PRE-ONBOARDING_FE (2월) (2)
        • PRE-ONBOARDING_FE (1월) (2)
        • PRE-ONBOARDING_FE (12월) (9)
      • 🔥 부트캠프-웹 개발 🔥 (118)
        • HTML5 (7)
        • CSS3 (21)
        • JavaScript (27)
        • JavaScript_advanced (9)
        • React (24)
        • Next (1)
        • MYSql (5)
        • Node (5)
        • 오늘하날(개인프로젝트) (12)
        • 이젠제주투어(팀프로젝트) (7)
      • 💻 CS 💻 (1)
        • 알고리즘 (1)
      • ⚡ 코딩테스트 ⚡ (11)
        • JavaScript (11)
      • 📚 Books 📚 (6)
        • 클린 아키텍처 (2)
        • 인사이드 자바스크립트 (4)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

    • Github
  • 공지사항

  • 인기 글

  • 태그

    php node
    node fcm
    rn bottom sheet
    expo fcm push
    프론트엔드 테스트코드
    expo map
    python node
    react native firebase analytics
    react native analytics
    컴파운드 컴포넌트 패턴
    expo deep linking
    expo 길찾기
    node crontab
    node.js fcm
    react native admob
    rn admob
    expo node fcm
    expo google map
    expo fcm
    expo admob
    react native expo fcm
    react native bottom sheet
    react vite
    라스콘4
    bottom sheet
    expo 지도
    javascript fcm
    Node
    node cron
    라스콘
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
Yeonhub
[DAY - 6] background 다중 이미지, 선택자(first-child, last-child, nth-child(), first-of-type
상단으로

티스토리툴바