CSS selectors are patterns used to select the elements you want to style. The most common selectors are the type, class, and ID selectors.
Selects all elements of a given type. For example, p
selects all paragraph elements.
Selects all elements with a specific class attribute. Use a period (.) followed by the class name, e.g., .example
.
Selects a single element with a specific ID. Use a hash (#) followed by the ID name, e.g., #unique
.
/* Type Selector */
p {
color: blue;
}
/* Class Selector */
.example {
font-weight: bold;
}
/* ID Selector */
#unique {
background-color: yellow;
}
Selects all elements that are descendants of a specified element. For example, div p
selects all p
elements inside div
elements.
Selects all elements that are direct children of a specified element. For example, ul > li
selects all li
elements that are direct children of a ul
.
/* Descendant Selector */
div p {
margin-bottom: 10px;
}
/* Child Selector */
ul > li {
list-style-type: none;
}
Selects elements based on an attribute or attribute value. For example, [type="text"]
selects all input elements with a type attribute of "text".
Selects elements based on their state. Common pseudo-classes include :hover
, :focus
, and :nth-child()
.
Selects and styles a part of an element. Examples include ::before
, ::after
, and ::first-line
.
/* Attribute Selector */
input[type="text"] {
border: 1px solid #ccc;
}
/* Pseudo-class Selector */
a:hover {
color: red;
}
/* Pseudo-element Selector */
p::first-line {
font-weight: bold;
}
Selects all elements in a document. It is represented by an asterisk (*).
Groups multiple selectors to apply the same styles to different elements. For example, h1, h2, h3
applies the same styles to all three heading levels.
/* Universal Selector */
* {
box-sizing: border-box;
}
/* Grouping Selector */
h1, h2, h3 {
color: navy;
}
Selects an element that is immediately preceded by a specified element. For example, h1 + p
selects the first p
element that follows an h1
.
Selects all elements that are siblings of a specified element. For example, h1 ~ p
selects all p
elements that are siblings of an h1
.
/* Adjacent Sibling Selector */
h1 + p {
margin-top: 0;
}
/* General Sibling Selector */
h1 ~ p {
color: gray;
}
These selectors match elements based on substrings within attribute values. Examples include [attr^="value"]
for prefix, [attr$="value"]
for suffix, and [attr*="value"]
for substring.
/* Attribute Substring Selectors */
a[href^="https"] {
color: green;
}
a[href$=".pdf"] {
text-decoration: underline;
}
a[href*="example"] {
font-style: italic;
}
Selects elements based on their position in a group of siblings. For example, :nth-child(2)
selects the second child.
Similar to :nth-child
, but only considers siblings of the same type.
/* :nth-child Selector */
li:nth-child(odd) {
background-color: #f9f9f9;
}
/* :nth-of-type Selector */
p:nth-of-type(2) {
font-size: larger;
}
Selects the first or last child element among a group of siblings.
/* :first-child Selector */
p:first-child {
margin-top: 0;
}
/* :last-child Selector */
p:last-child {
margin-bottom: 0;
}
Selects every checked <input>
element (checkboxes or radio buttons).
Selects every disabled <input>
element.
/* :checked Selector */
input:checked {
border-color: blue;
}
/* :disabled Selector */
input:disabled {
background-color: #ddd;
}
The :enabled
selector selects every enabled <input>
element, while the :focus
selector selects the element that has focus.
/* :enabled Selector */
input:enabled {
border: 1px solid green;
}
/* :focus Selector */
input:focus {
outline: 2px solid orange;
}
Selects every element that is not a specified element. For example, :not(.excluded)
selects all elements that do not have the class "excluded".
Selects the target element of a URL containing a fragment identifier.
/* :not Selector */
div:not(.excluded) {
background-color: lightblue;
}
/* :target Selector */
#section:target {
border: 2px solid red;
}
Selects every element that has no children (including text nodes).
/* :empty Selector */
div:empty {
display: none;
}
Applies a style when the user hovers over an element.
Applies a style when an element is activated, such as when a button is pressed.
/* :hover Selector */
button:hover {
background-color: #555;
}
/* :active Selector */
button:active {
background-color: #333;
}
The :visited
selector styles links that have been visited, while :link
styles links that have not yet been visited.
/* :visited Selector */
a:visited {
color: purple;
}
/* :link Selector */
a:link {
color: blue;
}
Specificity determines which CSS rule is applied by the browser when multiple rules could apply to the same element. It is calculated based on the types of selectors used.
Some CSS properties are inherited from parent to child elements, meaning the child elements will take on the parent's property value unless overridden.
/* Example of Specificity */
#header .nav li.active {
color: white; /* More specific than .nav li.active */
}
/* Example of Inheritance */
body {
font-family: Arial, sans-serif; /* This property is inherited by child elements */
}
The !important
rule in CSS is used to add more importance to a property/value than normal. It overrides any other declarations, even if they have higher specificity.
/* !important Rule */ p { color: red !important; /* This will override any other color declarations for
elements */ }
Newsletter
Subscribe to our newsletter for weekly updates and promotions.
Wiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesAds Policies