Commonly Used CSS Style Properties
Text Styling
| Property | Role |
|---|---|
color | Text color |
font-size | Text size |
font-family | Font |
font-weight | Font weight |
line-height | Line spacing |
text-decoration | Text decoration such as underline/strikethrough |
Background Images: background-image
The background-image property lets you put a background image on an element.
.card {
background-image: url("photo.jpg");
background-repeat: no-repeat; /* whether to repeat */
background-position: center; /* position */
background-size: cover; /* size */
}You need to set repeat, position and size together for the image to be placed neatly the way you want.
Gradients: linear-gradient
A gradient is an effect that changes smoothly from one color to another. You create it by putting the linear-gradient() function in background-image.
.banner {
background-image: linear-gradient(to right, #ff7e5f, #feb47b);
}- You can adjust the direction with values like
to right. - You can also add intermediate colors by specifying ratios between colors.
- Rather than calculating values by hand, it's much faster to generate the code while previewing it in a tool like cssgradient.io.
- Placing a gradient over an image can also improve text readability.
Shadows: box-shadow
box-shadow is the property that adds a shadow to an element.
.card {
box-shadow: 4px 4px 8px 2px rgba(0, 0, 0, 0.3);
/* x y blur spread color */
}Rather than forcing yourself to memorize the order of the values, it's better to understand them by meaning (position, blur, spread, color). In practice you often just copy the values a designer gives you, and if it's confusing, search for "box shadow generator" and learn by playing with it yourself.
Opacity: opacity vs rgba
opacity makes the entire element (including text and background) semi-transparent. The value is between 0 (fully transparent) and 1 (fully opaque).
.disabled-button {
opacity: 0.5; /* the whole button fades */
}If you want only the background to be semi-transparent while keeping the text sharp, use rgba() for the background color instead of opacity. opacity is often used for things like sold-out/disabled indicators, and rgba for things like semi-transparent overlays.
Summary
| Concept | One-line summary |
|---|---|
| Text styles | color, font-size, font-family, font-weight, line-height |
| background-image | Puts a background image on an element and controls repeat/position/size |
| linear-gradient | A background effect that blends smoothly from one color to another |
| box-shadow | Builds a shadow from position, blur, spread and color values |
| opacity vs rgba | opacity makes the whole element semi-transparent, rgba only the background color |
Reference: personal Notion study notes
