How to target tags, classes and IDs
Critical knowledge
You can style your content by targeting elements in one of three ways: HTML tags; classes or IDs. We'll use this HTML as an example:
- <div class="panel" id="userAccountBox">
- <h3>User account</h3>
- <p>Control your settings and look after your account</p>
- <ul>
- <li><a href="">Account settings</a></li>
- <li><a href="">Personal details</a></li>
- <li><a href="" class="logout">Logout</a></li>
- </ul>
- </div>
Target: element
Before we get into the detail, there is one more thing to be aware of: structuring our CSS. For each and every element, class or id we wish to give style to, this is how we write it:
- div {
- ...
- }
The div is where we would write the tag name (like this example), or class or ID. The curly brackets hold the styles that this 'div' should receive. The ellipsis is where our CSS rules will go. There is more detail on structure, later on in this article.
For every style we add to our CSS file, we just put after the existing ones. It pays to keep the styles in an order that loosely matches your HTML document's flow of markup, start with body, then h1, then p, then your classes. For example:
- body {
- ...
- }
- h1 {
- ...
- }
- p {
- ...
- }
- .panel {
- ...
- }
You've seen how we target a tag in the previous example, so let's demonstrate other methods.
Target: class
Staying with our first example, this is how we would target the class rather than the tag:
- .panel {
- ...
- }
Assigning styles to a particular class name requires that we preface it with a period and only a space between it and the opening curly bracket.
Target: tag with class
If we wanted to be specific about targeting only <div>s with the class panel we could do this:
- div.panel {
- ...
- }
See how the tag name precedes the period and the class name? By using this method, we've narrowed the target and the styles will only be applied to <div>s with the class of panel.
This can be a useful method, but you're encouraged to create meaningful class names so as to avoid this unnecessary level of detail when targeting elements and avoid further difficulties when editing the CSS at a later date.
Target: id
If we want to target the <div>'s id, we just do this:
- #userAccountBox {
- ...
- }
Instead of using a period, we use the number symbol (or, in digital patois, the hash sign). For any id that requires specific styles for just that element, this is how we target it.
If you have styles applied for an element with a class and an id, any attributes that match (of which we'll get onto later), like background-color or margin, will be trumped by the id. That is to say, style applied to an id will overrule any existing styles assigned via a tag or class. This is a prime example of the rules of the cascade – the final 'hook' in the flow of styles, takes the prize.
Target: element inside another
The broad method is to target all tags that match. In the example below all <p>s inside all <div>s will gain the styles we apply here. Note the use of spaces to separate our targets.
- div p {
- ...
- }
Target: element inside another
If we only wanted to target the <p> of our first example, we can use the id of its parent (the <div>) to narrow down the target.
- #userAccountBox p {
- ...
- }
Leaving a comment
Sometimes it can be helpful to leave very brief comments in your CSS, to help you and others that edit it to know what they're editing. Write your comment surrounded by forward slashes and asterisks. Here's an example:
- /* My brief comment */
- div.panel {
- ...
- }
You could even use comments to temporarily 'turn off' parts of your CSS. This can be useful when you don't want to delete styles during your design and/or testing process. However, if pays to clear out any unused styles once you're ready to release the CSS for launch.
Wrap the styles you don't wish to work, like so:
- /* Don't need this style
- div.panel {
- ...
- }
- */