Eliter Eliter - 12 days ago 4
CSS Question

Can you use the same ID with different element types in CSS?

I know this is invalid HTML:

<p id="sally">paragraph</p>
<p id="sally">paragraph</p>

Is this also invalid?

<div id="sally">
<p id="sally">sally paragraph, inside of sally div</p>
<p>paragraph with nothing special.</p>

and something like

width: 90%;
height: 3em;
padding: 20px;
font-size: 1em;
color: red;


I know this is invalid HTML


Is this also invalid?

Yes. An ID must be unique in the document. Not unique per type in the document.

(That won't stop browser error recovery from sometimes giving you the result you are trying to achieve, but you should not depend on that).

and something like

You can't use .#. A . starts a class selector. A # starts an ID selector.

You can have

div#sally { ... }
p#sally { ... }

And then have two different documents, one with a div with that ID and one with a paragraph with that ID, but both of which <link> to the same stylesheet.

If you want to identify two things are being in a group, then use a class. That is what class is for.