CSS (Cascading Style Sheets) is the language used to style and layout web pages. It works alongside HTML to control the visual presentation of your content, making your websites beautiful and responsive.
CSS stands for Cascading Style Sheets. It's a stylesheet language that describes how HTML elements should be displayed on screen, paper, or in other media.
CSS consists of selectors and declaration blocks:
selector { property: value; property: value; }
p {
color: blue;
font-size: 16px;
margin: 10px;
}
Targets all elements of a specific type:
h1 {
color: navy;
text-align: center;
}
p {
line-height: 1.6;
}
Targets elements with a specific class attribute:
.highlight {
background-color: yellow;
font-weight: bold;
}
.button {
padding: 10px 20px;
border: none;
border-radius: 4px;
}
Targets a single element with a specific ID:
#header {
background-color: #333;
color: white;
padding: 20px;
}
#main-content {
max-width: 1200px;
margin: 0 auto;
}
Create a separate .css file and link it in your HTML:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<!-- Content here -->
</body>
</html>
styles.css:
body {
font-family: Arial, sans-serif;
line-height: 1.6;
}
Add CSS inside a <style> tag in the HTML head:
<!DOCTYPE html>
<html>
<head>
<style>
body {
font-family: Arial, sans-serif;
line-height: 1.6;
}
h1 {
color: blue;
}
</style>
</head>
<body>
<!-- Content here -->
</body>
</html>
Add CSS directly to HTML elements (use sparingly):
<p style="color: red; font-size: 18px;">This is a red paragraph.</p>
CSS stands for "Cascading" Style Sheets because styles cascade down from more general rules to more specific ones.
More specific selectors override less specific ones:
/* Low specificity */
p {
color: black;
}
/* Higher specificity */
.content p {
color: blue;
}
/* Even higher specificity */
#main-content p.highlight {
color: red;
}
.text-styling {
font-family: 'Arial', sans-serif;
font-size: 16px;
font-weight: bold;
font-style: italic;
text-align: center;
text-decoration: underline;
color: #333333;
line-height: 1.5;
}
.background-example {
background-color: #f0f0f0;
background-image: url('pattern.jpg');
background-repeat: no-repeat;
background-position: center;
background-size: cover;
}
.border-example {
border: 2px solid #333;
border-radius: 8px;
border-left: 4px dotted red;
}
.spacing-example {
margin: 20px; /* All sides */
padding: 10px 15px; /* Top/bottom: 10px, Left/right: 15px */
margin-top: 10px; /* Just top */
padding-left: 20px; /* Just left */
}
CSS supports multiple color formats:
.color-examples {
/* Hexadecimal */
color: #ff0000;
/* RGB */
color: rgb(255, 0, 0);
/* RGBA (with transparency) */
color: rgba(255, 0, 0, 0.5);
/* HSL */
color: hsl(0, 100%, 50%);
/* HSLA (with transparency) */
color: hsla(0, 100%, 50%, 0.5);
/* Color names */
color: red;
}
Comments are ignored by browsers and help document your code:
/* This is a CSS comment */
h1 {
color: blue; /* Make headings blue */
}
/*
This is a multi-line comment
that spans multiple lines
*/
px: Pixels (screen dots)pt: Points (1/72 of an inch)in: Inchescm: Centimetersmm: Millimeters%: Percentage of parent elementem: Relative to the element's font sizerem: Relative to the root font sizevw: Viewport width (1% of viewport width)vh: Viewport height (1% of viewport height).unit-examples {
font-size: 16px; /* Absolute */
width: 50%; /* Relative to parent */
margin: 2em; /* 2 times the font size */
padding: 1rem; /* 1 times the root font size */
height: 100vh; /* Full viewport height */
}
Keep CSS in separate .css files for better organization and caching.
Choose descriptive class names that explain their purpose:
/* Good */
.navigation-menu, .call-to-action-button, .user-profile-card
/* Avoid */
.red-text, .big-font, .margin-10
Group related styles together and use comments:
/* === HEADER STYLES === */
.header { /* styles */ }
.nav { /* styles */ }
/* === MAIN CONTENT === */
.main-content { /* styles */ }
.sidebar { /* styles */ }
Design for mobile devices first, then add styles for larger screens:
/* Base styles (mobile) */
.container {
width: 100%;
padding: 10px;
}
/* Tablet and up */
@media (min-width: 768px) {
.container {
max-width: 750px;
}
}
/* Desktop and up */
@media (min-width: 1024px) {
.container {
max-width: 1200px;
}
}
Create a simple styled webpage:
In this lesson, you learned:
In the next lesson, you'll dive deeper into CSS selectors and properties to create more sophisticated styles.