CSS Class SelectorHTML elements are chosen or chosen using CSS selectors so that CSS styles may be applied to them. The class names of HTML elements are supplied as attributes within an HTML tag, and a class selector in CSS is used to choose HTML components based on these class names. Any string value followed by a dot (.) like .class may be used as a classname in HTML and is created by utilizing the class attribute within an HTML element. Using a class selector would pick all items with that specific class, which is vital to remember as several components may have the same class property. What Functions CSS Class Selector (".class") Performs?
Syntax The class attribute of an HTML element may be used to declare a class using the following syntax: The following syntax in CSS can be used to choose the previously mentioned class: Establishing a CSS ClassUsing the class attribute within an HTML element tag may construct a CSS class in HTML. Any string value with capital or lowercase characters, numbers, hyphens, and underscores can be used as a class name. A space cannot be contained within a CSS class name. In addition to being used within the class selector, a classname may also be utilized by JS to modify HTML elements. The example that follows demonstrates how to add a class name to any HTML element: An <div> element with the class name "division" and <a> tag with the class name "anchor" make up the previous example. Can CSS Classes Contain an Element?In HTML, a single element may contain many class names, each separated by a space. This is advantageous since using several class names may give items more specificity. For instance: An <div> element with the class names "another" and "division" and a <a> tag with the class names "another" and "anchor" make up the sample above. Using the CSS Class SelectorIn CSS, a class selector can be used together with a class name directly, as in the example below: Two div elements and two p elements are described in the HTML above. A div element's class is set to "Main". Within the main, the two div elements and one of the p elements all have the class set to the child. By utilizing the following CSS, we may alter the background and border colors of the components. For visibility, we have also added margins and padding. Output As you can see, each chosen item has been styled using the techniques mentioned above. The .child selector chose both the div and the p elements, and the same styles were applied to them. Pseudo-ClassesIn CSS, pseudo-classes are used to specify certain states for HTML components. For various states of HTML components, pseudo-classes offer additional functionality. Utilizing the appropriate pseudo classes, it is possible to provide states such as visited links, active links, mouse over items, first child, final child, nth-child, etc. The following syntax can be used to combine pseudo-classes and CSS selectors: Although there are many CSS pseudo-classes, we'll talk about some of the more popular ones in this article. The following list is the most popular CSS classes:
:hover Pseudo-classWhen our mouse cursor hovers over an element, this pseudo-class adds a particular effect to it. The illustration below shows how the text color changes from black to pink and yellow as our mouse reaches the box region. Example: The hover CSS pseudo-class is displayed in this example. Output :active Pseudo-classThis pseudo-class is used to identify an element that becomes active when a user clicks on it. The following sample shows how the text color briefly changes when you click on it. Example: This example displays the CSS active pseudo-class. Output :focus Pseudo-classThe element that the user is now focusing on is chosen using this pseudo-class. The script is invoked when a user clicks on one of the user input items used in forms. The background color of the input field that is now in focus changes in the following example. Example: The CSS focus pseudo-class is displayed in this example. Output :visited pseudo-classThe URLs already viewed by the user are chosen using the visited pseudo-class. When a link is clicked in the sample below, its color changes. Example: This example in CSS displays the visited pseudo-class. Output The element based on language characteristics is targeted for a particular value using an :lang Selector. This selection uses the language code to function. Syntax Language Codes:
Example Output :first-child pseudo-classIn CSS, the first-child pseudo-class is the first element in a set of related components. To style the first-child element of its parent, use the :first-child selector. Syntax Example Output
Next TopicCSS Display Block
|