Search code examples
cssinputlabel

How to hide element label by element id in CSS?


Let's say I've got this code

<table>
    <tr>
        <td><input id="foo" type="text"></td>
        <td><label for="foo">This is foo</label></td>
    </tr>
</table>

This will hide the input:

#foo { display: none;}  /* or hidden could work too, i guesss */

How do I hide the label?


Solution

  • If you give the label an ID, like this:

    <label for="foo" id="foo_label">
    

    Then this would work:

    #foo_label { display: none;}
    

    Your other options aren't really cross-browser friendly, unless javascript is an option. The CSS3 selector, not as widely supported looks like this:

    [for="foo"] { display: none;}