Javascript Gets The Child Element By Class

| | | |

The CSS selector: nth child () applies a style to elements at a specific position in a group. Often the:. Nth child selector () is used to style particular items in the list, such as every second or third item

When designing a website, you may want to apply styling only to a selected set of items in a container. . For example, you may want - to be the style that every other item in a list or every third row in a table

is the CSS pseudo-class is available in: nth child. The nth child pseudo-class compares elements based on their position in the list of elements on an HTML page

This tutorial will discuss, with reference to examples, the basics of pseudo-classes and how may. use the pseudo-class: nth child. By the end of this tutorial, you will be well versed in using:. Nth child pseudo-class to select elements on a web page

CSS pseudo-classes

CSS, selectors are used to select specific elements on a web page to apply a style to. For example, a selector can change the text color of all

elements on a web page to green.

You can choose to apply a style only to an element that exists in a specific state. Pseudo-classes allow you to apply styles only when an element is in a specific state

CSS:. Nth child pseudo-class

The pseudo-class: child nth- applies styles to elements of a group. You can apply styles to a specific element based on its location or multiple elements. A common: n-th child rule is to color each item in a strange position or even in the list

Here is the syntax for the pseudo-class:. Nth child:

pause Let this example down:

  • li refers to the element to which a style should be applied.
  • nth child is the selector used to match an element.
  • 2 is the formula used to match an element using nth child

In this case, we’ve applied our style to every second ento

  • element in a list

    :. nth child Accepted values ‚Äã‚Äã

    The alias: class nth child accepts two types of values. First, the pseudo-class accepts keyword values that are assigned from predefined formulas. These keywords are:

    • odd. Select the elements whose position in a list is an odd number
    • same: Select the elements whose position in a list is an even number <. / li>

    Second , the: nth child pseudo-class accepts a custom formula that specifies the elements that the pseudo-class should select in a web page. Here are some example formulas:.

    • : nth-child (6):. select the sixth element in a list
    • : nth-child (n + 2): select the second element and all subsequent elements
    • : nth-child (4n): selects all elements that are multiples of 4.

    n is the same as the position in which an element appears in a list

    Now that we know the basics of the pseudo-class: .. nth child, we can explore some examples of the pseudo-class in the action

    CSS examples: nth child

    Let’s take a look at two examples of the pseudo-class:. nth child

    A common use of the do-class PSEU:. -class nth-child is to select rows in a table

    Suppose you are designing a website for a local cooking club. The club asked us to set up a table for their website with the recipe for their special spicy hot chocolate. The club wants every second row of the table to be highlighted in hot pink, which is the main color of the club.

    We could create this table using the following code:.

    Click Image of the button

    We have defined a table. two columns and five rows (one of which is a header row ). The first column contains the list of ingredients used to make hot chocolate with spices. Our second column stores the amounts of each ingredient used

  • for en To learn more about how tables are created in HTML, read our to HTML tables .

    in our CSS file, we used the pseudo-class: nth child to style each same row in our table

    tr selector allows you to select all the rows of the table (indicated by the HTML tag) and the pseudo-class: nth child (same) selects each row of the table with the same identification number. Each line of the same table, the table of background colors - the plan line is set to bright pink

    :. List n -th child

    Another common use of the pseudo-class: nth -child is - to highlighted items in a list.

    Suppose we asked to make a list of the names of each member of the cooking club . The text for the first three names on the list should appear in light blue, because they hold titles in the club

    We could create. List this using the following code:

    Click Image of the button

    We have used a