Style Your Page With CSS
Style Your Page With CSS
Content
Hello and welcome to this guided project on CSS! Today, we’re taking a big step
into the world of styling by creating a personal webpage from scratch.
Have you ever visited a beautifully designed website and wondered, How did they
make it look so polished and professional? All those layouts, colors,
animations—it feels like magic! But here’s the cool part: you can see exactly how
it’s done.
Let me show you something interesting. Open up any webpage (say amazon.com)
right-click, and select Inspect (or use F12 on your keyboard). Then, find the
<head> section and remove it by clicking on the backspace key. Instantly, you’ll
notice the page changes dramatically—it loses all its styling, layout, and colors,
leaving just the plain structure of text and images.
This is because CSS (Cascading Style Sheets) is what brings a webpage to life.
It’s the design layer that transforms bare HTML into something beautiful and
user-friendly. Without CSS, a web page is just a skeleton, like a book with only
black and white text and no images, fonts, or style.
In this project, you’re going to learn exactly how CSS works and how to use it to
create your own polished, responsive, and interactive webpage. By the end, you’ll
have the skills to style and customize any webpage—just like the ones you admire!
So let’s dive in and unlock the power of CSS together!
Imagine a house built with just walls, floors, and a roof—it serves its purpose, but
it’s not inviting or visually appealing. Now, imagine that same house with beautiful
paint, furniture, decorations, and lighting—it’s much more welcoming and
impressive.
That’s the role of CSS in web design. HTML provides the structure (the walls and
floors), and CSS adds the style (the decorations and lighting). CSS is what makes
websites visually engaging and user-friendly. It allows you to create layouts, set
colors, choose fonts, and even add animations, ensuring a seamless and attractive
user experience.
Without CSS, the internet as we know it would look plain and uninspiring. CSS
turns ideas into designs and designs into experiences.
CSS works by using selectors, properties, and values to target and style HTML
elements. Let’s break this down:
● Selectors: These specify which HTML elements you want to style. For
example, a <p> selector targets all paragraph tags.
● Properties: These define what aspect of the element you want to style, like
color, font-size, or margin.
● Values: These specify how the property should be styled, like making the
color blue or the font size 16px.
Syntax:
selector {
property: value;
}
Here’s an example:
p{
color: blue;
font-size: 16px;
text-align: center;
This CSS block selects all <p> elements and styles them to have blue text, a font
size of 16px, and centered alignment.
Alright, so now we know how to write a CSS code for an element. But how do we
actually get CSS to work on a webpage? Well, there are three main ways to include
CSS, and each has its own perks.
1. Inline CSS
As the name suggests inline that means in between the code line, Imagine you want
to make a quick change to a single element, like turning a paragraph blue. That’s
where inline CSS comes in handy. You add styles directly into the HTML element
using the style attribute.
Quick, right? But here’s the catch: it’s not very efficient for larger projects. If you
style every element this way, your HTML will become a cluttered mess. Inline
CSS is great for one-off tweaks but not for managing entire websites.
2. Internal CSS
Let’s say you’re working on a single-page project, and you want to keep all your
styles in one place. That’s where internal CSS comes in. You write your CSS rules
inside a <style> tag in the <head> section of your HTML file.
Here’s an example:
<head>
<style>
p{
color: green;
font-size: 18px;
</style>
</head>
This is neat and keeps everything in one file. But imagine your website grows to
multiple pages—managing styles across all those pages with internal CSS would
become a nightmare!
3. External CSS
Now for the big one: external CSS. This is the superhero of CSS inclusion,
especially for larger projects. You write all your styles in a separate .css file and
then link it to your HTML file using a <link> tag in the <head>.
<head>
</head>
body {
background-color: #f4f4f4;
color: #333;
}
External CSS keeps your HTML clean and makes it easy to reuse styles across
multiple pages. Plus, if you need to update a style, you only have to do it in one
place. This is the method we’ll use in this project because it’s scalable, organized,
and professional.
Since we’re using our Compiler, a lot of the setup has already been taken care of
for us. We’re ready to jump straight into styling and experimenting. Here’s how we
can get everything organized and ready to go:
You can see the files name index.html and styles.css are already there
You don’t need to worry about creating new files or organizing them manually,
which is pretty handy.
Now, we need to link our styles.css, this can be done using <link> tag in our header
section.
<head>
</head>
<link> Tag:
The <link> tag is used in the <head> section of an HTML document to link
external resources like CSS stylesheets, icons, or fonts to the HTML page.
rel="stylesheet":
The rel attribute specifies the relationship between the HTML document and the
linked file. In this case, rel="stylesheet" tells the browser that the linked file is a
CSS (Cascading Style Sheets) file, which contains styles for the web page.
href="styles.css":
The href attribute specifies the location of the external file. styles.css is the name of
the CSS file we want to link to. This tells the browser, "Look for a file called
styles.css in the same folder as this HTML file.
To get started, let’s create a basic template of HTML in which we can do our
styling.
Sample code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<</body>
</html>
This is our basic boilerplate, in the next video we will learn how to style it using
CSS.
Now, let’s dive into the CSS starting with our typographies,Typography and colors
are the soul of any webpage design. They set the tone, convey the brand’s
personality, and make your content easier to read and visually appealing. Let’s
learn about them one by one:
Font-family: Fonts are everywhere—serif fonts like Times New Roman give a
formal look, while sans-serif fonts like Arial look modern and clean. The
font-family property lets us choose how the text appears on a webpage.
For example:
body
{ font-family: Arial, sans-serif;
This means the entire web page will use Arial font for the web-page, and in case
the Aerial isn’t available the browser will move to generic sans-serif font. Always
include the fallback options so that your web page looks good in all the browsers.
The size of your text can enhance readability and emphasize hierarchy. Use the
font-size property to set text sizes in pixels, em, rem, or percentages.
For example:
h1 {
Font-size: 30px;
p{
font-size:16px;
Relative Units like em or rem are great for making your site responsive as they
adjust according to the screen sizes:
The font-weight property allows you to adjust the boldness of text, creating visual
contrast between headings, subheadings, and regular text.
h1 {
Font-size: 30px;
font-weight:600/bold
}
p{
Font-size:16px;
font-weight:400;
Line-height
The line-height property controls the spacing between lines of text. Proper spacing
makes your content more readable.
For example:
p{
Font-size:16px;
Font-weight:400;
This is the basic solace between the lines you can increase or decrease it according
to your convenience, and you can also use pixels to specify them
For example:
p{
Why It Matters: Cramped text feels congested, while too much space looks
disconnected. A balanced line-height makes your content visually appealing.
Text-alignment
For example:
h1 {
text-align:center;
p{
text-align:justify;
Vertical-alignment
The vertical-align property is used to align the text vertically at top, bottom,
baseline, middle, it is mostly used when we are creating tables
For example:
td {
vertical-align:middle;
<table>
</table>
The background-color and color properties work together to make your text
visually striking.
Background-color: It gives color to the background of the specified element.
h1 {
Font-size: 30px;
font-weight:600;
Background-color:grey;
color:black;
p{
Font-size:16px;
Font-weight:400;
Background-color:black;
color:white;
Text Transform
The text-transform property controls the capitalization of text. It’s great for
headings, labels, or any text that needs uniform styling. Here’s how it works:
text-transform: uppercase;
This property is perfect for ensuring consistent text styling across your webpage.
Text Decoration
The text-decoration property lets you add or remove lines around text for emphasis
or stylistic purposes. It’s commonly used for links or special content.
text-decoration: underline;
text-decoration: line-through;
text-decoration: none;
text-decoration: overline;
}
Color is an essential part of web design, as it conveys mood, brand identity, and
usability. CSS provides several ways to define and use colors. Let’s break them
down in detail.
1. Named Colors
CSS supports a list of predefined color names, making it easy to apply common
colors without remembering codes.
Examples:
h1 {
color: red;
Named colors are convenient but limited in variety. For precise control, we use
other formats.
Hex codes are a popular way to represent colors in CSS. They start with a # and
consist of six characters:
Example:
h1 {
3. RGB Colors
The RGB color model specifies the intensity of Red, Green, and Blue in a range
from 0 to 255.
Syntax:
Example:
p{
Use RGB for intuitive control over the color balance. For instance:
● rgb(0, 0, 0) → Black
● rgb(255, 255, 255) → White
● rgb(255, 0, 0) → Red
4. RGBA Colors
The A in RGBA stands for alpha, which controls transparency. The alpha value
ranges from 0 (completely transparent) to 1 (fully opaque).
Syntax:
Example:
div {
RGBA is excellent for overlay effects or when you want colors to blend with their
background.
So, we know how to enhance the appearance of our text and apply colors. In the
next Video, we’ll explore The Box Model to structure and organize your web page
elements, ensuring everything looks clean and professional.
Imagine each element on a webpage as a box, whether it’s visible or not. This box
has multiple layers that define how the element interacts with its surroundings. The
CSS Box Model is the backbone of how web layouts work. It’s made up of the
following four parts:
1. Content: The actual content inside the element, like text, an image, or a
button.
2. Padding: Space between the content and the border, like a cushion around
the content.
3. Border: A visible line (or frame) that surrounds the content and padding.
4. Margin: The outermost layer that creates space between this box and other
elements.
To understand this box model in more better way let’s start by making an box:
<div class="box">
</div>
.box {
width: 200px;
height: 100px;
background-color: lightblue;
This will create a simple box of specified height and width, now we need to add
some padding, margins and borders to make this box look better.
Adding Padding
Let’s give the content some breathing room by adding padding.
.box {
width: 200px;
height: 100px;
background-color: lightblue;
padding: 20px;
This will create a specified space between the content and edge of the box.
Imagine it as wrapping a gift: the gift is your content, and the padding is the bubble
wrap protecting it.
Adding a Border
.box {
width: 200px;
height: 100px;
background-color: lightblue;
padding: 20px;
Adding Margins
Finally, let’s add some margin to create space between this box and other elements.
.box {
width: 200px;
height: 100px;
background-color: lightblue;
padding: 20px;
margin: 20px;
This margin creates 20 pixels of empty space outside the box which prevents the
box from overlapping with other elements, ensuring clean spacing.
The margins can padding can be included in paragraphs and headings too, For
example:
p{
padding-top:20px;
padding:left:30px;
padding-right:10px;
padding-bottom:40px;
Instead of writing padding for each direction separately we can include it within a
line For example:
Syntax:
Example
Margins and padding play a crucial role in making our web page neat and
presentable.
Let’s understand the box model better with a practical example. By combining the
properties we learnt above, we can create well-structured and visually appealing
layouts.
Example: let’s create a visually appealing card with an image and some text in it.
<div class="card">
<h2>Serene Landscape</h2>
</div>
This is looking very plain and boring so let’s add CSS to create a magic.
CSS
.card {
width: 300px;
background-color: #f9f9f9;
padding: 20px;
text-align: center;
border-radius: 10px;
.card img {
width: 100%;
border-radius: 8px;
margin-bottom: 15px;
.card h2 {
margin: 10px 0;
color: #333;
.card p {
margin: 10px 0;
color: #666;
line-height: 1.6;
As you can see we have created a proper card which has an image and the content
in it.
With just a few lines of CSS, we’ve transformed a plain structure into a visually
appealing card. The soft shadows, rounded corners, and carefully chosen spacing
make the card look polished and professional.
This example demonstrates the power of the box model and CSS styling in
real-world applications. Whether you’re designing individual elements or creating
a full webpage, mastering these foundational properties allows you to build clean,
organized, and visually stunning layouts.
Next, we’ll take your layout skills to the next level with Flexbox and Grid, two
essential tools for creating responsive and dynamic designs. Let’s dive in and
unlock their potential!
Welcome back! In this video, we’ll dive into the fascinating world of Flexbox and
Grid, two incredibly powerful tools in CSS for designing responsive and dynamic
layouts. But before we jump into these advanced techniques, it’s crucial to
understand a key foundational concept: the display property.
The display property defines how an element is visually laid out on the screen.
Whether an element behaves as a block or inline element has a huge impact on
how your layout takes shape. Let’s break it down!
Block element
Block elements take up the full width of their container and always start on a new
line.
Example:
<h4> I am a block element </h4>
As you can see the output shows that each element takes up the full width unless
stated otherwise.
Inline elements
Inline elements only take up as much width as their content and stay "inline" with
other elements.
Example:
You can observe in this output that the span element is only taking the space as that
of their content.
We can also specify in the css if we want the element to be displayed as inline or
block.
For Example:
.block-item {
display: block;
.inline-item
display: inline;
Understanding how the display property works is the foundation for mastering
advanced layout techniques like Flexbox and Grid. These tools build on these
concepts to provide even more control over element alignment and positioning.
4.2 Flexbox: The Flexible Layout
Flexbox (short for Flexible Box) is a layout model that makes it easy to create
one-dimensional layouts, where elements can be aligned, spaced, and distributed
evenly within a container. It’s great when you need to build layouts that adapt to
different screen sizes without too much hassle.
To start using Flexbox, you need to define a flex container, which will hold the
flex items (the elements inside the container). This is done by setting the
container’s display property to flex. Once you do that, all the direct children of the
container automatically become flex items.
Flexbox items are direct children of flexbox containers. These items can be
arranged vertically and horizontally inside flexbox containers based on need.
.container {
display: flex;
Flex Properties
There are some properties of flex to make them look presentable, before we need
to know about axes. There are two axis element in the flexbox:
1.Main Axis: Main axis is the primary axis along which items are arranged in
a container. By default this is the horizontal axis.
2.Cross Axis: Cross axis is perpendicular axis to primary axis. By default this
is the vertical axis.
3. flex-direction: Defines the direction of the main axis (row or column).
● row: Default value, lays out items horizontally.
● column: Lays out items vertically.
● row-reverse: Lays out items horizontally in reverse order.
● column-reverse: Lays out items vertically in reverse order.
.container {
display: flex;
justify-content:center;
align-items:flex-start;
flex-direction: column;
<html lang="en">
<head>
<style>
.container {
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
height: 100vh;
.item {
padding: 20px;
margin: 10px;
</style>
</head>
<body>
<div class="container">
</div>
</body>
</html>
This will create three boxes in a row with the specified property, like this we can
use flexbox to make dynamic cards, image galleries and many more.
To create a grid layout, you first need to define a grid container. This is done by
setting the container's display property to grid or inline-grid.
.grid-container {
display: grid;
Once the container is set to grid, its children become grid items, and you can use
grid properties to control their placement.
Grid Columns
● Fixed Column Widths: You can specify exact sizes like 200px or
percentages like 30%.
● Flexible Columns: Use 1fr (fractional unit) to divide space dynamically.
For example:
.grid-container {
display: grid;
Grid Rows
.grid-container {
display: grid;
grid-template-rows: 100px auto; (auto means second row will take remaining
space)
Repeat Function
Instead of repeating the same value, you can use the repeat() function for cleaner
code.
.grid-container {
Grid Gaps
You can add gaps (spacing) between rows and columns using grid-gap, row-gap, or
column-gap.
.grid-container {
display: grid;
grid-gap: 20px;
Grid items can be placed in specific cells using grid-column and grid-row
properties.
.grid-item {
For more readable and semantic layouts, you can name grid areas using the
grid-template-areas property.
.grid-container {
display: grid;
grid-template-areas:
.header {
grid-area: header;
.sidebar {
grid-area: sidebar;
.content {
grid-area: content;
}
.footer {
grid-area: footer;
The words (header, sidebar, content, footer) are grid area names:
● "header header header": The first row spans three columns and is
dedicated to the header.
● "sidebar content content": The second row has the sidebar in the
first column and content in the remaining two columns.
● "footer footer footer": The third row spans all three columns for the
footer.
This creates a visually descriptive layout that maps the purpose of each grid cell.
The first and last row (header and footer) will takes the height as that of content
while the 1fr(the sidebar content content) row will takes the remaining space
200px: The first column (sidebar) has a fixed width of 200 pixels.
1fr: The second and third columns share the remaining space equally.
Since, we have created our grid and specified its width and height now we need to
specify its alignment. CSS Grid offers precise alignment options to position items
within a grid, both vertically and horizontally. Let’s explore these alignment
techniques in detail with examples:
The align-items property adjusts the vertical alignment of all grid items within
their respective grid rows. The values define how items are placed relative to the
height of the grid rows.
The justify-items property controls the horizontal alignment of grid items within
their respective columns.
Values
Example
Create a grid containing 3 items
<div class="grid-container">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
</div>
.grid-container {
display: grid;
justify-items: end;
gap: 10px;
.item {
background-color: lightblue;
text-align: center;
In this case, all items will align to the right within their columns and at the center
of the row.
We can also use the shortcut for the vertical and horizontal alignment. The
place-items property is a shorthand for combining align-items and justify-items.
Syntax
Example
.grid-container {
display: grid;
}
This will vertically center items and align them to the right horizontally in each
cell.
So, using these properties of Grid we can create dynamic and responsive layouts
such as image galleries, proper web layouts, portfolio and project display layouts
and much more.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
/* Basic Reset */
*{
margin: 0;
padding: 0;
box-sizing: border-box;
.grid-container {
display: grid;
grid-template-areas:
min-height: 100vh;
gap: 10px;
.header {
grid-area: header;
background-color: #4caf50;
color: white;
padding: 20px;
text-align: center;
.nav {
grid-area: nav;
background-color: #2196f3;
color: white;
padding: 20px;
.sidebar {
grid-area: sidebar;
background-color: #ff9800;
color: white;
padding: 20px;
.content {
grid-area: content;
background-color: #f1f1f1;
color: #333;
padding: 20px;
.footer {
grid-area: footer;
background-color: #333;
color: white;
padding: 20px;
text-align: center;
</style>
</head>
<body>
<div class="grid-container">
<header class="header">Header</header>
<nav class="nav">Navigation</nav>
<aside class="sidebar">Sidebar</aside>
<main class="content">Main Content</main>
<footer class="footer">Footer</footer>
</div>
</body>
</html>
As you can see you have created a blue-print web page layout, now using some
more properties of css you have learned you can also create a well structured
webpage.
We've explored the powerful layout systems of Flexbox and CSS Grid, equipping
you with the tools to create dynamic, responsive designs for modern web pages.
From aligning items with Flexbox to building complex, structured layouts with
Grid, these techniques are essential for professional web development.
Now that you have a strong foundation in layout techniques, let’s move forward to
an equally exciting topic: Positioning Elements!
Welcome back! In this lesson, we dive into one of the most exciting aspects of web
design—positioning elements. While tools like Flexbox and Grid are fantastic for
structuring layouts, the position property gives us granular control to place
elements exactly where we want them. From floating buttons to sticky headers,
let’s explore the possibilities!
The position property in CSS allows you to specify how an element is positioned
within the document or its containing element. There are five key positioning
values:
Static: The default position for elements. They appear in the normal flow of the
document and are unaffected by offset properties (e.g., top, left).
example:
.static-item {
position: static;
}
Relative: The element's original position is according to normal flow of the page
just like static value. But now the left/right/top/bottom/z-index will work.
Example:
.relative-item {
position: relative;
top: 10px;
left: 20px;
The element moves 10px down and 20px right from its original position while still
occupying its original space.
Absolute: The element is completely removed from the document flow. It is then
positioned with respect to its containing block (the container in which it is placed),
and its edges are placed using the side-offset properties. An absolutely positioned
element may overlap other elements, or be overlapped by them.
Example:
.absolute-item {
position: absolute;
top: 0;
right: 0;
The element is pinned to the top-right corner of its container. It no longer affects
the layout of sibling elements.
Fixed: The element's fixed positioning is just like absolute positioning, except the
containing block of a fixed element is always the viewport. Here the element is
totally removed from the document's flow i.e from its container, it is no longer part
of the container or has a position relative to any part of the document.The element
stays in place, even when scrolling.
Example:
.fixed-item {
position: fixed;
bottom: 20px;
right: 20px;
Here the item will be pinneed at the 20px from bottom and 20px from right of the
viewpoint.
The difference between the fixed and sticky position is that in sticky the item
remains within its container while in fixed the item is no longer the part of any
container.
Example:
.sticky-item {
position: sticky;
top: 0;
Here the item will be stuck to the top of the container and won’t move while
scrolling.
The z-index property controls the stacking order of elements, determining which
element appears on top when elements overlap. The higher the z-index value, the
closer the element is to the viewer.
How Z-Index Works:
For z-index to take effect, the element must have a position value other than static
(e.g., relative, absolute, fixed, or sticky)
<div class="container">
</div>
CSS
.box1 {
position: absolute;
width: 100px;
height: 100px;
background-color: red;
z-index: 1;
.box2 {
position: absolute;
width: 100px;
height: 100px;
background-color: blue;
margin-left: -50px;
z-index: 2;
}
Here the boxes will overlap but the box2 will appear above the box1 as it has a
higher. This z-index property is more useful in cases like dropdowns, tooltips
which we will learn further in this Project.
Next, let's learn about the float and clearfix property in CSS. The float property in
CSS is a powerful tool that allows elements to be positioned horizontally, creating
layouts where elements align beside each other. However, floating elements can
disrupt the natural flow of a document, so understanding how to manage them with
techniques like the clearfix is essential.
The float property is used to position elements to the left or right within their
container, allowing inline content (like text) to flow around them.
Syntax:
<div class="container">
</div>
CSS:
.container {
width: 100%;
}
.left-column {
width: 20%;
float: left;
background-color: lightblue;
padding: 10px;
.right-column {
width: 20%;
float: right;
background-color: lightgreen;
padding: 10px;
Here the left column will be placed at the left position while the right column will
be placed at the right position.
1. Collapsed Parent Container: When child elements are floated, their parent
container may "collapse," as it no longer recognizes the floated children.
2. Content Overlap: Floated elements can overlap or behave unpredictably if
not managed properly.
The Problem:
<div class="container">
</div>
CSS
.container {
background-color: lightgray;
.floating-box {
float: left;
width: 100px;
height: 100px;
background-color: lightblue;
Result: The container collapses because it doesn't "see" the floated child.
.container::after {
content: "";
display: table;
clear: both;
Here this will create a pseudo element which will help to maintain the proper flow
of the container and display:table ensures that the pseudo-element is a block
element and ensures that it occupies the full width of the parent while clear: both
This clears the float from both sides (left and right).
This ensures the container recognizes its floated children and adjusts its height
accordingly.
Clearfix helps to prevent problems like container collapse, uneven heights,
overlapping content, inconsistent alignment.
In this topic, we learned the intricacies of positioning elements with the position
property, including static, relative, absolute, fixed, and sticky positioning values.
We also explored the z-index property for controlling stacking order and the float
and clearfix techniques to manage layout flow. With these tools, you can now place
elements with precision and handle layouts more effectively.Now in the next video
we will learn more about CSS interactivity through hover and active effect.
The :hover pseudo-class allows you to define styles for elements when the mouse
is placed over them. This creates a dynamic effect that engages users while the
:active pseudo-class allows you to define styles for elements when the mouse is
clicked over them. These effects are used to make our elements very interactive.
For example we can take a div and give it a hover effect that when we hover over
that div its background color will change. And when we click over it it’s border
color changes
Example:
<div>
</div>
CSS
div {
width:200px;
height:300px;
font-size:14px;
color:black;
div:hover {
background-color:#b43436;
color:white;
div:active {
As you can see in the output that when we hover over the div it changes its
background color to #b43436 which is a shade of red while its text color changes
to white. While when we click over it, the border color changes. Similarly we can
add hover and active effects to any element.
CSS
.button {
background-color: #4CAF50;
color: white;
cursor: pointer;
.button:hover {
.button:active {
background-color: #45a049;
Explanation:
When the user hovers over the button, the border color changes and when clicks it,
the background color changes.
a{
color: #000;
text-decoration: none;
a:hover {
color: #ff6347;
text-decoration: underline;
a:active {
transform: scale(1.1); (This will scale up the link when clicked over it.)
Explanation:
The link changes color and underlines when hovered, while it grows when we click
over it, with a smooth transition to enhance the experience.
position: relative;
overflow: hidden;
.image-container img {
width: 100%;
.image-container:hover img {
transform: scale(1.1); (using this we can scale up or zoom the image to 1.1 scale.
.image-container:active img {
transform: scale(0.95); (using this we can scale down or shrink the image to 0.95
scale.
}
Explanation:
When the user hovers and clicks over the image, it slightly zooms and shrinks in to
give a more interactive feel.
These are some of the simple usage of hover and active effects, we can use hover
effect with any element when with paragraph and heading tag to make it more
interactive.
Now, let’s move forward to see the usage of hover effect. Interactive elements like
dropdowns and tooltips often rely on hover or click events to display hidden
content, adding more functionality to a page.
position: relative;
display: inline-block;
.dropdown-content {
display: none;
position: absolute;
background-color: #f1f1f1;
min-width: 160px;
z-index: 1;
.dropdown:hover .dropdown-content {
display: block;
}
<div class="dropdown">
<div class="dropdown-content">
<p>Hello World!</p>
</div>
</div>
Explanation:
When the user hovers over the .dropdown element, the .dropdown-content
becomes visible, showing the hidden text.
Tooltips on Hover
Tooltips are small pop-up elements that display additional information when the
user hovers over an element.
Example:
.tooltip {
position: relative;
display: inline-block;
.tooltip .tooltiptext {
visibility: hidden;
width: 120px;
background-color: black;
color: #fff;
text-align: center;
border-radius: 5px;
padding: 5px 0;
position: absolute;
z-index: 1;
bottom: 125%;
left: 50%;
margin-left: -60px;
opacity: 0;
.tooltip:hover .tooltiptext {
visibility: visible;
opacity: 1;
</div>
Explanation:
The tooltip becomes visible when the user hovers over the .tooltip element,
displaying extra information about the element.
In the next topic, we’ll dive deeper into animations and transitions, where you’ll
learn how to create smoother, more complex effects for your web projects. Get
ready to take your CSS skills to the next level!
Topic 7: Transitions and Animation
In this topic, we’ll learn how to make web pages come alive using transitions and
animations. These tools help create smooth, attractive effects when elements
change, making websites more interactive and fun to use. Let’s dive into the
details!
A transition makes changes (like color, size, or position) on a web page happen
gradually, instead of instantly. For example, when you hover over a button, its
color can fade smoothly from one shade to another. This creates a more polished
and professional look.
The transition property tells the browser which CSS property to animate, how long
it should take, and how smooth the change should be.
transition-duration: Sets the time the transition takes to complete (in seconds or
milliseconds ms).
transition-delay: Adds a delay before the transition starts.
transition-timing-function: Defines the speed curve of the transition.
Common values:
<button>Hover Me!</button>
css
.button {
background-color: #4CAF50;
color: white;
border: none;
.smooth-button:hover {
background-color: #45a049;
transform: scale(1.1);
In the output you can see that, when we hover over the button its color changes and
also it grows to 1,1 scale with 0.2sdelay. Like this we can add the transition to
many elements of our website to make it look interactive and attractive.
Animations are like a movie for a web element. Instead of just one change,
animations allow you to define multiple stages of movement or appearance. For
example, a box can move up, down, or change color in a repeating cycle.
animation-name
Specifies the name of the keyframes to use for the animation.
Example:
animation-name: bounce;
animation-duration
Defines how long the animation will take to complete one cycle. Use seconds (s) or
milliseconds (ms).
animation-delay
Adds a delay before the animation starts.
animation-iteration-count
Determines how many times the animation will repeat. You can use numbers or
infinite for endless repetition.
Example:
animation-iteration-count: infinite;
animation-direction
Controls the direction of the animation on each cycle. For example:normal,
reverse, alternate, alternate-reverse(alternates but with reverse direction)
animation-timing-function
Defines the speed curve of the animation. Common values:ease, ease-in-out (same
as transition)
animation-fill-mode
Specifies what styles should be applied before and after the animation.
Let’s create a box that looks like it’s bouncing up and down:
html
<div class="ball"></div>
Css
.ball {
width: 50px;
height: 50px;
background-color: red;
border-radius: 50%;
position: relative;
animation-name: bounce;
animation-duration: 2s;
animation-delay: 0.5s;
animation-iteration-count: infinite;
animation-direction: alternate;
animation-timing-function: ease-in-out;
animation-fill-mode: both;
Now, we will define keyframes to make the browser understand how the object
will look at a particular step
@keyframes bounce {
0% {
transform: translateX(0);
50% {
transform: translateX(200px);
background-color: blue;
100% {
transform: translateX(0);
Here when the animation is 0% or 100% complete then the object will be at its
original position but when the animation is at 50% then it will translate (moves up
as X axis changes) 200px.
Now instead of writing too many lines for defining animation we can define it in a
single line too using shorthand property.
For example:
So, in this way you can use animation to create interactive web pages.
Now that you’ve learned the basics, get creative! Try combining transitions and
animations to make your web designs even more exciting. In the next topic, we’ll
explore advanced techniques to make our website responsive and how to use them
in real-world projects.
Topic 8: Responsive Design
Responsive design ensures that your website looks great and works well on all
devices, including desktops, tablets, and smartphones. Let’s learn how to create
responsive layouts using media queries.
Media queries are a CSS technique used to apply styles based on the size and type
of the user’s device. They allow you to create different layouts for different screen
sizes, making your website adaptable and user-friendly.
css
@media (condition) {
For example:
css
body {
font-size: 12px;
Like this we can provide styles for every screen size so that our website looks
responsive.
Conclusion:
Congratulations! You’ve completed the course and now have a solid understanding
of CSS basics, transitions, and animations. Here’s a quick summary of what we’ve
covered:
Now it’s your turn to get creative! Experiment with these concepts to build
engaging, interactive websites. Remember, CSS is all about enhancing user
experience, so keep practicing and exploring new techniques to make your designs
stand out.
Happy Coding!