Key Uses of CSS
Styling Text
CSS allows you to control the appearance of text, such as:
- Font size, family, and color.
- Line spacing and letter spacing.
- Text alignment and decoration (e.g., underline, bold, italic).
p {
font-family: Arial, sans-serif;
color: #333;
font-size: 16px;
}
Page Layout and Positioning
CSS enables the creation of complex layouts, including:
- Organizing content into grids or flex containers.
- Defining widths, heights, margins, and padding for elements.
- Positioning elements using methods like relative, absolute, or fixed positioning.
.container {
display: flex;
justify-content: space-between;
}
Styling Backgrounds and Borders
CSS is used to style element backgrounds and borders:
- Add colors, gradients, or images as backgrounds.
- Customize borders with different widths, styles, and rounded corners.
div {
background-color: lightblue;
border: 2px solid black;
border-radius: 10px;
}
Responsive Design
CSS helps websites adapt to different screen sizes and devices. Media queries allow developers to apply specific styles based on screen dimensions or device types.
@media (max-width: 600px) {
body {
font-size: 14px;
}
}
Adding Animations and Transitions
CSS can create dynamic effects to enhance user experience:
- Smooth transitions between states (e.g., button hover effects).
- Keyframe animations for complex motions.
button:hover {
background-color: green;
transition: background-color 0.3s ease;
}