WEB/CSS

[CSS] νŠΉμ •ν•œ 속성을 가진 μš”μ†Œλ“€μ„ 선택 : 속성 μ„ νƒμž

developer of the night sky 2023. 9. 26. 15:47

πŸ”»μ†μ„± μ„ νƒμžπŸ”»

νŠΉμ •ν•œ 속성을 가진 μš”μ†Œλ“€μ„ μ„ νƒν•˜κΈ° μœ„ν•΄ μ‚¬μš©λœλ‹€.

1. μ„ νƒμž[속성λͺ…]

2. μ„ νƒμž[속성λͺ…=κ°’]

3. μ„ νƒμž[속성λͺ…^=κ°’]

4. μ„ νƒμž[속성λͺ…$=κ°’]

5. μ„ νƒμž[속성λͺ…*=κ°’]

 

[곡톡 HTML]

<body>
    
    <div class="list">
        <a href="http://naver.com" target="_self">넀이버</a>
        <a href="http://yes24.com" target="_blank" title="링크">예슀24</a>
        <a href="https://daum.net">λ‹€μŒ</a>
        <a href="https://11st.co.kr">11λ²ˆκ°€</a>
        <a href="https://coupang.com" target="_blank">쿠팑</a>
    </div>

</body>

1. μ„ νƒμž[속성λͺ…]

    <style>
        a[target] {
            color: red;
        }

    </style>

target 속성 μ„€μ •λœ μš”μ†Œλ§Œ μŠ€νƒ€μΌ μ μš©λœλ‹€.


2. μ„ νƒμž[속성λͺ…=κ°’]

    <style>

        .list a {
            color: black;
            text-decoration: none;
            display: block;
            margin-bottom: 5px;
        }
        
        a[target="_blank"] {
            color: red;
        }
    </style>

target= "_blank"인 μš”μ†Œλ§Œ μŠ€νƒ€μΌ μ μš©λœλ‹€.


3. μ„ νƒμž[속성λͺ…^=κ°’]

    <style>

        a[href^="http://"] {
            color: red;
        }
    </style>

 

hrefκ°€ http://둜 μ‹œμž‘ν•˜λŠ” μš”μ†Œλ§Œ μŠ€νƒ€μΌ μ μš©λœλ‹€.


4. μ„ νƒμž[속성λͺ…$=κ°’]

    <style>
        
        a[href$='.com'] {
            color: red;
        }

    </style>

hrefκ°€ .com으둜 λλ‚˜λŠ” μš”μ†Œλ§Œ μŠ€νƒ€μΌ μ μš©λœλ‹€.


5. μ„ νƒμž[속성λͺ…*=κ°’]

    <style>
    
        a[href*=co] {
            color: red;
        } 

    </style>

hrefμ—μ„œ coκ°€ λ“€μ–΄κ°„ μš”μ†Œλ§Œ μŠ€νƒ€μΌ μ μš©λœλ‹€.

 

 


πŸ”»λ””μžμΈμ— μ μš©ν•˜κΈ°πŸ”»

1. ν•„μˆ˜ μž…λ ₯ ν•„λ“œ κ°•μ‘°ν•˜κΈ°

<body>
    
    <div id="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">
        <input type="text" value="6" disabled>
        <input type="text" value="8" >
        <input type="text" value="9">
        <input type="text" value="10" required>

    </div>
    <hr>

    <input type="button" value="λ²„νŠΌ">


</body>
    <style>
        input[type=text] {
            display: block;
            margin-bottom: 3px;
            border: 1px solid #555;
            background-color: #fff;
            padding: 3px;
            outline: none;
            color: #333;
        }

        input[type=text]:required {
            background-color: gold;
        }

        input[type=text]:optional {
            background-color: #ccc;
        }


    </style>

 


2. μž…λ ₯ν•˜μ§€ μ•ŠλŠ” ν•„λ“œμ™€ μž…λ ₯ν•˜λŠ” ν•„λ“œ κ΅¬λΆ„ν•˜κΈ°(1)

    <style>
        input[type=text] {
            display: block;
            margin-bottom: 3px;
            border: 1px solid #555;
            background-color: #fff;
            padding: 3px;
            outline: none;
            color: #333;
        }
        input[type=text]:disabled {
            background-color: #999;
        }

        /* readonly도 enabled에 μ†ν•œλ‹€ */
        input[type=text]:enabled {
            background-color: gold;
        }



    </style>

 


3. μž…λ ₯ν•˜μ§€ μ•ŠλŠ” ν•„λ“œμ™€ μž…λ ₯ν•˜λŠ” ν•„λ“œ κ΅¬λΆ„ν•˜κΈ°(2)

    <style>
        input[type=text]:read-only {
            background-color: gold;
        }

        input[type=text]:read-write {
            background-color: #ccc;
        }

    </style>


4. μž…λ ₯ ν•„λ“œ 속성에 따라 마우슀 포인터 λ°”κΎΈκΈ°

    <style>
        input[type=text]:read-only {
            background-color: gold;
        }

        input[type=text]:read-only {
            cursor: not-allowed;
        }

    </style>


πŸ”»μ²΄ν¬λ°•μŠ€, λΌλ””μ˜€, μ…€λ ‰νŠΈ css μ μš©ν•˜κΈ°πŸ”»

μ²΄ν¬λ°•μŠ€, λΌλ””μ˜€, μ…€λ ‰νŠΈλŠ” css 적용이 잘 μ•ˆλœλ‹€.

 

    <div>
        <input type="checkbox" class="cb">
        <input type="checkbox" class="cb">
        <input type="checkbox" class="cb">
    </div>
<style>
        .cb {
            border: 1px solid red;
            width: 100px;
            height: 100px;
            background-color: gold;
        }

    </style>

크기만 μ μš©λ˜μ—ˆλ‹€.


1. checked + opacity

    <style>
        .cb {
            /* 뢈투λͺ…도(0~1)*/
            opacity: 0.2;
        }

        .cb:checked {
            opacity: 1;
        }

    </style>


2. checked + visibility

<style>
        #cat {
            visibility: visible;
        }

        #cd-cat:checked + #cat {
            visibility: hidden;
        }

    </style>