CSS Notes
CSS Notes
CSS selectors including simple, combinator, pseudo-class, pseudo-element, and attribute selectors:
*Simple Selectors:*
1. *Type Selector:* Selects elements based on their element type. Example: div, p, span.
2. *Class Selector:* Selects elements based on their class attribute. Example: .classname.
5.*Grouping Selector:* Selects multiple elements same time separated by comma. Example:
h1,p,div{//css}
*Combinator Selectors:*
2. *Child Selector:* Selects an element that is a direct child of another specified element. Example: ul >
li.
3. *Adjacent Sibling Selector:* Selects an element that is immediately preceded by a sibling element.
Example: h2 + p.
4. *General Sibling Selector:* Selects elements that are siblings of a specified element. Example: h2 ~ p.
*Pseudo-class Selectors:*
8. *:nth-of-type():* Selects elements based on their position within a parent, counting only elements of
the same type.
*Pseudo-element Selectors:*
3. *::first-line:* Selects the first line of text within the selected element.
4. *::first-letter:* Selects the first letter of text within the selected element.
*Attribute Selectors:*
3. *[attribute~=value]:* Selects elements with an attribute that includes the specified value as one of its
space-separated values.
4. *[attribute|=value]:* Selects elements with an attribute that exactly matches the specified value or
starts with the specified value followed by a hyphen.
notes on the priorities between inline CSS, internal CSS, external CSS, class selectors, ID selectors, tag
name selectors.
** Inline CSS:**
- Highest specificity.
** Selector Specificity:**
- Inline styles have the highest specificity, followed by ID selectors, class selectors, and tag name
selectors.
- The more specific selector takes precedence over less specific selectors.
** ID Selectors:**
** Class Selectors:**
- Less specific than ID selectors but more specific than tag name selectors.
- Can be applied to multiple elements.
** Importance:**
- Should be used sparingly as it can make styles harder to override and maintain.
CSS selectors including simple, combinator, pseudo-class, pseudo-element, and attribute selectors:
*Simple Selectors:*
1. *Type Selector:* Selects elements based on their element type. Example: div, p, span.
2. *Class Selector:* Selects elements based on their class attribute. Example: .classname.
5.*Grouping Selector:* Selects multiple elements same time separated by comma. Example:
h1,p,div{//css}
*Combinator Selectors:*
2. *Child Selector:* Selects an element that is a direct child of another specified element. Example: ul >
li.
3. *Adjacent Sibling Selector:* Selects an element that is immediately preceded by a sibling element.
Example: h2 + p.
4. *General Sibling Selector:* Selects elements that are siblings of a specified element. Example: h2 ~ p.
*Pseudo-class Selectors:*
8. *:nth-of-type():* Selects elements based on their position within a parent, counting only elements of
the same type.
*Pseudo-element Selectors:*
3. *::first-line:* Selects the first line of text within the selected element.
4. *::first-letter:* Selects the first letter of text within the selected element.
*Attribute Selectors:*
3. *[attribute~=value]:* Selects elements with an attribute that includes the specified value as one of its
space-separated values.
4. *[attribute|=value]:* Selects elements with an attribute that exactly matches the specified value or
starts with the specified value followed by a hyphen.
### CSS Colors
- *Example:*
html
- *Description:* Use hex values to specify colors in CSS. The format is #RRGGBB or #RGB.
- *Example:*
html
- *Description:* Use RGB values to define colors. The format is rgb(red, green, blue), where each value
ranges from 0 to 255.
- *Example:*
html
<div style="color: rgb(255, 255, 255); background-color: rgb(0, 0, 0);">This text is white on black.</div>
- *Description:* Use RGBA values to define colors with an alpha channel (transparency). The format is
rgba(red, green, blue, alpha), where alpha ranges from 0 (completely transparent) to 1 (completely
opaque).
- *Example:*
html
<div style="color: rgba(255, 255, 255, 1); background-color: rgba(0, 0, 0, 1);">This text is fully opaque
white on black.</div>
- *Description:* Use HSL values to define colors. The format is hsl(hue, saturation%, lightness%). Hue
ranges from 0 to 360, saturation and lightness from 0% to 100%.
- *Example:*
html
<div style="color: hsl(0, 0%, 100%); background-color: hsl(0, 0%, 0%);">This text is white on
black.</div>
- *Description:* Use HSLA values to define colors with an alpha channel. The format is hsla(hue,
saturation%, lightness%, alpha).
- *Example:*
html
<div style="color: hsla(0, 100%, 50%, 1);">This text is fully opaque red.</div>
<div style="color: hsla(120, 100%, 50%, 0.3);">This text is more transparent green.</div>
<div style="color: hsla(0, 0%, 0%, 0.8);">This text is mostly opaque black.</div>
<div style="color: hsla(0, 0%, 100%, 1); background-color: hsla(0, 0%, 0%, 1);">This text is fully opaque
white on black.</div>
- *Named Colors:* Predefined color names like red, blue, green, etc.
- *HSL Colors:* hsl(hue, saturation%, lightness%) where hue is 0-360, saturation and lightness are 0-
100%
Absolute units are fixed and not relative to other elements. They are useful when you need precise
control over the size of elements.
1. **Pixels (px)**
- *Example:*
html
2. **Centimeters (cm)**
- *Example:*
html
3. **Millimeters (mm)**
- *Example:*
html
- *Example:*
html
5. **Picas (pc)**
- *Example:*
html
6. **Points (pt)**
- *Example:*
html
Relative units are based on the size of other elements and the viewport. They are more flexible and
useful for responsive design.
1. **Percentage (%)**
- *Example:*
html
- *Example:*
html
- *Example:*
html
- *Example:*
html
<div style="width: 50vmin; height: 50vmin; background-color: lightyellow;">50vmin</div>
- *Example:*
html
6. **Em (em)**
- *Description:* Relative to the font-size of the element (2em means 2 times the size of the current
font).
- *Example:*
html
</div>
7. **Rem (rem)**
- *Example:*
html
The display: flex property in CSS allows you to create a flexible layout structure. This is achieved by
defining a flex container that can adapt the size and position of its child elements. Below are detailed
notes on how to use display: flex effectively, along with examples.
- *Description:* Setting an element to display: flex makes it a flex container, with its children becoming
flex items.
- *Example:*
html
</div>
- *Description:* Defines the direction in which the flex items are placed.
- *Example:*
html
</div>
- *Description:* Aligns the flex items along the main axis (horizontal for row).
- space-between: Items are evenly spaced, with the first item at the start and the last item at the end.
- space-around: Items are evenly spaced with equal space around them.
- space-evenly: Items are evenly spaced with equal space between them.
- *Example:*
html
</div>
#### 4. Align Items
- *Description:* Aligns the flex items along the cross axis (vertical for row).
- *Example:*
html
</div>
- *Example:*
html
</div>
### CSS Text Properties
The text-align property specifies the horizontal alignment of text within an element.
- *justify*: Stretches the lines so that each line has equal width (like in newspapers and magazines).
css
p{
text-align: center;
The text-decoration property adds decoration to text. It can be used as a shorthand or with individual
properties.
- *text-decoration-style*: Sets the style of the text decoration (solid, wavy, dotted, dashed, double).
css
p{
*Individual properties:*
css
p{
text-decoration-line: underline;
text-decoration-color: red;
text-decoration-style: wavy;
text-decoration-thickness: 2px;
p{
text-transform: uppercase;
The text-shadow property adds shadow to text. You can specify multiple shadows separated by
commas.
- *Horizontal offset*: Required. Positive values move the shadow to the right, negative values to the left.
- *Vertical offset*: Required. Positive values move the shadow down, negative values up.
- *Blur radius*: Optional. The higher the number, the more blurred the shadow.
css
h1 {
The white-space property controls how text is handled when it is too long to fit in its containing
element.
css
p{
white-space: nowrap;
#### 1. *Overflow*
The overflow property specifies what happens if content overflows an element's box.
- *visible*: Default. Content is not clipped and may be rendered outside the element's box.
- *hidden*: Content is clipped and not visible outside the element's box.
- *scroll*: Content is clipped, but a scrollbar is added to see the rest of the content.
- *auto*: Similar to scroll, but scrollbars are added only when necessary.
css
div {
overflow: auto;
css
div {
overflow-x: hidden;
overflow-y: scroll;
To remove the scrollbar in WebKit browsers (like Chrome and Safari), you can use the ::-webkit-scrollbar
pseudo-element.
css
div {
overflow: auto;
div::-webkit-scrollbar {
}
### CSS Vendor Prefixes in Short
Vendor prefixes allow you to use CSS features that are not yet standardized across all browsers. Each
major browser has its own prefix:
- **-moz-**: Firefox
1. **Transform**
```css
.element {
-webkit-transform: rotate(45deg);
-moz-transform: rotate(45deg);
-ms-transform: rotate(45deg);
-o-transform: rotate(45deg);
transform: rotate(45deg);
```
2. **Transition**
```css
.element {
-webkit-transition: all 0.3s ease;
```
3. **Animation**
```css
.element {
```
4. **Box Shadow**
```css
.element {
```
5. **Border Radius**
```css
.element {
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
border-radius: 10px;
```
- **Modern Use**: Many properties no longer need prefixes in modern browsers, but they are
necessary for older browser support.
### Example
```css
.element {
-webkit-transform: scale(1.2);
-moz-transform: scale(1.2);
-ms-transform: scale(1.2);
-o-transform: scale(1.2);
transform: scale(1.2);
-webkit-border-radius: 8px;
-moz-border-radius: 8px;
border-radius: 8px;
```
div {
overflow: auto;
div::-webkit-scrollbar {
display: none;
```
Using vendor prefixes ensures compatibility across different browsers and their versions.
### CSS Font Properties
The `font-family` property specifies the font for an element. You can specify multiple fonts as a fallback
system. Fonts can be divided into several categories:
```css
body {
```
```css
body {
```
- **Monospace**: Fonts where each character takes up the same amount of width.
```css
body {
```
body {
```
```css
body {
```
The `font-size` property sets the size of the font. It can be specified in various units such as `px`, `em`,
`rem`, `%`, `vw`, etc.
```css
p{
h1 {
```
```css
p{
strong {
.light-text {
```
The `font-style` property specifies whether the font should be italic, oblique, or normal.
```css
em {
p{
}
.oblique-text {
```
To use Google Fonts, you can link them in the `<head>` of your HTML file without using `@import` in
CSS. Here’s how to do it:
1. Go to [Google Fonts](https://fonts.google.com/).
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap"
rel="stylesheet">
<style>
body {
</style>
<title>Document</title>
</head>
<body>
</body>
</html>
```
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link
href="https://fonts.googleapis.com/css2?family=Lobster&family=Roboto:wght@400;700&display=swap
" rel="stylesheet">
<style>
body {
font-family: 'Roboto', sans-serif;
font-size: 16px;
h1 {
font-size: 3em;
font-weight: normal;
p{
font-size: 1em;
font-weight: 400;
font-style: normal;
.highlight {
font-style: italic;
</style>
</head>
<body>
<h1>Welcome to My Website</h1>
<p>This is a paragraph styled with the Roboto font.</p>
</body>
</html>
```
In this example:
- The body text uses the `Roboto` font from Google Fonts.
- The heading `h1` uses the `Lobster` cursive font from Google Fonts.
#### 1. Background-Color
```css
body {
header {
```
#### 2. Background-Image
The `background-image` property sets one or more background images for an element. The value is a
URL to the image.
```css
body {
background-image: url('background.jpg');
```
#### 3. Background-Position
header {
background-image: url('header-bg.jpg');
section {
background-image: url('section-bg.jpg');
```
#### 4. Background-Repeat
```css
body {
background-image: url('pattern.png');
header {
background-image: url('banner.jpg');
}
section {
background-image: url('tile.png');
```
#### 5. Background-Size
```css
header {
background-image: url('header-bg.jpg');
section {
background-image: url('section-bg.jpg');
div {
background-image: url('small-pattern.png');
```
#### 6. Background Shorthand
The `background` shorthand property can be used to set all the background properties at once.
```css
div {
/* Equivalent to:
background-color: #ffcc00;
background-image: url('pattern.png');
background-repeat: no-repeat;
background-position: center;
background-size: cover;
*/
```
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
body {
header {
background-image: url('header-bg.jpg');
background-position: center;
background-repeat: no-repeat;
background-size: cover;
height: 200px;
section {
background-image: url('section-bg.jpg');
background-repeat: no-repeat;
background-size: contain;
padding: 20px;
footer {
/* Shorthand:
background-color: #0044cc;
background-image: url('footer-pattern.png');
background-repeat: repeat;
background-position: center;
background-size: auto;
*/
height: 100px;
</style>
</head>
<body>
<header></header>
<section>
</section>
<footer></footer>
</body>
</html>
```
In this example:
- The `header` has a background image that covers the entire element.
- The `section` uses a background image that is positioned at the top right and is contained within the
element.
- The `footer` uses the shorthand `background` property to set a background color, image, repeat,
position, and size.
### CSS Border Properties
#### 1. Border-Color
```css
div {
```
#### 2. Border-Width
```css
div {
```
#### 3. Border-Style
```css
div {
}
/* Other styles include: none, hidden, dotted, dashed, double, groove, ridge, inset, outset */
```
#### 4. Border-Radius
```css
div {
```
The `border` shorthand property allows you to set the width, style, and color in one declaration.
```css
div {
```
These properties allow you to set the border properties for each side of an element.
```css
div {
```
These properties allow you to set the border radius for each corner of an element.
```css
div {
```
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
.container {
width: 300px;
padding: 20px;
text-align: center;
.special-border {
padding: 10px;
</style>
</head>
<body>
<div class="container">
</div>
<div class="special-border">
<p>This container has different border styles and rounded corners for each side.</p>
</div>
</body>
</html>
```
In this example:
- The `.container` class has a simple solid border with rounded corners using `border-radius`.
- The `.special-border` class demonstrates different border styles, colors, and widths for each side, along
with individual corner radii.
Transition Property :- The transition property in css is used to make some transition
effect.
The transition effect can be defined in two states.(:hover and :active) using pseudo class
selectors.
The Transition property is the combination of 4 properties:-
1. Transition-property
2. Transition-duration
3. Transition-timing-function
4. Transition-delay
Transition-Property : To specify on which property transition should be applied.
Values :- width , Height , all, Background, none.
Transition-duration : This property allows you to determine how long it will take to
complete the transition from one CSS property to the other.
Or
Shorthand Property :
transition: (property name) | (duration) | (timing function) | (delay);
Animation Property :- The CSS animation property is used to specify the animation
that should be applied to an element.
Animation Name :
To animate an element we have to provide animation to the element.
Example: Animation-name : Box1 ;
By this name {Box1} @keyframe will target the element for animation.
@keyframes identifier {
From{
}
}
2. Percentage :-
@keyframes identifier { We can use percentage. By using
0%{ percentage (%) we can target any
moment in the animation
background-color : red;
}
50% {
background-color : green;
}
100%{
background-color : red;
}
}
Value :-
1. normal Forward
2. Reverse
Reverse
3. Alternate
Forward
Reverse
4. alternate-reverse
Reverse
Forward