CSS
CSS Selector-Child, Sibling, Attribute, :first-child, nth-child(2n)
- Child Selector (>):
- Adjacent Sibling Selector (+):
- Attribute Selector:
- General Sibling Selector (~):
- :first-child:
- :nth-child(2n):
In CSS (Cascading Style Sheets), selectors are used to target specific HTML elements for styling. Here are some advanced selectors along with examples:
¶Child Selector (>):
- Selects direct children of a parent element.
Copy code
/* Selects all direct <p> children of the element with class "container" */
.container > p {
color: blue;
}
¶Adjacent Sibling Selector (+):
- Selects an element that is directly adjacent to another element.
/* Selects the <span> directly adjacent to an <h2> */
h2 + span {
font-weight: bold;
}
¶Attribute Selector:
- Selects elements based on their attributes.
/* Selects all elements with a "data-category" attribute */
[data-category] {
background-color: yellow;
}
¶General Sibling Selector (~):
- Selects all sibling elements that follow an element.
/* Selects all <p> elements that are siblings following an <h2> */
h2 ~ p {
margin-top: 10px;
}
¶:first-child:
- Selects the first child element of its parent.
/* Selects the first <li> in a list */
li:first-child {
font-weight: bold;
}
¶:nth-child(2n):
- Selects every even child element.
/* Selects every even <tr> in a table */
tr:nth-child(2n) {
background-color: lightgray;
}
These selectors provide a powerful way to target and style specific elements in your HTML documents. Remember that browser compatibility may vary for some of these selectors, so it’s a good idea to check compatibility if you have to support older browsers.
Pseudo Class - hover, focus, active and Pseudo Element - before, after
Overflow - scroll, auto, visible, hidden, overflow-x, overflow-y
All Tutorials in this playlist
Popular Tutorials
Categories
-
Artificial Intelligence (AI)
11
-
Bash Scripting
1
-
Bootstrap CSS
0
-
C Programming
14
-
C#
0
-
ChatGPT
1
-
Code Editor
2
-
Computer Engineering
3
-
CSS
28
-
Data Structure and Algorithm
18
-
Design Pattern in PHP
2
-
Design Patterns - Clean Code
1
-
E-Book
1
-
Git Commands
1
-
HTML
19
-
Interview Prepration
2
-
Java Programming
0
-
JavaScript
12
-
Laravel PHP Framework
37
-
Mysql
1
-
Node JS
1
-
Online Business
0
-
PHP
28
-
Programming
8
-
Python
12
-
React Js
19
-
React Native
1
-
Redux
2
-
Rust Programming
15
-
Tailwind CSS
1
-
Typescript
10
-
Uncategorized
0
-
Vue JS
1
-
Windows Operating system
1
-
Woocommerce
1
-
WordPress Development
2
Tags
- Artificial Intelligence (AI)
- Bash Scripting
- Business
- C
- C Programming
- C-sharp programming
- C++
- Code Editor
- Computer Engineering
- CSS
- Data Structure and Algorithm
- Database
- Design pattern
- Express JS
- git
- Git Commands
- github
- HTML
- Java
- JavaScript
- Laravel
- Mathematics
- MongoDB
- Mysql
- Node JS
- PHP
- Programming
- Python
- React Js
- Redux
- Rust Programming Language
- TypeScript
- Vue JS
- Windows terminal
- Woocommerce
- WordPress
- WordPress Plugin Development