Here are a few examples of CSS selectors and how they are used to style HTML elements:
- Element selector:-
The element selector is used to select all elements of a particular type and apply a style to them. For example, the following CSS code selects all <h1>
headings on a page and changes their font size to 24px:
h1 {
font-size: 24px;
}
- Class selector:-
The class selector is used to select all elements with a particular class and apply a style to them. For example, the following CSS code selects all elements with the class "my-class" and changes their background color to red:
.my-class {
background-color: red;
}
In the HTML code, this could be applied to a paragraph like this:
<p class="my-class">This paragraph will have a red background color.</p>
3). ID selector:-
The ID selector is used to select a single element with a unique ID and apply a style to it.Let me tell you "by the word unique id means there should not be the repetition of id in any other element or tag for example you should not write
<p id="first"> some content </p>
<!-- Then --->
<div id = "first"> some content </p> "
" For example, the following CSS code selects the element with the ID "my-id" and changes its font size to 20px:
#my-id {
font-size: 20px;
}
In the HTML code, this could be applied to a heading like this:
<h2 id="my-id">This heading will have a font size of 20px.</h2>
From here Even i was confused what are difference between different combination selectors so i have included some images to help you better understand what is the differnce between all
Now Combination Selectors
- Descendant selector:-
The descendant selector is used to select elements that are nested inside other elements. For example, the following CSS code selects all <p>
elements that are nested inside a <div>
element and changes their font color to blue:
div p {
color: blue;
}
In the HTML code, this could be applied to a section like this:
<div>
<p>This paragraph will have blue font color.</p>
<p>So will this one.</p>
</div>
These are just a few examples of CSS selectors and how they can be used to style HTML elements. By using CSS selectors, developers have precise control over the visual presentation of a web page, making it more engaging and appealing to users.
5). Child Selector :- The child selector is a CSS selector that is used to select direct children of an element. It allows you to apply styles to only the immediate children of a particular element and not to any deeper descendants.
The child selector is represented by the ">" symbol and is placed between two selectors. The first selector represents the parent element, while the second selector represents the child element. For example, the following code selects all the direct child elements of a div:
div > p {
color: blue;
}
In this example, the child selector is "> p", which selects all the <p>
elements that are direct children of a <div>
element. The color property is then applied to these selected elements.
Here's an example of how this child selector works in HTML:
<div>
<p>This paragraph will be blue.</p>
<ul>
<li>This list item will not be blue.</li>
<li>This list item will not be blue either.</li>
</ul>
<p>This paragraph will also be blue.</p>
</div>
In this example, only the first and last <p>
elements will be styled with the color blue, since they are direct children of the <div>
element.
Using the child selector can be useful when you want to target only the immediate children of a particular element and not any deeper descendants. It can also be used in combination with other CSS selectors to create more complex styles.
- SIbling Selector:- The sibling selector is a CSS selector that is used to select elements that share the same parent and are located immediately after a particular element in the HTML document. It allows you to apply styles to elements that come after a specific element but not to any elements that come before it or to other elements that are not siblings.
The sibling selector is represented by the "+" symbol and is placed between two selectors. The first selector represents the element that comes before the sibling, while the second selector represents the sibling element. For example, the following code selects all the sibling <p>
elements that come immediately after a <h2>
element:
h2 + p {
color: blue;
}
In this example, the sibling selector is "+ p", which selects all the <p>
elements that come immediately after a <h2>
element. The color property is then applied to these selected elements.
Here's an example of how the sibling selector works in HTML:
<div>
<h2>This heading will not be blue.</h2>
<p>This paragraph will not be blue.</p>
<h2>This heading will not be blue either.</h2>
<p>This paragraph will be blue.</p>
<p>This paragraph will also be blue.</p>
<h2>This heading will not be blue.</h2>
<p>This paragraph will not be blue.</p>
</div>
In this example, only the second and third <p>
elements will be styled with the color blue, since they are siblings that come immediately after a <h2>
element.
Using the sibling selector can be useful when you want to target specific elements that come after a particular element but not other elements that come before it or that are not siblings. It can also be used in combination with other CSS selectors to create more complex styles.