
CSS Selectors
CSS Selectors CSS Selectors are patterns used in CSS to select and target HTML elements so that styles can be applied to them. They define which elements on a web page should receive specific styling rules. Used to select HTML elements based on tag name, class, id, or attributes. Help apply...
Header Text
Paragraph Text
2. Element Selector: Targets all elements of a specific type, such as paragraphs or headers. Example: Setting a common font size for all paragraphsThis paragraph styled with font size 16px.
3. Class Selector (.): Applies styles to elements with a specific class attribute. Example: Making all buttons have a blue background. 4. ID Selector (#): Styles a single element identified by its unique id. Example: changing the background color of a header.This paragraph inside a div will be red.
This is a direct child and has a left margin.
This is not a direct child.
This is a header.
This is immediately following the header and is bold.
This will not be bold.
4. General Sibling Selector (~): Styles all siblings that follow a specific element. Example: Italicizing all paragraphs following an h1.This is a header.
This is a sibling of the header and will be italicized.
This will also be italicized because it's a sibling of the header.
3. Attribute Selectors Attribute selectors in CSS target elements based on the presence or value of their attributes. 1. Presence Selector: It selects elements that contain a specific attribute. Example: styling all inputs with a type attribute. 2. Attribute Value Selector: It targets elements with a particular attribute value. Example: Styling text inputs. 3. Substring Matching(^=): It matches elements where the attribute contains a substring. Example: Styling links with https in their href. Secure link Non-secure link 4. Wildcard Selector (*=): Matches elements where the attribute value contains a specific string. Example: Underlining links with example in the URL. This contains 'example' and is underlined. This is not underlined. 5. Ends With Selector ($=): Matches elements whose attribute value ends with a specific string. Example: Styling links that end with .pdf in their URL. Word Match Selector (~=): Matches elements whose attribute contains a specific whole word (space-separated). Example: Styling elements that have the class highlight among multiple class names. Hyphen Match Selector (|=): Matches elements whose attribute value starts with a word followed by a hyphen. Example: Styling elements with language attributes like en or en-US. 4. Pseudo-Classes Pseudo-classes in CSS define the special states of elements for styling. :hover: Styles elements when the user hovers over them. Example: Changing the color of a link when hovered. Hover over this to see the effect. :focus: Styles the elements when the user focus on any particular element. :first-child: Styles the element which is the first child of it's parent.Hello1
Hello2
Hello1
Hello2
Hello1
Hello2
Welcome to GFG
::after: To insert some content after an element.Welcome to GFG
::first-line: Styles the first line of text within a block element. Line breaks mark the beginning of a new line.Welcome to GFG
Hello GFG
Welcome to GFG
::placeholder: Styles the placeholder of a specific input field. References: https://www.w3schools.com/css/css_howto.asp https://www.geeksforgeeks.org/css/css-selectors/ https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Selectors/Type_selectors https://www.programiz.com/css/units📰Originally published at dev.to
Staff Writer