CSS
Stylesheet language used to design and layout HTML elements
Lessons
- What is CSS?
- CSS Syntax
- CSS Selectors
- Text Styling
- Colors in CSS
- The Box Model
- Classes vs IDs
- Flexbox Layout
- Styling Links
- CSS Positioning
- Responsive Design with Media Queries
- Transitions and Animations
What is CSS?
- CSS stands for Cascading Style Sheets
- CSS describes how HTML elements are to be displayed on screen, paper, or in other media
- CSS saves a lot of work. It can control the layout of multiple web pages all at once
- External stylesheets are stored in CSS files
CSS Demo - One HTML Page - Multiple Styles!
Here we will show one HTML page displayed with four different stylesheets. Click on the "Stylesheet 1", "Stylesheet 2", "Stylesheet 3", "Stylesheet 4" links below to see the different styles
Why Use CSS?
CSS is used to define styles for your web pages, including the design, layout and variations in display for different devices and screen sizes.
CSS Solved a Big Problem
HTML was NEVER intended to contain tags for formatting a web page!
HTML was created to describe the content of a web page, like:
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
When tags like <font>, and color attributes were added to the HTML 3.2 specification, it started a nightmare for web developers. Development of large websites, where fonts and color information were added to every single page, became a long and expensive process.
To solve this problem, the World Wide Web Consortium created CSS.
CSS removed the style formatting from the HTML page!
CSS Saves a Lot of Work!
The style definitions are normally saved in external .css files.
With an external stylesheet file, you can change the look of an entire website by changing just one file!
Example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>home</title>
<style>
body {
background-color: lightblue;
}
h1 {
color: white;
text-align: center;
}
p {
font-family: verdana;
font-size: 20px;
}
</style>
</head>
<body>
</body>
</html>
Example Description:
CSS Syntax
The selector points to the HTML element you want to style.
The declaration block contains one or more declarations separated by semicolons.
Each declaration includes a CSS property name and a value, separated by a colon.
Multiple CSS declarations are separated with semicolons, and declaration blocks are surrounded by curly braces.
Example:
<!DOCTYPE html>
<html>
<head>
<style>
p {
color: red;
text-align: center;
}
</style>
</head>
<body>
<p>Hello World!</p>
<p>These paragraphs are styled with CSS.</p>
</body>
</html>
Example Description:
CSS Selectors
Selectors are used to target HTML elements for styling. Common types include element selectors, class selectors, ID selectors, and combinators.
Example:
<p class="highlight">This is a highlighted paragraph.</p>
<style>
.highlight {
color: red;
font-weight: bold;
}
</style>
Example Description:
Text Styling
CSS allows you to style text properties like color, size, alignment, font family, letter spacing, and line height.
Example:
<h1>Welcome</h1>
<style>
h1 {
color: blue;
font-size: 32px;
text-align: center;
letter-spacing: 2px;
}
</style>
Example Description:
Colors in CSS
Colors can be defined using color names, HEX codes, RGB, or HSL values.
Example:
<div class="color-box">Colored Box</div>
<style>
.color-box {
background-color: #ffcc00;
color: #000;
padding: 10px;
}
</style>
Example Description:
The Box Model
Every HTML element is treated as a box consisting of content, padding, border, and margin.
Example:
<div class="box">Box Content</div>
<style>
.box {
padding: 20px;
border: 2px solid black;
margin: 10px;
}
</style>
Example Description:
Classes vs IDs
Classes can be reused on multiple elements, while IDs should be unique for one element.
Example:
<p class="note">This is a note.</p>
<p id="unique">This is unique.</p>
<style>
.note {
color: green;
}
#unique {
font-weight: bold;
}
</style>
Example Description:
Flexbox Layout
Flexbox helps create responsive layouts with flexible item alignment along horizontal or vertical axes.
Example:
<div class="flex-container">
<div>One</div>
<div>Two</div>
<div>Three</div>
</div>
<style>
.flex-container {
display: flex;
gap: 10px;
}
.flex-container div {
background-color: lightblue;
padding: 20px;
}
</style>
Example Description:
Styling Links
Links can be styled in different states: normal, hover, active, and visited.
Example:
<a href="#">Click Me</a>
<style>
a {
color: blue;
text-decoration: none;
}
a:hover {
color: red;
text-decoration: underline;
}
</style>
Example Description:
CSS Positioning
The position property controls how elements are placed in the document: static, relative, absolute, fixed, or sticky.
Example:
<div class="parent">
<div class="child">I'm positioned</div>
</div>
<style>
.parent {
position: relative;
}
.child {
position: absolute;
top: 10px;
left: 20px;
background-color: yellow;
}
</style>
Example Description:
Responsive Design with Media Queries
Media queries allow you to apply styles based on screen size or device type.
Example:
<div class="responsive-box">Resize the window</div>
<style>
.responsive-box {
background-color: green;
color: white;
padding: 20px;
}
@media (max-width: 600px) {
.responsive-box {
background-color: red;
}
}
</style>
Example Description:
Transitions and Animations
CSS transitions create smooth changes in styles over time, while animations can control multiple states.
Example:
<button class="btn">Hover Me</button>
<style>
.btn {
background-color: blue;
color: white;
padding: 10px;
transition: background-color 0.3s;
}
.btn:hover {
background-color: darkblue;
}
</style>
Example Description: