Targeting Elements:
- Selectors are used to target specific elements on a webpage.
- You define selectors followed by curly braces {} where you specify the properties and their values.
selector {
property: value;
/* more properties... */
}
Targeting by Class:
- You can target elements with a specific class using a dot (.) followed by the class name.
- This is useful when you want to style multiple elements with the same class.
.class1 { }
Targeting by Multiple Classes:
- You can target elements that have multiple classes by chaining the class names together without spaces.
.class1.class2 { }
Targeting by Element Type:
- You can target elements based on their tag names directly.
- This will style all elements of that type on the page.
div { }
Targeting by ID:
- You can target elements with a specific ID using a hash (#) followed by the ID name.
- IDs should be unique within a page.
#id { }
Targeting by Attribute Presence:
- You can target elements based on the presence of an attribute regardless of its value.
[attr] { }
Targeting by Attribute Value:
- You can target elements with a specific attribute value.
[attr='value'] { }
Targeting by Attribute Value Prefix (CSS 3):
- You can target elements with an attribute whose value starts with a specific string.
[attr^='val'] { }
Targeting by Attribute Value Suffix (CSS 3):
- You can target elements with an attribute whose value ends with a specific string.
[attr$='ue'] { }
Targeting by Attribute Value Containing a Word:
- You can target elements with an attribute containing a specific word in a space-separated list of values.
[otherAttr~='foo'] { }
Targeting by Attribute Value Containing a Dash-Separated Word (CSS 3):
- You can target elements with an attribute containing a specific word in a dash-separated list of values.
[otherAttr|='en'] { }