Javatpoint Logo
Javatpoint Logo

CSS Class Selector

HTML 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?

  1. In CSS, class attributes are used to style items using a class selector.
  2. In CSS, a class selector picks out HTML elements that belong to the specified class and applies the appropriate styles to them.
  3. Class selectors add particular styles to the element based on the class name supplied for an element.
  4. In CSS, a class selector may simultaneously pick several items with the same class name and apply the same style.

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 Class

Using 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 Selector

In 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

CSS Class Selector

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-Classes

In 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:

  1. :active
  2. :hover
  3. :link
  4. :visited
  5. :lang
  6. :focus
  7. :first-child

:hover Pseudo-class

When 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

CSS Class Selector
CSS Class Selector

:active Pseudo-class

This 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

CSS Class Selector

:focus Pseudo-class

The 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

CSS Class Selector

:visited pseudo-class

The 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

CSS Class Selector

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:

  • en: used for English.
  • hi: used for Hindi.
  • fr: used for French.
  • de: used for German
  • it: is used for Italian
  • ca: used for Canadian

Example

Output

CSS Class Selector

:first-child pseudo-class

In 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

CSS Class Selector
Next TopicCSS Display Block





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA