Basic CSS
CSS Selectors
Selectors are one of the most important parts of CSS as they define which HTML elements the styles will be applied to. Knowing how to use different types of selectors will allow you to write CSS rules that affect the right elements of your web page.
Basic Selectors
Basic selectors refer directly to HTML elements by their name, class, or ID.
Type Selector
This selector applies to all HTML elements of a specific type. For example, if we want all the paragraphs on our page to have green text color, we can use a type selector.
css
Class Selector
A class selector applies styles to any element that has a specific class. Classes are very useful when we want to apply the same style to multiple elements but not all elements of a certain type.
css
ID Selector
An ID selector is more specific than a class selector and is used to target a single element on the page. Each ID must be unique within an HTML document.
css
Combining Selectors
We can combine selectors to target elements more precisely. For example, we can apply a style only to elements with a specific class that are also of a certain type.
css
Pseudo-Classes
Pseudo-classes allow you to select elements in a specific state. One of the most common pseudo-classes is :hover
, which applies when the user hovers over an element.
css
Other Useful Pseudo-Classes
-
:focus
: Applies when an element, such as a form field, is active or focused.css"This CSS rule applies a blue border when the input field is in focus, i.e., when the user is typing in it." -
:nth-child(n)
: Selects the nth child of its parent.css"We use nth-child to select the second list item, li, and apply a red color to it."
Pseudo-Elements
Pseudo-elements allow you to style specific parts of an element. For example, you can add content before or after an element using ::before
and ::after
.
css
Specificity in CSS
When multiple selectors target the same element, CSS uses specificity to determine which rule to apply. Specificity depends on the type of selector used. Here is a list of common selectors, from highest to lowest specificity:
- ID selectors (#id)
- Class selectors (.class)
- Type selectors (HTML tag)
For example, if two CSS rules target the same element, such as a paragraph, but one uses a class selector and the other a type selector, the rule with the class selector will take precedence.
css
Conclusion
In this chapter, we covered the different types of selectors in CSS and how they can be used to effectively apply styles to HTML elements. Selectors are a powerful tool for controlling which elements receive styles, and understanding their use is crucial for writing efficient CSS. In the next chapter, we will learn about the box model in CSS, which is key to understanding how elements are laid out on the page.
- Introduction to CSS
- CSS Selectors
- The Box Model in CSS
- Colors and Backgrounds in CSS
- Typography in CSS
- Design Techniques with CSS
- Responsive Design Fundamentals in CSS
- Responsive Navigation in CSS
- Responsive Forms in CSS
- Combinando CSS con HTML para un diseño completo
- CSS Debugging and Optimization
- Working with CSS Libraries and Frameworks
- Customizing CSS Frameworks
- Structuring Large CSS Projects
- Best Practices for CSS Performance
- Keeping CSS Code Clean and Well-Documented
- Testing and Debugging CSS
- Ensuring Accessibility with CSS
- Use animations and transitions in an accessible manner
- CSS Optimization for Large Websites
- Keeping Clean and Scalable CSS Code