Pseudo-elements in CSS are used to style specified parts of an element. They allow you to do things like add content before or after an element's content, style the first letter or line, and much more.
Some of the most common pseudo-elements include ::before
, ::after
, ::first-letter
, and ::first-line
. These elements are often used for adding decorative content or styling specific portions of text.
Below is a simple example demonstrating how to use ::before
and ::after
pseudo-elements to add content around an element.
p::before {
content: "Start: ";
color: cyan;
}
p::after {
content: " :End";
color: cyan;
}
In this example, the ::before
pseudo-element adds the word "Start: " before the content of every paragraph, and the ::after
pseudo-element adds " :End" after it. This is a simple way to dynamically include additional text without modifying the HTML structure.
Example Output:
Start: This is a paragraph. :End
The ::first-letter
pseudo-element is used to apply styles to the first letter of a block-level element, such as making it larger or changing its color to draw attention.
Similarly, the ::first-line
pseudo-element allows you to style the first line of a block element, which can be useful for creating drop caps or other typographic effects.
Here is an example of how these pseudo-elements can be used to enhance text appearance.
p::first-letter {
font-size: 2em;
color: teal;
}
p::first-line {
font-weight: bold;
color: orange;
}
In this example, the first letter of the paragraph is styled to be larger and teal in color, while the first line is bolded and colored orange, making the text more visually appealing.
Example Output:
This is the first line of the paragraph.
The ::selection
pseudo-element is used to customize the appearance of text when it is highlighted or selected by the user. This can improve user experience by providing a cohesive theme.
Commonly, ::selection
is used to change the background color and text color of selected text, matching the overall design of the website.
Below is a code snippet demonstrating how to use ::selection
to alter the selection colors.
::selection {
background: red;
color: white;
}
In this example, when text is selected, the background turns red and the text becomes white, providing a clear contrast that enhances readability during selection.
Example Output:
Select this text to see the effect.
The ::marker
pseudo-element allows styling of the bullet or number markers in lists. This provides greater flexibility in designing list items to match your webpage's style.
With ::marker
, you can change the color, font, and even the content of list markers. This is particularly useful for creating unique and branded list styles.
Here is an example demonstrating how to use ::marker
to style list markers.
ul li::marker {
color: pink;
font-size: 1.5em;
}
In this example, the list markers are styled to be pink and larger than the default size, drawing attention to each list item and enhancing the visual hierarchy.
Example Output:
The ::placeholder
pseudo-element is used to style the placeholder text in input fields. This can help guide users by making placeholders more noticeable or aesthetically pleasing.
You can change the color, font, and size of the placeholder text, allowing it to blend seamlessly with your site's design.
Below is an example of how to use ::placeholder
to style input placeholders.
input::placeholder {
color: orange;
font-style: italic;
}
In this example, the placeholder text within input fields is styled to be orange and italicized, making it distinct yet harmonious with the input field design.
Example Output:
The ::backdrop
pseudo-element is used to style the backdrop of elements such as modals or fullscreen elements. It is particularly useful for creating overlays and enhancing focus on dialog content.
You can modify the color, opacity, and even add animations to the backdrop, providing a more immersive user experience.
Here is an example demonstrating how to use ::backdrop
to style a modal backdrop.
dialog::backdrop {
background-color: rgba(0, 0, 255, 0.5);
}
In this example, the backdrop of a dialog element is styled with a semi-transparent blue color, creating a soft overlay that draws attention to the dialog content.
Example Output:
The ::part
and ::slotted
pseudo-elements are used in conjunction with web components to style parts of shadow DOM and slotted content, respectively.
These pseudo-elements allow developers to apply styles to parts of custom elements, enabling more granular control over component styling without breaking encapsulation.
Below is an example of how these pseudo-elements can be used to style web components.
custom-element::part(button) {
background-color: yellow;
}
::slotted(p) {
color: teal;
}
In this example, the ::part
pseudo-element is used to style a button within a custom element, while ::slotted
styles paragraphs that are slotted into the shadow DOM.
Example Output:
Slotted Paragraph
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