CSS Selectors
Selectors target the HTML elements on your pages. They allow you to add styles based on their ID, class, type, attribute.
Types of selectors:
- Simple Selectors
- Element Selector
- Id Selector
- Class Selector
- Universal Selector
- Group Selector
- Attribute Selector
- Pseudo-Class Selector
- Pseudo-Element Selector
Simple Selectors
Element Selector
Selects HTML elements based on their tag names.
Id Selector
Targets an HTML element with a specific id attribute.
Class Selector
Selects elements with a particular class attribute.
/* element selector */
p {
color: blue;
}
/* id selector */
#container {
color: red;
}
/* class selector */
.intro {
color: green;
}
Universal Selector:
Used to select all the elements in an HTML document. It will be applied to each and every HTML element on the page
/* universal selector */
* {
color: purple;
}
Group Selector:
It is used to style all comma-separated elements with the same style.
Suppose you want to apply common styles to different selectors, instead of writing rules separately you can write them in groups
/* grouping selector */
h2, h3 {
color: orange;
}
Attribute Selector
It is used to select the elements with a specified attribute or attribute value.
/* attribute selector */
a[href] {
color: brown;
}
Pseudo-class Selector
It is used to style a special type of state of any element.
Example: It is used to style an element when a mouse cursor hovers over it.
/* pseudo-class selector*/
a:link {
color: yellow;
}
Pseudo-element Selector
It is used to style any specific part of the element.
Example- It is used to style the first letter or the first line of any element.
/* pseudo-element selector */
p::first-line {
color: pink;
}
CSS Properties
CSS properties are the various attributes that you can use to style and format HTML elements on a web page.
Each property has a specific set of allowed values, and these properties control different aspects of an element's appearance or behavior.
Color Properties:
color
: Sets the text colorbackground-color
: Sets the background color
Text Properties:
font-family
: Specifies the fontfont-size
: Sets the text sizefont-weight
: Controls text boldnesstext-align
: Aligns text (left, right, center, justify)
Box Model Properties:
width
,height
: Set element dimensionsmargin
: Controls space outside an elementborder
: Sets element borders
Layout Properties:
display
: Specifies how an element should be displayedposition
: Controls element positioningfloat
: Places an element to the left or right of its container
Flexbox Properties:
flex-direction
: Sets the direction of flex itemsjustify-content
: Aligns flex items along the main axis
Grid Properties:
grid-template-columns
: Defines columns in a grid layoutgrid-gap
: Sets gaps between grid items