Skip to content

Commit def8006

Browse files
authored
Merge pull request #375 from AmrutaJayanti/LeetCode
Create Colors-and-Backgrounds.md
2 parents 6630119 + 67a7b8b commit def8006

File tree

1 file changed

+150
-0
lines changed

1 file changed

+150
-0
lines changed
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
# Colors and Background
2+
3+
In CSS, controlling colors and background properties is fundamental for styling web pages. This includes setting text colors, background colors, images, gradients, and more. Below are the main properties and techniques for handling colors and backgrounds in CSS.
4+
5+
**1. Text Color**
6+
7+
The color property sets the color of the text. Colors can be defined using various formats, such as named colors, HEX codes, RGB, RGBA, HSL, and HSLA.
8+
9+
Hex - Codes :
10+
11+
<img src="https://tse4.mm.bing.net/th?id=OIP.9ipFzqjVN-PlJKwrpuAiuQHaFM&pid=Api&P=0&h=180" alt="HexCode" height="200cm" width="200cm"/>
12+
13+
RGB (Red-Green-Blue):
14+
15+
<img src="https://tse4.mm.bing.net/th?id=OIP.zBlyNAyf8fFyqp7_vdv2mgHaCx&pid=Api&P=0&h=180" alt="RGB"/>
16+
17+
HSL (Hue-Saturation-Lightness):
18+
19+
<img src="https://giggster.com/guide/static/fed42130c194b0c240a4ec10408adf97/8282f/hsl-cover-2.png" alt="HSL"/>
20+
21+
```css
22+
/* Named color */
23+
p {
24+
color: red;
25+
}
26+
27+
/* HEX code */
28+
h1 {
29+
color: #ff5733;
30+
}
31+
32+
/* RGB */
33+
div {
34+
color: rgb(255, 87, 51);
35+
}
36+
37+
/* RGBA (with opacity) */
38+
span {
39+
color: rgba(255, 87, 51, 0.8);
40+
}
41+
42+
/* HSL */
43+
a {
44+
color: hsl(11, 100%, 60%);
45+
}
46+
47+
/* HSLA (with opacity) */
48+
footer {
49+
color: hsla(11, 100%, 60%, 0.8);
50+
}
51+
```
52+
53+
**2. Background Color**
54+
55+
The `background-color` property sets the background color of an element.
56+
57+
```css
58+
/* Named color */
59+
body {
60+
background-color: lightblue;
61+
}
62+
63+
/* HEX code */
64+
header {
65+
background-color: #333333;
66+
}
67+
68+
/* RGB */
69+
section {
70+
background-color: rgb(240, 240, 240);
71+
}
72+
73+
/* RGBA (with opacity) */
74+
nav {
75+
background-color: rgba(0, 0, 0, 0.5);
76+
}
77+
78+
/* HSL */
79+
article {
80+
background-color: hsl(210, 100%, 95%);
81+
}
82+
83+
/* HSLA (with opacity) */
84+
aside {
85+
background-color: hsla(210, 100%, 95%, 0.8);
86+
}
87+
```
88+
89+
**3. Background Image**
90+
91+
The `background-image` property sets an image as the background of an element. You can also control its position, size, repeat behavior, and more.
92+
93+
```css
94+
/* Background image */
95+
body {
96+
background-image: url('path/to/image.jpg');
97+
background-repeat: no-repeat; /* Prevents image from repeating */
98+
background-size: cover; /* Scales image to cover the entire element */
99+
background-position: center; /* Centers the image */
100+
}
101+
102+
/* Gradient background */
103+
div {
104+
background-image: linear-gradient(to right, red, yellow);
105+
}
106+
```
107+
108+
**4. Background Repeat**
109+
110+
The `background-repeat` property controls if/how a background image repeats.
111+
112+
```css
113+
div {
114+
background-image: url('path/to/image.jpg');
115+
background-repeat: no-repeat; /* Do not repeat the image */
116+
background-repeat: repeat-x; /* Repeat the image horizontally */
117+
background-repeat: repeat-y; /* Repeat the image vertically */
118+
background-repeat: round; /* Repeat the image, but rescale to fit */
119+
background-repeat: space; /* Repeat the image, but add space between repetitions */
120+
}
121+
```
122+
123+
**5. Background Size**
124+
125+
The `background-size` property specifies the size of the background image.
126+
127+
```css
128+
section {
129+
background-image: url('path/to/image.jpg');
130+
background-size: auto; /* Default, original size */
131+
background-size: cover; /* Scale the image to cover the element */
132+
background-size: contain; /* Scale the image to be contained within the element */
133+
background-size: 100px 200px; /* Specify exact size */
134+
background-size: 50% 50%; /* Specify size as a percentage */
135+
}
136+
```
137+
138+
**6. Background Position**
139+
140+
The `background-position` property sets the starting position of the background image.
141+
142+
```css
143+
header {
144+
background-image: url('path/to/image.jpg');
145+
background-position: left top; /* Align image to top left */
146+
background-position: center; /* Center the image */
147+
background-position: 50% 50%; /* Center the image using percentages */
148+
background-position: 10px 20px; /* Set position using exact values */
149+
}
150+
```

0 commit comments

Comments
 (0)