The selector is the part of an CSS commands that identifies the parts of a page to be formatted.
Simple Selectors
HTML tags can be specified:
h1 { font-family: 'Trebuchet MS', sans-serif; color: orange; }
id Selectors
An HTML element can be given an id – any id can only be used once on a page:
<p id="pageAuthor">Mr Thoresen</p>
CSS can then format that specific id:
#pageAuthor { text-align: right; color: grey; }
Class Selectors
An HTML id can only be used once on page.
A class can be be used several times on the same page.
<p class="caption">Photograph by Mr Thoresen</p> ... <p class="caption">Photograph by Ms Bailey</p>
CSS can then format all occurrences of that class:
.caption { text-align: center; color: grey; }
Grouping Selectors
If two or more elements will have the same formatting, the common elements can be grouped.
In this example, both h1 and h2 elements wll use the Trebuchet font, but only h1 elements will have orange text:
h1, h2 { font-family: 'Trebuchet MS', sans-serif; } h1 { color: orange; }
U
niversal Selector
The universal selector will apply styles to every page element.
Example: Browsers use default settings for margins and padding when displaying HTML elements. To override these defaults, a universal selector (*) can be used at the top of a stylesheet to set the margin and padding of every element to 0. This will allow students to witness only the margins and paddings they actually code.
* { padding: 0px; margin: 0px; }
![](https://glow-prod-sh.s3.eu-west-1.amazonaws.com/sh/public/ahscomputinghtml/uploads/sites/3275/2021/06/05211153/sqa-h-150x150.png?X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Security-Token=IQoJb3JpZ2luX2VjEP7%2F%2F%2F%2F%2F%2F%2F%2F%2F%2FwEaCWV1LXdlc3QtMSJIMEYCIQCpl3psmJv5BnsSpEX5sfAFWyUiFSdN8QldZOmoZatrlQIhAJmGsL3GQcK%2FxXLirzxUd6YUthw2D76LEs9ItsPekF59KsIFCPf%2F%2F%2F%2F%2F%2F%2F%2F%2F%2FwEQBRoMMjczNTcwMTk1NDMzIgzaUhYqUt3Tmakjle4qlgWLxytds6%2B1e7WGa0RaTJgVJ7WqzMnN814%2BiT9RyKWIrmMhqq5kgZZ9Dx0oxkUApwNmJsoeC6SaTmDGljYfHHeCuzzVnNDFVzeLjKDqxVwvr%2FqUPU2Djn5%2F8rVNvPndP54bvAw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A256&X-Amz-Credential=ASIAT7MQN47UZSA4P5UK%2F20240816%2Feu-west-1%2Fs3%2Faws4_request&X-Amz-Date=20240816T214525Z&X-Amz-SignedHeaders=host&X-Amz-Expires=900&X-Amz-Signature=d0119c674a139b11f7b6e9b1ece5fc91574fc3d8b48b64628aa01cfa12970c0c)
Descendant Selectors (a Combinator)
Descendant selectors give a list of two or more selectors, separated with spaces. The formatting rules are only applied to those selectors that are inside the previous selectors.
Example: This block will format only format list items that are inside an unsorted list that are inside a navigation block.
nav ul li { float:left; width:80px; text-align:center; }
This is used on the page for horizontal navigation bars.