CSS selectors are used to target HTML elements on your pages, allowing you to apply styles based on their ID, class, type attributes, and more. There are mainly 5 types of selectors.
- Basic CSS Selectors: These are used to target elements by tag, .class, or #id for fundamental styling needs.
- Combinators: Ideal for styling elements based on their DOM relationships (e.g., parent-child or sibling relationships).
- Group Selectors: Use to apply the same styles to multiple, unrelated elements simultaneously.
- Attribute Selectors: Perfect for styling elements based on specific attributes or values, such as form inputs or links with certain prefixes or states.
- Pseudo-Classes: Best for styling elements dynamically or interactively, like :hover for user interaction or :nth-child() for structural styling.
Types of CSS Selectors
Basic Selectors
Basic selectors in CSS are simple tools used to target specific HTML elements for styling. These include selecting by element name (e.g., h1), class (.class Name), ID (#idName), or universally (* for all elements).
1. Universal Selector (*): Selects all elements on the page and applies the same style universally. For example, setting the font color for every element.
HTML
<!--Driver Code Starts{-->
<html>
<head>
<style>
<!--Driver Code Ends }-->
* {
color: red;
}
<!--Driver Code Starts{-->
</style>
</head>
<body>
<h1>Header Text</h1>
<p>Paragraph Text</p>
</body>
</html>
<!--Driver Code Ends }-->
2. Element Selector: Targets all elements of a specific type, such as paragraphs or headers. For example, setting a common font size for all paragraphs
HTML
<!--Driver Code Starts{-->
<html>
<head>
<!--Driver Code Ends }-->
<style>
p {
font-size: 16px;
}
</style>
<!--Driver Code Starts{-->
</head>
<body>
<p>This paragraph text will be styled with font size 16px.</p>
</body>
</html>
<!--Driver Code Ends }-->
3. Class Selector (.): Applies styles to elements with a specific class attribute. For instance, making all buttons have a blue background.
HTML
<!--Driver Code Starts{-->
<html>
<head>
<!--Driver Code Ends }-->
<style>
.button {
background-color: blue;
color: white;
}
</style>
<!--Driver Code Starts{-->
</head>
<body>
<button class="button">Click Me!</button>
</body>
</html>
<!--Driver Code Ends }-->
4. ID Selector (#): Styles a single element identified by its unique id. For example, changing the background color of a header
HTML
<!--Driver Code Starts{-->
<html>
<head>
<!--Driver Code Ends }-->
<style>
#header {
background-color: gray;
}
</style>
<!--Driver Code Starts{-->
</head>
<body>
<div id="header">This is the header section.</div>
</body>
</html>
<!--Driver Code Ends }-->
Combinator Selectors
Combinators in CSS are used to define relationships between selectors, allowing you to style elements based on their hierarchy or positioning in the document. Common combinators include descendant ( ), child (>), adjacent sibling (+), and general sibling (~).
1. Descendant Selectors: Targets an element inside another, such as paragraphs inside div .For example, styling paragraphs inside a div.
HTML
<!--Driver Code Starts{-->
<html>
<head>
<!--Driver Code Ends }-->
<style>
div p {
color: red;
}
</style>
<!--Driver Code Starts{-->
</head>
<body>
<div>
<p>This paragraph inside a div will be red.</p>
</div>
</body>
</html>
<!--Driver Code Ends }-->
2. Child Selector (>): They only affects the direct child elements of a parent. For example, styling direct children paragraphs of a div.
HTML
<!--Driver Code Starts{-->
<html>
<head>
<!--Driver Code Ends }-->
<style>
div>p {
margin-left: 20px;
}
</style>
<!--Driver Code Starts{-->
</head>
<body>
<div>
<p>This paragraph is a direct child of div and has a left margin.</p>
<div>
<p>This paragraph is not a direct child of the outer div.</p>
</div>
</div>
</body>
</html>
<!--Driver Code Ends }-->
3. Adjacent Sibling Selector (+): Styles an element immediately following another .For example, making the first paragraph bold after an h1.
HTML
<!--Driver Code Starts{-->
<html>
<head>
<!--Driver Code Ends }-->
<style>
h1+p {
font-weight: bold;
}
</style>
<!--Driver Code Starts{-->
</head>
<body>
<h1>This is a header.</h1>
<p>This paragraph is immediately following the header and is bold.</p>
<p>This paragraph will not be bold.</p>
</body>
</html>
<!--Driver Code Ends }-->
4. General Sibling Selector (~): Styles all siblings that follow a specific element. For example, italicizing all paragraphs following an h1.
HTML
<!--Driver Code Starts{-->
<html>
<head>
<!--Driver Code Ends }-->
<style>
h1~p {
font-style: italic;
}
</style>
<!--Driver Code Starts{-->
</head>
<body>
<h1>This is a header.</h1>
<p>This paragraph is a sibling of the header and will be italicized.</p>
<p>This paragraph will also be italicized because it's a sibling of the header.</p>
</body>
</html>
<!--Driver Code Ends }-->
Attribute Selectors
Attribute selectors in CSS target elements based on the presence or value of their attributes. Examples include [attr] (selects elements with the attribute), [attr=”value”] (matches specific values), and [attr^=”val”] (matches values starting with “val”).
1. Presence Selector: It selects elements that contain a specific attribute. For example, styling all inputs with a type attribute.
HTML
<!--Driver Code Starts{-->
<html>
<head>
<!--Driver Code Ends }-->
<style>
input[type] {
border: 2px solid black;
}
</style>
<!--Driver Code Starts{-->
</head>
<body>
<input type="text" placeholder="Text input">
<input type="number" placeholder="Number input">
</body>
</html>
<!--Driver Code Ends }-->
2. Attribute Value Selector: It targets elements with a particular attribute value. For example, styling text inputs.
HTML
<!--Driver Code Starts{-->
<html>
<head>
<!--Driver Code Ends }-->
<style>
input[type="text"] {
background-color: yellow;
}
</style>
<!--Driver Code Starts{-->
</head>
<body>
<input type="text" placeholder="Text input">
<input type="password" placeholder="Password input">
</body>
</html>
<!--Driver Code Ends }-->
3. Substring Matching(^=): It matches elements where the attribute contains a substring. For example, styling links with https in their href.
HTML
<!--Driver Code Starts{-->
<html>
<head>
<!--Driver Code Ends }-->
<style>
a[href^="https"] {
color: green;
}
</style>
<!--Driver Code Starts{-->
</head>
<body>
<a href="https://example.com">Secure link</a>
<a href="http://example.com">Non-secure link</a>
</body>
</html>
<!--Driver Code Ends }-->
4. Wildcard Selector (*=): Matches elements where the attribute value contains a specific string. For example, underlining links with example in the URL.
HTML
<!--Driver Code Starts{-->
<html>
<head>
<!--Driver Code Ends }-->
<style>
a[href*="example"] {
text-decoration: underline;
}
</style>
<!--Driver Code Starts{-->
</head>
<body>
<a href="https://example.com">This link contains 'example' and is underlined.</a>
<a href="https://otherlink.com">This link is not underlined.</a>
</body>
</html>
<!--Driver Code Ends }-->
Pseudo-Classes
Pseudo-classes in CSS define the special states of elements for styling. Examples include :hover (applies when an element is hovered), :first-child (targets the first child of a parent), and :nth-child(2) (targets the second child).
1. :hover: Styles elements when the user hovers over them. For example, changing the color of a link when hovered.
HTML
<!--Driver Code Starts{-->
<html>
<head>
<!--Driver Code Ends }-->
<style>
a:hover {
color: red;
}
</style>
<!--Driver Code Starts{-->
</head>
<body>
<a href="https://example.com">Hover over this link to see the effect.</a>
</body>
</html>
<!--Driver Code Ends }-->
2. :focus: Styles the elements when the user focus on any particular element.
HTML
<!--Driver Code Starts{-->
<html>
<head>
<!--Driver Code Ends }-->
<style>
input:focus {
outline: 3px solid red;
}
</style>
<!--Driver Code Starts{-->
</head>
<body>
<input type="text">
</body>
</html>
<!--Driver Code Ends }-->
3. :first-child: Styles the element which is the first child of it’s parent.
HTML
<!--Driver Code Starts{-->
<html>
<head>
</head>
<!--Driver Code Ends }-->
<style>
p:first-child {
color: brown;
}
</style>
<!--Driver Code Starts{-->
<body>
<div>
<p>Hello1</p>
<p>Hello2</p>
</div>
</body>
</html>
<!--Driver Code Ends }-->
4. :last-child: Style’s the element which is the last child of it’s parent.
HTML
<!--Driver Code Starts{-->
<html>
<head>
</head>
<!--Driver Code Ends }-->
<style>
p:last-child {
color:green;
}
</style>
<!--Driver Code Starts{-->
<body>
<div>
<p>Hello1</p>
<p>Hello2</p>
</div>
</body>
</html>
<!--Driver Code Ends }-->
5. :not: Helps to remove a particular element from the styling index or styling context.
HTML
<!--Driver Code Starts{-->
<html>
<head>
</head>
<!--Driver Code Ends }-->
<style>
p:not(.one) {
color: blue;
}
</style>
<!--Driver Code Starts{-->
<body>
<div>
<p class="one">Hello1</p>
<p class="two">Hello2</p>
</div>
</body>
</html>
<!--Driver Code Ends }-->
5. Pseudo-Element’s
The Pseudo Element help’s to access and control a specific part of an element for inserting content before an element or inserting content after an element. Targeting any specific part of a word or a sentence. It is usually used to beautify the internal content of an element.
1. ::before : It helps to insert some content before an element.
HTML
<!--Driver Code Starts{-->
<html>
<head>
</head>
<!--Driver Code Ends }-->
<style>
h1::before {
content: "★ "
}
</style>
<!--Driver Code Starts{-->
<body>
<h1 tabindex="0">Welcome to GFG</h1>
</body>
</html>
<!--Driver Code Ends }-->
2. ::after: It help’s to insert some content after an element.
HTML
<!--Driver Code Starts{-->
<html>
<head>
</head>
<!--Driver Code Ends }-->
<style>
h1:active::before {
content: "☀ ";
color: orangered;
}
</style>
<!--Driver Code Starts{-->
<body>
<h1 tabindex="0">Welcome to GFG</h1>
</body>
</html>
<!--Driver Code Ends }-->
3. ::first-line: Styles the first line of text within a block element. Line breaks mark the beginning of a new line.
HTML
<!--Driver Code Starts{-->
<html>
<head>
</head>
<!--Driver Code Ends }-->
<style>
p::first-line {
color: red;
}
</style>
<!--Driver Code Starts{-->
<body>
<p>Welcome to GFG<br>
Hello GFG</p>
</body>
</html>
<!--Driver Code Ends }-->
4. ::first-letter: It Styles the first-letter of a word or a sentence.
HTML
<!--Driver Code Starts{-->
<html>
<head>
</head>
<!--Driver Code Ends }-->
<style>
p::first-letter {
color: red;
font-size: 23px;
}
</style>
<!--Driver Code Starts{-->
<body>
<p>Welcome to GFG</p>
</body>
</html>
<!--Driver Code Ends }-->
5. ::placeholder: Styles the placeholder of a specific input field.
HTML
<!--Driver Code Starts{-->
<html>
<head>
</head>
<!--Driver Code Ends }-->
<style>
input::placeholder {
font-size: 20x;
font-family: sans-serif;
font-weight: 900;
}
</style>
<!--Driver Code Starts{-->
<body>
<input type="text" placeholder="Enter your name">
</body>
</html>
<!--Driver Code Ends }-->
Similar Reads
CSS Tutorial
CSS stands for Cascading Style Sheets. It is a stylesheet language used to style and enhance website presentation. CSS is one of the main three components of a webpage along with HTML and JavaScript.HTML adds Structure to a web page.JavaScript adds logic to it and CSS makes it visually appealing or
6 min read
CSS Introduction
CSS (Cascading Style Sheets) is a language designed to simplify the process of making web pages presentable. It allows you to apply styles to HTML documents by prescribing colors, fonts, spacing, and positioning.The main advantages are the separation of content (in HTML) and styling (in CSS) and the
5 min read
CSS Syntax
CSS (Cascading Style Sheets) is a stylesheet language used to describe the presentation of a document written in HTML. Understanding CSS syntax is fundamental for creating visually appealing and well-structured web pages. Basic CSS SyntaxCSS is written as rulesets. A ruleset consists of a selector a
6 min read
CSS Selectors
CSS selectors are used to target HTML elements on your pages, allowing you to apply styles based on their ID, class, type attributes, and more. There are mainly 5 types of selectors.Basic CSS Selectors: These are used to target elements by tag, .class, or #id for fundamental styling needs.Combinator
7 min read
CSS Comments
CSS comments are used to add notes or explanations to your code, helping you and others understand it better. They start with /* and end with */ and can be used for both single-line and multi-line comments. Note: Comments are ignored by browsers, so they wonât affect how your webpage looks or works.
2 min read
CSS Colors
CSS colors are used to set the color of different parts of a webpage, like text, background, and borders. This helps make the page look more attractive and easier to read. You can define colors using names, hex codes, RGB values, and more. You can try different formats of colors here- #content-ifram
6 min read
CSS Borders
Borders in CSS are used to create a visible outline around an element. They can be customized in terms of Width: The thickness of the border.Style: The appearance of the border (solid, dashed, dotted, etc.).Color: The color of the border.You can try different types of borders here- #custom-iframe{ h
6 min read
CSS Margins
CSS margins are used to create space around an element, separating it from neighboring elements and the edges of the webpage. They control the layout by adjusting the distance between elements, providing better organization and readability. Syntax:body { margin: value;}[GFGTABS] HTML <!--Driver C
4 min read
CSS Height and Width
Height and Width in CSS are used to set the height and width of boxes. Their values can be set using length, percentage, or auto. Width and HeightThe width and height properties in CSS are used to define the dimensions of an element. The values can be set in various units, such as pixels (px), centi
4 min read
CSS Outline
CSS outline is a property used to draw a line around an element's border. It does not affect the layout, unlike borders. It's often used to highlight elements, providing a visual emphasis without altering the dimensions of the element. Syntaxselector{ outline: outline-width outline-type outline-colo
4 min read