Learn How to Use Advanced CSS Selectors Like a Pro

CSS selectors are the backbone of web styling, allowing developers to target HTML elements precisely and efficiently. While basic selectors like classes and IDs are commonly used, advanced CSS selectors offer powerful ways to style elements based on their attributes, position, and state. This guide delves into these advanced selectors, complete with examples to enhance your web design skills.
Table of Contents
Attribute Selectors
Pseudo-Classes
Dynamic Pseudo-Classes
Structural Pseudo-Classes
3. Pseudo-Elements
4. Combinators
Descendant Combinator
Child Combinator
Adjacent Sibling Combinator
General Sibling Combinator
5. Negation Pseudo-Class :not()
6. Universal Selector
7. Conclusion
Attribute Selectors
Attribute selectors allow you to select elements based on their attributes and attribute values.
Syntax and Examples
- [attr]: Selects elements with the specified attribute.
/* Selects all input elements with a 'required' attribute */
input[required] {
border: 2px solid red;
}
2. [attr=”value”]: Selects elements with an exact attribute value.
/* Selects all anchor tags linking to example.com */
a[href="https://www.example.com"] {
color: green;
}
3. [attr~=”value”]: Selects elements whose attribute value contains a specified word.
/* Selects elements with a class attribute containing 'card' */
div[class~="card"] {
box-shadow: 0 4px 8px rgba(0,0,0,0.2);
}
4. [attr^=”value”]: Selects elements whose attribute value starts with a specified value.
/* Selects all links that start with 'https' */
a[href^="https"] {
text-decoration: underline;
}
5. [attr$=”value”]: Selects elements whose attribute value ends with a specified value.
/* Selects image files ending with '.png' */
img[src$=".png"] {
border: 1px solid #ccc;
}
6. [attr=”value”]*: Selects elements whose attribute value contains a specified substring.
/* Selects all elements with a data attribute containing 'user' */
[data-info*="user"] {
background-color: #f9f9f9;
}
Pseudo-Classes
Pseudo-classes target elements in a specific state, such as when a user hovers over them or when they are the first child of their parent.
Dynamic Pseudo-Classes
:hover
: Styles an element when a user hovers over it.
/* Changes button color on hover */
button:hover {
background-color: #555;
color: white;
}
2. :active
: Styles an element when it is activated (e.g., clicked).
/* Shrinks button slightly when clicked */
button:active {
transform: scale(0.98);
}
3.:focus
: Styles an element when it has focus.
/* Highlights input fields when focused */
input:focus {
outline: 2px solid #0066cc;
}
Structural Pseudo-Classes
:first-child
: Selects the first child of its parent.
/* Styles the first list item differently */
li:first-child {
font-weight: bold;
}
2.:last-child
: Selects the last child of its parent.
/* Styles the last paragraph with a margin */
p:last-child {
margin-bottom: 0;
}
3. :nth-child(n)
: Selects the nth child of its parent.
/* Styles every even table row */
tr:nth-child(even) {
background-color: #f2f2f2;
}
4. :nth-of-type(n)
: Selects the nth child of its type among its siblings.
/* Styles the second paragraph in a container */
p:nth-of-type(2) {
font-size: 1.2em;
}
5. :only-child
: Selects an element that is the only child of its parent.
/* Highlights images that are the only child */
img:only-child {
border: 5px solid #ccc;
}
Pseudo-Elements
Pseudo-elements allow you to style specific parts of an element’s content.
::before
and::after
: Inserts content before or after an element's content.
/* Adds a quotation mark before blockquotes */
blockquote::before {
content: "“";
font-size: 2em;
vertical-align: top;
color: #ccc;
}
2. ::first-letter
: Styles the first letter of a text block.
/* Makes the first letter of a paragraph larger */
p::first-letter {
font-size: 2em;
float: left;
margin-right: 5px;
}
3. ::selection
: Styles the portion of an element that is selected by a user.
/* Changes the background color when text is selected */
::selection {
background: #ffb7b7;
}
Combinators
Combinators define the relationship between selectors.
Descendant Combinator
- Selector A Selector B: Selects all elements B that are descendants of A.
/* Styles all links inside a footer */
footer a {
color: #fff;
}
Child Combinator
- Selector A > Selector B: Selects all elements B that are direct children of A.
/* Styles direct child list items */
ul > li {
margin-bottom: 10px;
}
Adjacent Sibling Combinator
- Selector A + Selector B: Selects the element B that immediately follows element A.
/* Styles the paragraph immediately following a heading */
h2 + p {
text-indent: 50px;
}
General Sibling Combinator
- Selector A ~ Selector B: Selects all elements B that are siblings of A.
/* Styles all paragraphs that are siblings of a heading */
h2 ~ p {
color: #666;
}
Negation Pseudo-Class :not()
The :not()
pseudo-class selects elements that do not match the selector inside the parentheses.
/* Styles all buttons except those with the class 'cancel' */
button:not(.cancel) {
background-color: #28a745;
color: white;
}
Universal Selector
The universal selector *
matches any element.
/* Resets margin and padding for all elements */
* {
margin: 0;
padding: 0;
}
Conclusion
Mastering advanced CSS selectors enhances your ability to create sophisticated and efficient stylesheets. By leveraging these selectors, you can target elements with precision, reduce the need for excessive classes or IDs, and maintain cleaner HTML structures. Experiment with these selectors in your projects to see the difference they can make in your web development workflow.
📌 Stay Updated
Follow me for more design tips and tools! ✨
🐙 GitHub: Follow me for more web development resources.
🔗 LinkedIn: Connect with me for tips and tricks in coding and productivity.
✍️ Medium: Follow me for in-depth articles on web development and productivity.
Subscribe to my newsletter
Read articles from Chandan kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Chandan kumar
Chandan kumar
Hello, World! 👋 I'm Chandan Kumar, a passionate and results-driven MEAN stack web developer with a strong background in designing and implementing web applications that deliver exceptional user experiences. I'm dedicated to staying at the forefront of web development trends and technologies to create cutting-edge solutions for my clients. My Expertise: MEAN Stack Development: I specialize in building robust web applications using MongoDB, Express.js, Angular, and Node.js. This full-stack approach allows me to create seamless, end-to-end solutions that meet and exceed client expectations. Front-end Excellence: I have a keen eye for UI/UX design and a deep understanding of front-end technologies such as HTML5, CSS3, and JavaScript. I leverage these skills to craft engaging, responsive, and user-friendly interfaces. Back-end Proficiency: With extensive experience in server-side scripting, API development, and database management, I ensure that the applications I build are not only visually appealing but also highly performant and secure. Problem Solver: I thrive on challenges and enjoy solving complex problems. Whether it's optimizing code for efficiency or troubleshooting issues, I'm committed to finding innovative solutions. My Mission: My mission is to create digital experiences that make a meaningful impact. I'm passionate about collaborating with teams and clients to turn ideas into reality. I believe that technology can empower businesses and individuals alike, and I'm excited to be a part of that journey. Let's Connect: I'm always open to networking and exploring opportunities for collaboration. Whether you're interested in discussing a potential project, sharing insights, or simply connecting with fellow professionals in the tech industry, feel free to reach out. Let's connect and start a conversation! Contact Information: 📩 Email: chandanku845415@gmail.com 📱 LinkedIn: developerchandan 🌐 Portfolio: developerchandan.github.io ✍️ Medium: developerchandan Thank you for visiting my profile, and I look forward to connecting with you!