CSS Tutorial || CSS Selectors
CSS Selectors
CSS selectors are used to "find" (or select) the HTML elements you want to style
CSS selectors select HTML elements according to its id, class, type, attribute etc.
Different types of selectors in CSS.
- CSS Element Selector
- CSS Id Selector
- CSS Class Selector
- CSS Universal Selector
- CSS Group Selector
- Type selector
- Attribute selector
Universal selector
Selects all elements. Optionally, it may be restricted to a specific namespace or to all namespaces.Syntax: * ns|* *|*
Example: * will match all the elements of the document.
- <!DOCTYPE html>
- <html>
- <head>
- <style>
- * {
- color: red;
- text-align: center;
- }
- </style>
- </head>
- <body>
- <h2>This is heading</h2>
- <p>This is paragraph1.</p>
- <p > This is paragraph2.</p>
- </body>
- </html>
RESULT
This is heading
This is paragraph1.This is paragraph2.
element selector
Selects all elements that have the given node name.Syntax: elementname
Example: input will match any <input> element.
- <!DOCTYPE html>
- <html>
- <head>
- <style>
- p.para1 {
- color: red;
- text-align: center;
- }
- </style>
- </head>
- <body>
- <h2 class="para1">This is heading</h2>
- <p class="para1">This is paragraph1.</p>
- <p > This is paragraph2.</p>
- </body>
- </html>
RESULT
This is heading
This is paragraph1.
This is paragraph2.Class selector
Selects all elements that have the given class attribute.Syntax: .classname
Example: .index will match any element that has a class of "index".
- <!DOCTYPE html>
- <html>
- <head>
- <style>
- .para1 {
- color: red;
- text-align: center;
- }
- </style>
- </head>
- <body>
- <h2>This is heading</h2>
- <p class="para1">This is paragraph1.</p>
- <p > This is paragraph2.</p>
- </body>
- </html>
RESULT
This is heading
This is paragraph1.
This is paragraph2.ID selector
Selects an element based on the value of its id attribute. There should be only one element with a given ID in a document.Syntax: #idname
Example: #toc will match the element that has the ID "toc".
- <!DOCTYPE html>
- <html>
- <head>
- <style>
- #para1 {
- color: red;
- text-align: center;
- }
- </style>
- </head>
- <body>
- <h2>This is heading</h2>
- <p id="para1">This is paragraph1.</p>
- <p > This is paragraph2.</p>
- </body>
- </html>
RESULT
This is heading
This is paragraph1.
This is paragraph2.
Attribute selector
Selects all elements that have the given attribute.Syntax: [attr] [attr=value] [attr~=value] [attr|=value] [attr^=value] [attr$=value] [attr*=value]
Example: [autoplay] will match all elements that have the autoplay attribute set (to any value).
CSS classes can be reusable and applied to many elements. Class selectors are denoted with a period ' . 'followed by the class name. CSS ID selectors should be unique and used to style only a single element. ID selectors are denoted with a hash sign' # 'followed by the id name
- <!DOCTYPE html>
- <html>
- <head>
- <style>
- a[target] {
- background-color: yellow;
- }
- </style>
- </head>
- <body>
- <a href="https://www.4utechie.com/">techie1 </a>
- <a href="https://www.4utechie.com/" target="_blank">techie2 </a>
- <a href="https://www.4utechie.com/" target="_top">techie3 </a>
- </body>
- </html>
RESULT
tchie1 tchie2 tchie3
Grouping selectors
Selector listThe , is a grouping method, it selects all the matching nodes.
Syntax: A, B
Example: div, span will match both <span> and <div> elements.
- <!DOCTYPE html>
- <html>
- <head>
- <style>
- h2, p {
- color: red;
- text-align: center;
- }
- </style>
- </head>
- <body>
- <h2>This is heading</h2>
- <p id="para1">This is paragraph1.</p>
- <p > This is paragraph2.</p>
- </body>
- </html>
RESULT
This is heading
This is paragraph1.
This is paragraph2.