Contents:

Course with employment: "Front-end Developer Profession"
Learn MoreChoosing the right selector plays a key role in ensuring code correctness and simplifying the layout process. Knowing the different types of selectors and how to use them significantly facilitates the work of both beginners and experienced front-end developers and layout designers looking to brush up on their skills. In this article, we will cover the main types of selectors and their application in development, which will help improve the quality and efficiency of your work.
Table of contents is an important element of any text, helping to structure information and make it more accessible. A properly formatted table of contents allows readers to quickly navigate the material and find the sections they need. To optimize text for SEO, it is necessary to consider keywords and phrases that are relevant to the topic. This will help improve the visibility of the content in search engines and attract the target audience. Clear and logical content not only improves user experience but also increases time spent on the page, which has a positive impact on website rankings.
- What are CSS selectors?
- Main types of selectors
- Relationship selectors
- Attribute selectors
- A little about pseudo-classes
- How to determine selector priority
What are selectors in CSS?
A selector is a pattern used to target elements or groups of elements on a web page to apply CSS styles. A selector is specified before a block of properties and allows developers to precisely customize the appearance and design of elements. Thanks to selectors, you can effectively manage styles by setting them for specific elements, classes, or IDs, which makes the web design process more flexible and convenient. Proper use of selectors in CSS improves the structure and readability of code, and also improves the performance of a web page.
In the example shown, the selector targets the hyperlink (a) tag. This allows the browser to disable underlining for all links on the page by setting the text-decoration property to none. This allows the links to be displayed without standard decoration, allowing the designer to create a unique style for the web page.
We will include CSS styles in this HTML file. This will improve the visual design of the page and make it more attractive to users. Using external styles not only simplifies the editing process but also ensures faster page loading. To include CSS, you must use the <link> tag, which points to the stylesheet. Make sure that the path to the file is specified correctly so that the styles are correctly applied to the elements of your HTML document. Proper use of CSS styles significantly improves user experience and optimizes the site for search engines.
Here's how it appears in the browser:


CSS selectors are effective at interacting with elements on a page, but they aren't the only method for selecting them. There are other methods, such as using JavaScript, jQuery, and various libraries that allow you to access DOM elements. The choice of method depends on the specific task and project requirements. Proper use of selectors and methods for accessing elements can significantly improve the performance and usability of web pages.
Incorporating CSS into HTML is an important step in web development. It allows you to style web pages, making them more attractive and user-friendly. There are several ways to do this, including inline styles, internal styles, and external style sheets. Each of these methods has its own advantages and can be used depending on the project requirements. For more detailed information on how to properly connect CSS to HTML, we recommend reading the corresponding article on our website.
Types of selectors
The structure of real projects is much more complex. Web pages of online stores, corporate sites and blogs contain many nested and similar elements that require a unique design. If we could access these elements only by their names, the web would remain just a repository of monotonous hypertext, even with the use of CSS. A unique approach to design and development allows you to create attractive and functional interfaces that improve user experience and increase visitor engagement.
There are about 20 effective methods for selecting the necessary elements. In this text, we will consider the main ones.
The * (asterisk) selector in CSS applies to all elements on the page, which makes it a universal styling tool. Using this selector, you can effectively reset margins and set box-sizing values for all block elements at once, simplifying the layout process and ensuring a consistent appearance. This is especially useful when creating responsive and cross-browser designs, when it's important that all elements display correctly and consistently.
A CSS selector allows you to apply styles to all elements with the same tag. For example, this can apply to elements like <p>, <h1>, <div>, and others. We already examined this selector when removing the underline from links. An ID selector is denoted by the # (pound) symbol and is applied to an element with a given id attribute. This id must be unique on the page, meaning that an element can only have one id.
Example CSS code: the text in the <p> block with the ID intro will be colored red, and the text with the ID article_content will be highlighted in bold sans-serif. This approach to styles allows you to create a clear and understandable structure on a web page, improving user experience and increasing the SEO optimization of your content. Using unique IDs and selectors helps organize styling better and simplifies code maintenance.
CSS selector by class is used To select elements assigned a class attribute with a specific value. One element can belong to multiple classes, which are then separated by spaces. This allows you to apply styles to a group of elements, simplifying the process of formatting and managing them. Using classes in CSS makes the code more structured and modular, which improves the performance and maintainability of web projects.
The paragraph belongs to the plain_text and article classes, which means that styles from both classes are applied to it. This ensures uniform text formatting and improves readability. Using such classes helps create a harmonious design, which facilitates better user perception of information. Thus, the correct use of classes in HTML content is an important aspect of web development and affects the overall efficiency and visual appeal of the page.
CSS selectors can be effectively grouped to apply styles to multiple elements, classes, or groups of elements. To do this, simply list them separated by commas. This approach allows you to reduce the amount of code and improve its readability, as well as speed up page loading by reducing the number of repeated rules. By grouping selectors, you can more effectively manage the styles of your web design.
Selecting Elements by Relationship and Position
There is a group of selectors that allow you to select elements based on their relationship to other elements, such as parent and child, as well as their position in the DOM (Document Object Model) structure. These selectors provide flexibility and precision when working with HTML content, allowing developers to effectively manage the styles and behavior of web page elements. Using selectors such as parent, child, and sibling improves the ability to style and manipulate elements, which is an important aspect of web development.
To target all child B elements inside an A element, regardless of their nesting level, use the selector A B. This construction allows you to select all B elements located inside the A element, which makes it an effective tool for working with the hierarchical structure of the document. Using this selector simplifies the process of styling and manipulating elements on the page, providing more precise control over the display of content.
In this example, we set a bottom margin of 20 pixels for all images inside the figure element. This helps create visual balance and improve the perception of the content on the page. Proper use of margins contributes to a neater design, which has a positive impact on user experience and SEO optimization.
To apply CSS styles to B elements that are direct descendants of the A element at the first nesting level, use the symbol «>». This symbol specifies that the style will be applied only to B elements located directly inside A, excluding deeper nested B elements. This approach allows for more precise control over styles and document structure, providing the necessary flexibility in web design.
In this example, we have set bottom margins of 40 pixels for the images placed inside the container with the .container class. This helps create a harmonious distance between elements and improves the visual perception of the content on the page. Setting margins is important for ensuring proper perception and ease of navigation for users.
The A ~ B selector in CSS is used to select all B elements that appear after the A element in the document. It is important to note that the B elements do not have to be nested within A, but simply follow it in the hierarchy. For example, using this selector, you can set the background color #f2f3f5 for all cards located after the block with the .about_us class. This allows you to effectively manage the styles of elements on the page, providing flexibility in design and simplifying the styling process.
The A + B selector in CSS is used to select only the first B element that immediately follows the A element. This selector allows you to create more precise styles for elements located in a specific document structure. For example, if you need to change the style of a heading that comes immediately after a paragraph, you can use the A + B selector to ensure that the styles are applied only to the desired element. This approach helps avoid unnecessary changes to other elements on the page and makes your CSS more organized and efficient. Using selectors in CSS, such as A + B, can improve the performance and manageability of your code, which is important for optimizing web pages.
In this example, the background color with the code #f2f3f5 will be applied exclusively to the card located immediately after the element with the class .about_us. This allows you to highlight the card and create a visual separation of content, improving user perception of information and contributing to better SEO optimization of the page.
CSS Attribute Selectors
Attribute selectors are a powerful tool in web development, allowing you to select elements on a page based on their attributes. You can use them to select elements by the attribute name, its value, or even part of that value. This makes attribute selectors indispensable for fine-tuning the styles and interactions with elements on a page. Let's consider the main capabilities they offer.
The attribute in question allows you to apply styles to specific elements on a web page. Using this attribute, you can customize the appearance and design of elements such as text, images, and buttons. This allows developers to create a unique design that matches the overall concept of the site. Correct use of the attribute improves the visual perception of the content and promotes better user interaction with the web resource. Optimizing styles with this attribute can also have a positive impact on SEO, as it improves the readability and structure of the page.
Text is based on the attribute name and value.
Finding elements with a specific attribute whose value begins with the specified value is an important task when working with HTML and JavaScript. This approach allows for efficient filtering of page elements based on the initial portion of their attributes. Using methods such as querySelectorAll or loop filtering, developers can quickly find the desired elements, simplifying DOM manipulation. Such operations are often used in tasks related to form processing, dynamic content display, and user interface improvement. Optimizing element searches by attributes also improves the performance of web applications and makes code more readable and maintainable.
The search is performed by the attribute name and its value, which exactly matches or begins with the specified value.
CSS styles can be applied to HTML elements whose value of a certain attribute ends with a specified value. This allows for more precise customization of the style of elements on a web page, improving visual perception and making it easier to work with content. To achieve this, an attribute selector in CSS is used. For example, if you want to select all elements whose class attribute ends with «value», you can use the following syntax: [class$=»value»]. Using this approach can significantly simplify the styling process and make the code easier to edit and maintain.
A selector by attribute name and the value contained in the value attribute allows you to effectively find elements on a web page. This method is used in CSS and JavaScript to specifically select elements with specific attributes. For example, you can select all elements with a certain attribute where the value contains the specified text. This is especially useful for filtering and styling elements based on their attributes, which greatly simplifies working with dynamic content and improves the user experience. Use attribute selectors to increase development efficiency and improve your site's SEO.
This pattern finds elements with an attr attribute whose value consists of multiple words separated by spaces and includes a specific word—value.
Pseudo-Classes and Pseudo-Elements
A pseudo-class in CSS allows you to select elements that are in a specific state or position in the DOM hierarchy. It provides the ability to style elements based on their state, such as on hover or on activation. Pseudo-classes improve the user experience, allowing you to create more dynamic and responsive interfaces. Using pseudo-classes helps developers effectively manage styles and improve user experience on websites.
Here are some examples of states that can occur in different situations. These states can include emotional, physical, or psychological aspects that influence our behavior and perception of the world around us. For example, anxiety can arise as a result of uncertainty or stress, while joy often accompanies positive events. These states can be temporary or chronic, and understanding them is important for improving quality of life. Being aware of your emotions and states helps you better manage them and find ways to optimize them.
- the mouse is hovering over a button;
- the user has or has not clicked on a link;
- the cursor is positioned over an input field.
CSS can be used to increase the size of links on hover. This is achieved using the :hover pseudo-class, which allows you to change the style of an element upon user interaction. To do this, you can set new properties, such as font size or letter spacing, which makes links more visible and improves the user experience. For example, by increasing the font size or adding a color-changing effect, you can draw more attention to the links on your site.

In the following example, we set The bottom margin for the last paragraph within the container, labeled "Paragraph 3." This creates a visual separation between elements and improves the readability of the text on the page. Proper use of indents helps format content better and makes it more readable for users.
CSS is a stylesheet language used to describe the visual presentation of HTML documents. With CSS, developers can control colors, fonts, sizes, margins, and other aspects of web page design. This allows for the creation of responsive and attractive interfaces that meet modern web design standards.
CSS allows for the separation of content and design, significantly simplifying the process of website development and maintenance. This allows developers to easily make style changes without affecting the HTML code itself. Using external style sheets improves performance and speeds up page loading.
One of the key features of CSS is the ability to apply styles to different elements depending on the state, device type, or screen size. This makes CSS an indispensable tool for creating responsive designs that ensure a smooth website experience on any device.
CSS also provides powerful tools for working with animation and element transformations, allowing you to create dynamic and interactive user interfaces. Using modern techniques such as Flexbox and Grid Layout greatly simplifies the creation of complex layouts.
In conclusion, CSS is a fundamental tool for web developers, providing flexibility and control over the visual presentation of websites. Understanding and skillfully using CSS allows you to create high-quality web applications that meet user requirements and modern design trends.
When using DevTools, you can notice that a margin is displayed under the third p element, highlighted in beige. This margin is responsible for the element's indentation and can affect the overall layout of the page. Understanding its presence and correct use of margin allows you to optimize the layout and improve the visual perception of the content on the site.

Main CSS pseudo-classes allow you to control the state of elements on a web page and improve user experience. Pseudo-classes help highlight certain states, such as hovering, selecting an element, or activating it. Some of the most common pseudo-classes include :hover, :active, and :focus. Using these pseudo-classes makes web designs more interactive and engaging. Pseudo-classes not only enhance the user experience but also contribute to improved search engine optimization by allowing you to better organize elements on a page. Understanding and correctly using pseudo-classes is an important aspect of web development that helps create a high-quality and user-friendly website. For more information about pseudo-classes, visit the Mozilla website. There you will find detailed descriptions and examples of various pseudo-classes, which will help you better understand their functionality and application in web development.
CSS Selector Weight, or Specificity
A web page can assign multiple styles to the same element. When different values are specified for a given property in different parts of the CSS file, the browser is forced to choose one of them. This process is called the cascading of styles and depends on the order in which styles are defined, the specificity of selectors, and priorities. Therefore, it is important to organize your CSS properly to avoid conflicts and ensure that elements on the page display correctly.
When specifying styles in CSS, the rule with the highest specificity is applied, although this does not always mean that the last rule will take precedence. Some selectors have a higher specificity, which allows them to override styles specified by other selectors. This is important to consider when developing web pages to avoid conflicts and ensure that content displays correctly. Selector specificity plays a key role in style management and allows developers to more precisely control the appearance of page elements.
Specificity is a key parameter that allows browsers to determine which CSS styles to apply to elements on a web page. It is represented as four numbers, written in the format 0.0.0.0, where each number corresponds to a specific style priority level established by specific rules. Understanding specificity is important for web developers, as it helps avoid style conflicts and ensure that elements on a website display correctly. It is important to keep in mind that higher specificity values take precedence over lower ones, allowing you to fine-tune the visual appearance of your web pages.
- Styles specified in the style attribute have the highest priority (1.0.0.0).
- Second place goes to selectors by ID (0.1.0.0).
- Then come three equal groups: selectors by class, attribute, and pseudo-classes (0.0.1.0).
- Fourth place goes to selectors by tag and pseudo-elements (0.0.0.1).
- The combinators ~, >, +, and the universal selector * do not add weight.
- The weight of the pseudo-classes :is(), :has(), and :not() is equal to the weight of the most specific selector inside the brackets.
To determine the “heaviest” selector, the browser First, the browser weights each selector and then compares them bitwise. This process is important for optimizing page rendering and improving performance. Proper use of selectors helps reduce page load time and improve the efficiency of CSS processing. The lighter the selector, the faster the browser can apply styles to elements.
Let's consider how the browser functions when processing CSS selectors. Let's say there is an element on a web page to which two different CSS selectors are applied. It is important to understand how the browser determines which selector will take precedence and how it affects the style of this element.
The browser analyzes selectors in order of their specificity. The higher the specificity of a selector, the more likely its styles will be applied to an element. If two selectors have the same specificity, the last one specified in the code is applied.
Therefore, when developing web pages, it is important to consider the order and specificity of CSS selectors in order to avoid conflicts and style elements correctly. Using selectors correctly can improve the visual appeal and functionality of your website, which in turn will improve user experience and SEO.
Let's calculate their weight.
- The .container class adds 1 to the third position, while div and ul each add 1 to the fourth position. Result: 0.0.1.2.
- The our_team identifier adds 1 to the second position, the div tag adds 1 to the fourth position, and the .developers class adds 1 to the third position. We get 0.1.1.1.
The browser will apply the styles specified for the #our_team div.developers selector, since this selector refers to a specific identifier. This allows you to precisely customize the display of elements within the «our team» block. Using IDs in CSS helps manage styles more efficiently, providing targeted formatting and improving the visual hierarchy on the page.
If the element in question has a style attribute, no additional evaluation is required. This is because the style attribute has the highest precedence in Cascading Style Sheets (CSS).
There's a secret trick that allows you to override any CSS rules. If you add the !important keyword to a property, the style will take precedence and will be applied to the element without exception. This method is useful when you need to strictly enforce certain styles despite other rules that may conflict. However, it should be used with caution, as frequent use of !important can lead to difficulties in style management and maintenance.
To change the text style in links, you will need to use the !important property in a more specific selector. This will override any previously set styles. For example, you could use the following selector:
Experienced developers advise avoiding the !important property in CSS, as it can complicate code maintenance and disrupt the style cascade. When debugging styles, it is important to remember the presence of this property, as it can be located in the styles.css file and affect the display of elements. It is recommended to adhere to a more structured approach to writing CSS to avoid conflicts and improve code readability.
Summary
CSS offers a variety of selectors that allow you to effectively style web pages. Once you master them, you will be able to write code that is not only understandable but also easy to maintain. For practice and consolidation of knowledge, we recommend using the exciting browser game CSS Dinner, which will help you improve your skills in working with CSS selectors.
Reworked text:
Dive into the world of reading opens new horizons and enriches our inner world. Reading not only develops your imagination but also improves concentration, expands your vocabulary, and fosters critical thinking. By reading, we can better understand the world around us and delve deeper into various topics and ideas. It's important to choose interesting and useful books that will inspire and motivate you. Consider classics, modern bestsellers, and specialized publications that can help you develop professionally. Reading is not only a form of entertainment but also an important tool for personal growth and self-improvement.
Read also:
- A quick way to connect CSS to HTML
- Test: Which IT specialist from the movies are you like?
- What is CSS - and how to layout it

