Using class identifiers in CSS is a straightforward process. You can define a class by adding a dot (.) before the class name in your CSS code.
A class can be applied to multiple HTML elements, making it a convenient way to style groups of elements consistently. This is especially useful for styling navigation menus, buttons, or other repetitive elements on your website.
In CSS, class identifiers are defined using the dot notation. For example, if you want to define a class called "header", you would use the code ".header { ... }". This defines the styles for any HTML element with the class "header".
By using class identifiers, you can keep your CSS code organized and efficient.
Selectors
Selectors are a fundamental part of CSS, allowing you to target specific elements on your webpage.
You can combine selectors in any way you like to create complex rules. For instance, you can select list items in a specific list, optional items in that list, and unvisited links with a certain class.
The class selector is used to select items based on their assigned class, which is similar to the ID attribute but can be reused as many times as desired on a page.
To use the class selector, type a period followed by the tag's class in front of the CSS block.
Classes can be selected in CSS by writing a period followed by the name of the class, without needing to write the name of the element.
You can apply the same CSS styles to several different elements at once using a selector list, which is written by comma-separating each individual selector.
Selector lists can include any type of selector or combinator, and it's okay to mix selector types.
To select for all h2 elements inside a section element and all p elements inside a section, you would need to write section h2, section p.
You can use specific selectors to customise styles in a specific section or element on your webpage, such as using a page section class and ID.
By using the right selectors, you can target specific elements on your webpage and apply custom styles to them.
Combinators
Combinators are a powerful tool in CSS that allow you to create complex selectors by combining multiple selectors together. This can be done in any way you like, as seen in the example of combining selectors to affect list items in a specific class.
Decendant combinators are particularly useful for selecting elements that are nested within other elements. They are written as a space-separated series of selectors, with the parent selector first. For instance, div h2 selects for any h2 element that is a decendant of a div element.
You can use any type of selector as part of a decendant relationship, including classes and elements. For example, .product-item h3 selects for any h3 that is a decendant of an element with a class of product-item.
Decendant Combinator
The decendant combinator is a powerful tool in CSS that allows you to select for an element that is a child, grandchild, or further decendant of one or more specific selectors.
It's written as a space-separated series of selectors, with the parent first. For instance, div h2 selects for any h2 element that is a decendant of a div element.
You can use any type of selector as part of a decendant relationship, such as a class as the parent and an element as the decendant: .product-item h3.
It's not necessary to include intermediate elements from the family tree if you're selecting for deep nesting. For example, ul li span could be written as ul span.
Any type of selector or combinator can be used in a decendant combinator, and it's okay to mix selector types. For example, blockquote, .featured-paragraph selects for all blockquote elements and all elements with a class of featured-paragraph.
Each comma in a selector list completely resets the selector, so be careful when using the decendant combinator or other combinators in a selector list.
First Child
The first child combinator is a powerful tool in CSS selectors. It matches if the given element is the first child of its parent.
The syntax for the first child combinator is :first-child. This can be used in combination with other selectors to create more specific rules.
You can use the first child combinator to target the first item in a list, like this: ul#main_navigation > li:first-child. This will match the first element in the list.
The first child combinator can be used to create a unique visual effect for the first item in a list.
Here are some examples of how to use the first child combinator:
- Home
- About
- Contact
In this example, the first child combinator would match theHome element, since it is the first child of its parent.
Last-Child
The last-child combinator is a powerful tool in CSS that allows you to target the last child element of a parent element.
It's the reverse of the first-child combinator, and it's used in the same way, but with a different outcome.
The last-child combinator is denoted by the syntax ":last-child" and can be used in combination with other selectors.
For example, "ul#main_navigation > li:last-child" matches if the click lands onDeath metal since it is the last child element of its parent.
This combinator is useful when you need to apply styles to the last element in a list or a series of elements.
It's a simple yet effective way to add a touch of uniqueness to your designs.
Class
The class attribute is used to assign an element to a "family" of similar elements, unlike the ID attribute which assigns a "name" for a single element. This attribute can be re-used as many times as desired on a given page.
The CSS Class Selector is used to select items based on their assigned class, and is appropriate for selecting a hand-picked collection of elements. To use the class selector, type a period (.) followed by the tag's class in front of the CSS block.
Classes can be applied to one or more HTML elements using an attribute, and can be selected in CSS by writing a period (.) followed by the name of the class. For example, if an element has a class name of "special-heading", you can select it in CSS by writing .special-heading.
A class selector can be used to select just one or just a few specific instances of an element, rather than all instances. This is useful when you want to style a specific element differently than its siblings.
You can use a class selector to customise the styles in a specific section of a page, by adding the class name to the CSS block. For example, if a page section has a class name of "page-section", you can add the following code to your custom CSS: .page-section[data-section-id="5e1230f488456a2a6f5faf3b"] { h3 {color:red;} }.
Querying Elements
You'll often use document.querySelector and document.querySelectorAll with CSS selectors.
These two JavaScript methods are used to fetch elements on a page that match a given selector.
document.querySelector returns the first element on the page that matches the given selector.
It's like finding the first needle in a haystack.
document.querySelectorAll returns a list of all the elements on the page that match the given selector.
This method is like gathering all the needles in the haystack.
The selector is passed as a string argument to the method.
Just like how you'd search for a specific word in a book, you pass the selector as a string.
HTML elements are essentially objects, and just capturing them makes little sense.
You'll want to do something with those objects, like parse a property from them.
You can't use regular array methods with the list returned by document.querySelectorAll.
It's a NodeList, not an array, so you'll need to use workarounds.
If you want to map all elements in the list to get a modified array, you'll need to invoke the method from the array prototype.
It's a good thing to keep in mind when working with document.querySelectorAll.
Sources
- CSS Selectors (sfu.ca)
- W3C Documentation (w3.org)
- W3C Documentation (w3.org)
- W3C Documentation (w3.org)
- CSS Selectors (htmlandcssguidebook.com)
- Squarespace CSS: How to find page ID and class selectors (bamn.digital)
- w3schools.com CSS Selector Reference (w3schools.com)
- CSS selector tester (w3schools.com)
- stellar browser support (caniuse.com)
Featured Images: pexels.com