CSS is what makes websites beautiful, organized, and responsive. It controls how HTML elements look, making web pages visually appealing and user-friendly.
CSS (Cascading Style Sheets) is used to make your website beautiful by styling your HTML elements. Before the styles can work, you must link your CSS to your HTML file.
Inline CSS is written inside an HTML tag using the style attribute.
It only affects that specific element.
Example:
<div style="background-color: lightblue; color: white; padding: 20px;">
<h2>This is an inline styled div</h2>
<p>Inline CSS is written directly inside the tag.</p>
</div>
Internal CSS is written inside the <style> tag in the <head> section of your HTML document.
It’s useful when styling only one page.
Example:
<head>
<style>
div {
background-color: green;
color: white;
padding: 20px;
text-align: center;
}
</style>
</head>
<body>
<div>
<h2>This is an internal CSS example</h2>
<p>Style is written inside the head section.</p>
</div>
</body>
External CSS is written in a separate file (for example, style.css), and then linked to your HTML using the <link> tag.
This method is best for large websites.
Example:
<head>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div>
<h2>This is an external CSS example</h2>
<p>The styles are written in a separate CSS file.</p>
</div>
</body>
In CSS, colors are used to make your web page elements more attractive. You can change the color of text, backgrounds, borders, and more using different color values.
CSS provides different ways to define colors. The most common ones are:
red, blue, green)#ff0000 for red)rgb(255, 0, 0))rgba(255, 0, 0, 0.5) for transparent red)hsl(0, 100%, 50%))
You can use the color property to change text color.
Example:
<h2 style="color: red;">This text is red</h2>
<h2 style="color: #00ff00;">This text is green</h2>
<h2 style="color: rgb(0, 0, 255);">This text is blue</h2>
You can use the background-color property to set the background color of an element.
Example:
<div style="background-color: yellow; padding: 20px;">
<p>This div has a yellow background.</p>
</div>
<div style="background-color: rgb(255, 0, 0); padding: 20px;">
<p>This div has a red background.</p>
</div>
You can also add colors using an external CSS file. This is the preferred method for larger projects.
style.css:
p {
color: blue;
background-color: lightgray;
padding: 10px;
}
HTML:
<p>This paragraph is blue with a light gray background.</p>
color to change text color.background-color to change background color.Borders in CSS are used to add lines around elements such as text, images, or boxes. You can customize the border’s style, width, color, and even make the corners rounded.
To add a simple border, use the border property.
Example:
<div style="border: 2px solid black; padding: 20px;">
<p>This div has a solid black border.</p>
</div>
CSS provides several border styles you can use:
solid — normal linedotted — dotted linedashed — dashed linedouble — double linesgroove — 3D grooved effectridge — 3D ridged effectinset — looks pressed inoutset — looks raisedExample:
<div style="border: 3px dotted blue; padding: 15px;">Dotted border</div>
<div style="border: 3px dashed red; padding: 15px;">Dashed border</div>
<div style="border: 3px double green; padding: 15px;">Double border</div>
You can control each part of the border separately:
border-width — sets how thick the border isborder-color — sets the border colorborder-top, border-right, border-bottom, border-left — control each side individuallyExample:
<div style="border-top: 5px solid red;
border-right: 5px dashed blue;
border-bottom: 5px double green;
border-left: 5px dotted orange;
padding: 20px;">
<p>This div has different borders on each side.</p>
</div>
To make the corners of a border rounded, use the border-radius property.
Example:
<div style="border: 2px solid purple;
border-radius: 15px;
padding: 20px;">
<p>This div has rounded corners.</p>
</div>
You can also define all your border styles in a separate CSS file for cleaner code.
style.css:
.box {
border: 3px solid teal;
border-radius: 10px;
padding: 20px;
}
HTML:
<div class="box">This div is styled using external CSS.</div>
border to add lines around elements.border-radius to create rounded corners.Margins and Padding are two important CSS properties that control **spacing** around elements. They make your web page look neat and organized by giving elements room to “breathe.”
Margin is the space **outside** the border of an element. It pushes elements away from each other.
Example:
<div style="background-color: lightblue; margin: 30px;">
<p>This div has a margin of 30px.</p>
</div>
You can set margin for each side using:
margin-topmargin-rightmargin-bottommargin-left
<div style="background-color: yellow; margin-top: 20px; margin-bottom: 40px;">
<p>Different top and bottom margins.</p>
</div>
Padding is the space **inside** the border of an element. It pushes the content away from the element’s border.
Example:
<div style="background-color: lightgreen; padding: 25px;">
<p>This div has padding of 25px inside it.</p>
</div>
You can also set padding for each side:
padding-toppadding-rightpadding-bottompadding-left
<div style="background-color: pink; padding-top: 10px; padding-bottom: 30px;">
<p>Different padding for top and bottom.</p>
</div>
The main difference is:
Example:
<div style="background-color: lightgray;
border: 2px solid black;
margin: 20px;
padding: 20px;">
<p>This div has both margin and padding.</p>
</div>
You can set all sides at once using shorthand:
margin: 10px; — same margin on all sidesmargin: 10px 20px; — top/bottom 10px, left/right 20pxpadding: 5px 10px 15px 20px; — top, right, bottom, leftExample:
<div style="background-color: orange;
margin: 20px 40px;
padding: 10px 30px;">
<p>This div uses shorthand for margin and padding.</p>
</div>
margin to control space outside an element.padding to control space inside an element.Example:
<p style="text-transform: uppercase;">This text is uppercase.</p>
Backgrounds in CSS are used to make your web page more beautiful by adding colors, images, or gradients behind your content. You can apply backgrounds to any element — a whole page, a section, or even a single box.
The background-color property sets the background color of an element.
You can use color names, HEX, RGB, or HSL values.
Example:
<div style="background-color: lightblue; padding: 20px;">
<p>This div has a light blue background.</p>
</div>
<div style="background-color: #ffcc00; padding: 20px;">
<p>This div uses a HEX color.</p>
</div>
<div style="background-color: rgb(200, 100, 255); padding: 20px;">
<p>This div uses an RGB color.</p>
</div>
The background-image property allows you to add an image as a background.
Example:
<div style="background-image: url('background.jpg');
background-repeat: no-repeat;
background-size: cover;
padding: 40px; color: white;">
<p>This div has an image background.</p>
</div>
Explanation:
background-repeat — controls if the image repeats (repeat, no-repeat, repeat-x, repeat-y)background-size — defines how the image fits (cover fills the area, contain fits the image)background-position — sets image position (like center, top, bottom)CSS gradients let you blend two or more colors together. There are two main types: linear and radial gradients.
Example:
<div style="background: linear-gradient(to right, red, yellow);
padding: 30px;">
<p>This background fades from red to yellow.</p>
</div>
Example:
<div style="background: radial-gradient(circle, lightblue, purple);
padding: 30px;">
<p>This background fades in a circular pattern.</p>
</div>
The background-attachment property controls if a background scrolls with the page or stays fixed.
Example:
<div style="background-image: url('bg.jpg');
background-attachment: fixed;
background-size: cover;
color: white;
height: 300px; padding: 20px;">
<p>This background stays fixed when you scroll.</p>
</div>
You can use the background shorthand to set multiple background properties in one line.
<div style="background: url('bg.jpg') no-repeat center/cover;
color: white; padding: 30px;">
<p>This uses shorthand for all background settings.</p>
</div>
background-color — adds color behind contentbackground-image — adds image backgroundsbackground-repeat, background-size, background-position — control how images displaylinear-gradient and radial-gradient — create gradient backgroundsbackground-attachment — fixes background when scrollingbackground — shorthand for all background propertiesThe display property in CSS controls how an element is shown (displayed) on a web page. It determines whether an element appears as a block, inline, inline-block, or something else.
Here’s how the display property changes how elements appear:
<div class="box1">Block Element</div>
<span class="box2">Inline Element</span>
<span class="box3">Inline-Block Element</span>
CSS:
div.box1 {
display: block;
background-color: lightgreen;
padding: 10px;
margin-bottom: 10px;
}
span.box2 {
display: inline;
background-color: orange;
padding: 10px;
}
span.box3 {
display: inline-block;
background-color: lightblue;
padding: 10px;
width: 150px;
height: 50px;
}
display: none<div class="hidden-box">This text is hidden</div>
.hidden-box {
display: none;
}
When you use display: none;, the element disappears completely from the page.
Unlike visibility: hidden;, it doesn’t even take up space.
| Value | Description |
|---|---|
block |
Takes full width, starts on a new line |
inline |
Stays in line with other elements |
inline-block |
In line, but you can set width and height |
none |
Completely hides the element |
The position property in CSS controls how an element is placed (positioned)
on a web page. It allows you to move, fix, or stick elements in specific locations using
top, right, bottom, and left.
<div class="box static">Static Box</div>
.static {
position: static;
background-color: lightgray;
padding: 15px;
margin: 10px 0;
}
Explanation: The box stays where it naturally appears on the page. This is the default.
<div class="box relative">Relative Box</div>
.relative {
position: relative;
top: 20px;
left: 30px;
background-color: skyblue;
padding: 15px;
}
Explanation: The box moves 20px down and 30px right
from its normal position, but still keeps its original space.
<div class="parent">
<div class="box absolute">Absolute Box</div>
</div>
.parent {
position: relative;
background-color: #eee;
height: 150px;
}
.absolute {
position: absolute;
top: 20px;
right: 20px;
background-color: orange;
padding: 15px;
}
Explanation: The absolute box is placed inside the parent using exact coordinates.
<div class="box fixed">Fixed Box</div>
.fixed {
position: fixed;
bottom: 10px;
right: 10px;
background-color: red;
color: white;
padding: 10px;
}
Explanation: This box stays in the bottom-right corner even when the page is scrolled.
<div class="box sticky">Sticky Box</div>
.sticky {
position: sticky;
top: 0;
background-color: green;
color: white;
padding: 10px;
}
Explanation: This box behaves like relative until you scroll; then it sticks to the top of the page.
| Value | Description |
|---|---|
static |
Default position; element stays in normal flow |
relative |
Moves element relative to its normal spot |
absolute |
Positioned based on nearest parent with position |
fixed |
Stays in place even when scrolling |
sticky |
Sticks to top when scrolled into view |