Cascading Style Sheets (CSS) is a cornerstone technology of the web, used to style and format HTML documents. It allows developers to control the layout, colors, fonts, and responsiveness of web pages, enhancing both appearance and usability. This guide covers the fundamentals of CSS to help beginners get started.
CSS Basics: A Comprehensive Guide for Beginners. |
CSS (Cascading Style Sheets) is a stylesheet language used to describe the presentation of an HTML document. It separates content from design, enabling developers to create visually appealing and responsive web pages.
CSS applies styles to HTML elements based on selectors. Rules consist of selectors and declarations, like this:
selector {
property: value;
}
Example:
h1 {
color: blue;
font-size: 24px;
}
This code applies a blue color and 24px font size to all <h1>
elements.
<p style="color: red;">This is red text.</p>
<style>
tag in the <head>
section:<style>
p {
color: green;
}
</style>
.css
file linked to HTML:<link rel="stylesheet" href="styles.css">
CSS file example (styles.css
):
p {
color: green;
}
Selectors define which HTML elements are styled.
h1 {
color: blue;
}
.highlight {
background-color: yellow;
}
#main-heading {
text-align: center;
}
h1, h2, h3 {
font-family: Arial, sans-serif;
}
* {
margin: 0;
padding: 0;
}
Text Styling:
p {
font-size: 16px;
font-weight: bold;
text-align: center;
color: #333333;
}
Backgrounds and Borders:
div {
background-color: lightblue;
border: 2px solid black;
border-radius: 10px;
}
Box Model:
div {
width: 200px;
height: 100px;
padding: 10px;
margin: 15px;
}
Positioning Elements:
div {
position: absolute;
top: 50px;
left: 100px;
}
Flexbox for Layouts:
.container {
display: flex;
justify-content: center;
align-items: center;
}
Grid for Layouts:
.grid-container {
display: grid;
grid-template-columns: 1fr 2fr;
gap: 10px;
}
Media Queries:
@media (max-width: 600px) {
body {
background-color: lightgray;
}
}
This example changes the background color for smaller screens.
Units for Responsiveness:
%
: Relative to parent element.em
: Relative to parent font size.rem
: Relative to root font size.vh
and vw
: Relative to viewport height and width.Transitions:
div {
transition: all 0.5s ease-in-out;
}
Animations:
@keyframes move {
0% { transform: translateX(0); }
100% { transform: translateX(100px); }
}
.box {
animation: move 2s infinite alternate;
}
To simplify development, frameworks like Bootstrap and Tailwind CSS provide pre-designed styles and components.
Example with Bootstrap:
<link href="https://cdn.jsdelivr.net/npm/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet">
<button class="btn btn-primary">Click Me</button>
CSS is a versatile and powerful tool for styling web content. By mastering its basics—selectors, properties, layouts, and responsiveness—you can create visually appealing and functional websites. Combining CSS with HTML and JavaScript forms the foundation of modern web development, enabling endless possibilities for design and interactivity.