ν”„λ‘œκ·Έλž˜λ° 곡뢀/HTML&CSS

[ex34~37] CSS(selector-nth:child,접은글,::before)

λŒ€μž₯μΏ΅μ•Ό 2023. 4. 18. 09:51

ex34

 

가상 클래슀
1. :first-child > μžμ‹ μ΄ μžμ‹ μ˜ κ·Έλ£Ήμ—μ„œ μ²«λ²ˆμ§Έ μžμ‹μΈμ§€?
2. :last-child > μžμ‹ μ΄ μžμ‹ μ˜ κ·Έλ£Ήμ—μ„œ λ§ˆμ§€λ§‰ μžμ‹μΈμ§€?
3. :nth-child() > 1λΆ€ν„° μ‹œμž‘
4. :nth-last-child() > λ’€μ—μ„œλΆ€ν„°

n
1. μˆ«μž: index(μœ„μΉ˜) > 1λΆ€ν„° μ‹œμž‘
2. μˆ˜μ—΄: 2n, 2n+1 (0λΆ€ν„° μ‹œμž‘)
3. μˆ˜μ—΄: even, odd

 

μ½”λ“œ

더보기
    <style>

        li:nth-child(odd) {
            color: orange;
        }




        /* 
        li:first-child {
            color: orange;
        }

        li:last-child {
            color: coral;
        }

        li:nth-child(3) {
            color: blue;
        }

        li:nth-last-child(3) {
            color: gold;
        } 
        */

        .tbl {
            border: 1px solid black;
            border-collapse: collapse;
        }

        .tbl td {
            border: 1px solid black;
            padding: 5px;
        }

        /* .tbl tr:first-child {
            background-color: gold;
        } */

        /* λͺ¨λ“  tdλ“€μ˜ 첫째듀 > μ²«λ²ˆμ§Έμ—΄ */
        /* .tbl td:first-child {
            background-color: tomato;
        } */
        
        /* λ‹€μ„―λ²ˆμ§Έμ—΄ */
        /* .tbl td:nth-child(5) {
            background-color: tomato;
        } */

        /* μ§μˆ˜ν–‰ */
        /* .tbl tr:nth-child(even) {
            background-color: #DDD;
        } */

        /* μ§μˆ˜ν–‰μ΄λ©΄μ„œ ν™€μˆ˜μ—΄ */
        .tbl tr:nth-child(even) td:nth-child(odd) {
            background-color: gold;
        }
        
        /* ν™€μˆ˜ν–‰μ΄λ©΄μ„œ μ§μˆ˜μ—΄ */
        .tbl tr:nth-child(odd) td:nth-child(even) {
            background-color: gold;
        }

        
    </style>
</head>
<body>
    <!-- ex34_selector.html -->

    <h1>λͺ©λ‘</h1>

    <ul class="list1">
        <li>κ°•μ•„μ§€</li>
        <li>고양이</li>
        <li>λ‹€λžŒμ₯</li>
        <li>μ†Œ</li>
        <li>돼지</li>
        <li>말</li>
        <li>코끼리</li>
        <li>μ‚¬μž</li>
        <li>ν˜Έλž‘μ΄</li>
        <li>λŠ‘λŒ€</li>
    </ul>

    <h1>ν…Œμ΄λΈ”</h1>

    <!-- table.tbl>tr*20>td{item}*10 -->
    <table class="tbl">
        <tr>
            <td>item</td>
            <td>item</td>
            <td>item</td>
            <td>item</td>
            <td>item</td>
            <td>item</td>
            <td>item</td>
            <td>item</td>
            <td>item</td>
            <td>item</td>
        </tr>
        <tr>
            <td>item</td>
            <td>item</td>
            <td>item</td>
            <td>item</td>
            <td>item</td>
            <td>item</td>
            <td>item</td>
            <td>item</td>
            <td>item</td>
            <td>item</td>
        </tr>
        <tr>
            <td>item</td>
            <td>item</td>
            <td>item</td>
            <td>item</td>
            <td>item</td>
            <td>item</td>
            <td>item</td>
            <td>item</td>
            <td>item</td>
            <td>item</td>
        </tr>
        <tr>
            <td>item</td>
            <td>item</td>
            <td>item</td>
            <td>item</td>
            <td>item</td>
            <td>item</td>
            <td>item</td>
            <td>item</td>
            <td>item</td>
            <td>item</td>
        </tr>
    </table>

</body>
</html>

 

ν™”λ©΄

더보기

λͺ©λ‘

  • κ°•μ•„μ§€
  • 고양이
  • λ‹€λžŒμ₯
  • μ†Œ
  • 돼지
  • 말
  • 코끼리
  • μ‚¬μž
  • ν˜Έλž‘μ΄
  • λŠ‘λŒ€

ν…Œμ΄λΈ”

item item item item item item item item item item
item item item item item item item item item item
item item item item item item item item item item
item item item item item item item item item item

ex35

 

속성 μ„ νƒμž
1. μ„ νƒμž[속성λͺ…]     : ν•΄λ‹Ή μ†μ„±μ„ λͺ…μ‹œν–ˆλŠ”μ§€?
2. μ„ νƒμž[속성λͺ…=κ°’]  : ν•΄λ‹Ή μ†μ„±κ³Ό κ°’을 λͺ…μ‹œν–ˆλŠ”μ§€?
3. μ„ νƒμž[속성λͺ…^=κ°’] : μ†μ„±κ°’이 κ°’μœΌλ‘œ μ‹œμž‘ν•˜λŠ”μ§€?
4. μ„ νƒμž[속성λͺ…$=κ°’] : μ†μ„±κ°’이 κ°’μœΌλ‘œ λλ‚˜λŠ”μ§€?
5. μ„ νƒμž[속성λͺ…*=κ°’] : μ†μ„±κ°’이 ν¬ν•¨λ˜λŠ”μ§€?

 

/* target 속성을 κ°€μ§€κ³ μžˆλŠ” a νƒœκ·Έ */
.list a[target] { background-color: gold;}
/* 속성값 쑰건  */
.list a[target=_blank] { background-color: gold;}
/* 속성값이 ~둜 μ‹œμž‘ν•˜λŠ”μ§€ */
.list a[href^=https] { background-color: gold;}
/* 속성값이 ~둜 λλ‚˜λŠ”μ§€ */
.list a[href$='.com'] { background-color: gold;}
/* 속성값에 ~값이 ν¬ν•¨λ˜λŠ”μ§€ */
.list a[href*=m] { background-color: gold;}

ex36

 

μ ‘κΈ°

 

μ½”λ“œ

더보기

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .list input[type=text] {
            display: block;
            border: 1px solid #555;
            padding: 3px;
            margin-bottom: 3px;
            background-color: white;
            color: #444;
            outline: none;
        }

        /* optional : requiredκ°€ μ•„λ‹Œκ²ƒ */
        /* .list input[type=text]:required { background-color: gold; } */
        /* .list input[type=text]:optional { background-color: #CCC; } */

        /* .list input[type=text]:disabled { background-color: #999; }
        .list input[type=text]:enabled { background-color: gold; } */

        /* .list input[type=text]:read-only { cursor: not-allowed; }
        .list input[type=text]:read-write { background-color: gold; } */
        

        input[type=checkbox] {
            /* 거의 μ•ˆλ¨ */
            /* border: 1px solid black; */
            /* background-color: blue; */

            /* width: 200px; */

            /* 뢈투λͺ…도(0~1) */
            /* opacity: 0.2; */
        }

        input[type=checkbox]:checked {
            opacity: 1;
        }

        .cb-cat + .cat {
            visibility: hidden;
        }
        
        .cb-cat:checked + .cat {
            visibility: visible;
        }

        .cb1 + div {
            /* visibility: hidden; */

            /* μ•ˆλ³΄μΌλ•Œ 곡간 μ°¨μ§€x */
            display: none;
        }
        
        .cb1:checked + div {
            /* visibility: visible; */
            display: block;
        }


        .item {
            border: 1px solid #555;
            width: 200px;
        }

        .item #cb2 {
            display: none;
        }

        .item label {
            font-size: 1.5rem;
            font-family: Arial;
            font-variant: small-caps;
            display: block;
            padding: 10px;
            background-color: greenyellow;
            cursor: pointer;
        }

        .item div {
            padding: 12px;
            border-top: 1px solid #555;
            display: none;
        }

        .item #cb2:checked + label {
            background-color: gold;
        }

        /* cb2κ°€ 체크되면 ν˜•μ œμΈ label의 ν˜•μ œμΈ divκ°€ 보인게 */
        .item #cb2:checked + label + div {
            display: block;
        }


        
    </style>
</head>
<body>
    <!-- ex36_selector.html -->

    <div class="item">
        <input type="checkbox" class="cb2" id="cb2">
        <label for="cb2">Html</label>
        <div>Lorem ipsum dolor sit amet consectetur adipisicing elit. Aspernatur architecto et magni dolores eligendi harum ducimus odio quos sed quod dolorum error consectetur rem, libero cupiditate repellendus rerum voluptatem vero!</div>
    </div>



    <hr>

    제λͺ© <input type="checkbox" class="cb1">
    <div>Lorem ipsum, dolor sit amet consectetur adipisicing elit. Optio, commodi ab! Molestias tempora tenetur nesciunt quod numquam recusandae quibusdam, dolore perferendis maxime esse quisquam dignissimos quos ipsa odit optio delectus.</div>

    <details>
        <summary>제λͺ©</summary>
        <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Expedita neque deleniti impedit! Vel voluptatibus quo magnam in, nobis, quod ex culpa, error asperiores odio ut voluptatum exercitationem nesciunt. Delectus, ad.</p>
    </details>

    <div>
        <input type="checkbox" class="cb-cat">
        <img src="images/catty01.png" class="cat">
    </div>

    <div>
        <input type="checkbox">
        <input type="checkbox">
        <input type="checkbox">
    </div>

    <div class="list">
            <input type="text" value="1" required>
            <input type="text" value="2" readonly>
            <input type="text" value="3" readonly>
            <input type="text" value="4">
            <input type="text" value="5" required>
            <input type="text" value="6" disabled>
            <input type="text" value="7" disabled>
            <input type="text" value="8">
            <input type="text" value="9">
            <input type="text" value="10" required>
    </div>
</body>
</html>

 

ν™”λ©΄

더보기
Document
Lorem ipsum dolor sit amet consectetur adipisicing elit. Aspernatur architecto et magni dolores eligendi harum ducimus odio quos sed quod dolorum error consectetur rem, libero cupiditate repellendus rerum voluptatem vero!

제λͺ©
Lorem ipsum, dolor sit amet consectetur adipisicing elit. Optio, commodi ab! Molestias tempora tenetur nesciunt quod numquam recusandae quibusdam, dolore perferendis maxime esse quisquam dignissimos quos ipsa odit optio delectus.
제λͺ©

Lorem ipsum dolor sit amet consectetur adipisicing elit. Expedita neque deleniti impedit! Vel voluptatibus quo magnam in, nobis, quod ex culpa, error asperiores odio ut voluptatum exercitationem nesciunt. Delectus, ad.


ex37

 

CSS μ„ νƒμž(가상 ν΄λž˜μŠ€)
1. :first-child
    - κ°€μƒ ν΄λž˜μŠ€
    - κ²€μƒ‰λœ νƒœκ·Έ μžμ²΄λ₯Ό μ‘°μž‘
2. ::before
    - κ°€μƒ μš”μ†Œ(Element)
    - κ²€μƒ‰λœ νƒœκ·Έ μ£Όλ³€μ„ μ‘°μž‘

μ „ν›„ μ„ νƒμž
1. ::before(:before)
2. ::after(:after)

 

μ „μ²΄μ½”λ“œ 

더보기
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>

        span {
            color: blue;
        }

        span::before {
            content: '[';
        }
        span::after {
            content: ']';
        }

        /* μ•žμ— μ‚½μž… */
        /* span::before {
            content: '홍길동';
            color: orange;
            font-weight: bold;
            font-size: 1.5rem;
        }
        /* 뒀에 μ‚½μž… */
        /* span::after {
            content: 'μ•„λ¬΄κ°œ';
            color: pink;
        } */ 

        /* 곡백제거 방법1 > font-size: 0; */
        /* #list {
            font-size: 0;
        }
        
        #list div {
            width: 100px;
            height: 100px;
            display: inline-block;
            font-size: 1rem;
        } */
        
        
        /* 곡백제거 방법2 > float */
        #list .item {
            width: 100px;
            height: 100px;
            float: left;
        }

        /* float λ’·μ²˜λ¦¬ μš©λ„ */
        #list::after {
            content: '';
            display: block; /* clearλŠ” λΈ”λŸ­νƒœκ·Έμ—λ§Œ 뢙일 수 있음*/
            clear: left;
        }


        #list div:nth-child(1) { background-color: tomato;}
        #list div:nth-child(2) { background-color: gold;}
        #list div:nth-child(3) { background-color: cornflowerblue;}
        #list div:nth-child(4) { background-color: greenyellow;}
        #list div:nth-child(5) { background-color: plum;}



    </style>
</head>
<body>
    <!-- ex37_selector.html -->
    <p>Lorem <span>ipsum</span> dolor sit amet consectetur, adipisicing elit. Eaque assumenda eligendi <span>porro</span> rerum consequuntur? Dolor, nihil <span>doloribus</span>. Dolore culpa error cum unde amet voluptas atque libero, eius qui in eveniet!</p>

    <hr>

    <div id="list">
        <div class="item">λΉ¨κ°•</div>
        <div class="item">λ…Έλž‘</div>
        <div class="item">νŒŒλž‘</div>
        <div class="item">초둝</div>
        <div class="item">보라</div>
        <!-- <div style="clear:left;"></div> -->
    </div>

    <div>λ‹€λ₯Έ λ‚΄μš©λ“€..</div>

</body>
</html>