CSS Selectors
Finding an element using css selector
Basically css
is the combination of attributes that will identify an element or group of elements.
Finding by <tag-name>
<tag-name>
This will match all the elements with that tag-name
For example, css selector a matches all the link elements inside the page.
Id selector : #id-value
#id-value
If we want to find an element using it's id, we can write css selector like
Here we can find the section
with css selector using id attributes as
Class selector : .class-value
.class-value
If we want to find an element using it's id, we can write css selector like
if we want to find element which has multiple class attributes, then we can combine the classes together with .
Here we can find the ul
with css selector using class attributes as
Selector using any attribute: tag-name[attribute-name = 'attribute-value']
tag-name[attribute-name = 'attribute-value']
If the element that we want to locate is not having either of class
or id
attributes and having other attributes like type, data-target, href
etc., then we can make use of them to locate the elements.
For example
This is most common and useful syntax, we can locate any element in the page using this syntax by taking any of it's attributes
Immediate child selector: >
>
The >
selects the immediate child of the element before the symbol.
For example:
In the above section, if I want to find the list element li
which has none of the attributes inside ul
unordered list then we can write css selector like
Any child selector: blank-space
blank-space
The blank-space
selects the immediate child of the element before the symbol.
For example:
In the above section, if I want to find the list element li
which has none of the attributes inside section
then we can write css selector like
nth-child selector: :nth-child()
:nth-child()
When a written css is locating more than one element, and if want to select an element based on it's position the we can use
In the above section, if I want to find the first list element li
which has none of the attributes inside ul
unordered list then we can write css selector like
More about nth-child()
can be found here
Last updated