- Inline CSS – written inside HTML tags.
- Internal CSS – inside
<style>
in the HTML<head>
. - External CSS – in a separate
.css
file (best practice).
Author: Saim Khalid
-
What are the types of CSS?
-
What is CSS?
CSS (Cascading Style Sheets) is used to style and format HTML elements (colors, fonts, layouts, responsiveness)
- Example:
p { color: blue; font-size: 18px; }
-
Image Styling
<!DOCTYPE html> <html> <head> <style>
</style> </head> <body> <img src="https://via.placeholder.com/200" alt="Example"> </body> </html>img { width: 200px; border-radius: 15px; /* Rounded edges */ box-shadow: 4px 4px 10px rgba(0,0,0,0.5); transition: transform 0.3s; } img:hover { transform: scale(1.1); /* Zoom effect */ }
Explanation:
border-radius
: softens corners.box-shadow
: adds shadow.hover
+scale
: zooms image when hovered.
-
CSS Grid Layout
<!DOCTYPE html> <html> <head> <style>
</style> </head> <body> <div class="grid">.grid { display: grid; grid-template-columns: repeat(3, 1fr); /* 3 equal columns */ gap: 10px; } .grid div { background: lightcoral; padding: 20px; text-align: center; }
</div> </body> </html><div>1</div> <div>2</div> <div>3</div> <div>4</div> <div>5</div> <div>6</div>
Explanation:
display: grid;
arranges items into a grid.grid-template-columns: repeat(3, 1fr);
makes 3 equal columns.gap
: space between items.
-
Navigation Menu
<!DOCTYPE html> <html> <head> <style>
</style> </head> <body> <ul>ul { list-style-type: none; background: #333; overflow: hidden; padding: 0; margin: 0; } li { float: left; } li a { display: block; color: white; padding: 14px 20px; text-decoration: none; } li a:hover { background: #111; }
</ul> </body> </html><li><a href="#">Home</a></li> <li><a href="#">About</a></li> <li><a href="#">Services</a></li> <li><a href="#">Contact</a></li>
Explanation:
- A horizontal navbar using
ul
+li
. hover
effect changes background color when mouse is over.
- A horizontal navbar using
-
Rounded Corners
<!DOCTYPE html> <html> <head> <style>
</style> </head> <body> <div class="circle">Circle</div> </body> </html>.circle { width: 150px; height: 150px; background: orange; border-radius: 50%; /* Makes perfect circle */ text-align: center; line-height: 150px; color: white; font-weight: bold; }
Explanation:
border-radius: 50%
creates a circle.- Smaller values like
10px
just make rounded corners.
-
Text Shadow & Box Shadow
<!DOCTYPE html> <html> <head> <style>
</style> </head> <body> <h1>Text Shadow Example</h1> <div class="box">Box Shadow</div> </body> </html>h1 { text-shadow: 2px 2px 5px gray; /* X, Y, blur, color */ } .box { width: 200px; height: 100px; background: lightgreen; box-shadow: 5px 5px 15px rgba(0,0,0,0.3); text-align: center; line-height: 100px; }
Explanation:
text-shadow
: adds a glowing or shadow effect to text.box-shadow
: adds shadow around elements.
-
CSS Gradient Background
<!DOCTYPE html> <html> <head> <style>
</style> </head> <body> <div class="gradient">Gradient Background</div> </body> </html>.gradient { width: 300px; height: 150px; background: linear-gradient(to right, red, yellow); text-align: center; line-height: 150px; font-weight: bold; color: white; }
Explanation:
linear-gradient(to right, red, yellow)
creates a smooth color transition.- You can also use
to bottom
,to left
,to top right
, etc.
-
Responsive Design
<!DOCTYPE html> <html> <head> <style>
</style> </head> <body> <h2>Resize the browser window</h2> </body> </html>body { background: lightblue; } @media (max-width: 600px) { body { background: pink; /* Changes background on small screens */ } }
Explanation:
- Media queries let you apply styles for different screen sizes (used in responsive design).
-
Flexbox Layout
<!DOCTYPE html> <html> <head> <style>
</style> </head> <body> <div class="container">.container { display: flex; gap: 20px; background: lightgray; padding: 20px; } .item { background: skyblue; padding: 30px; flex: 1; /* Equal width items */ text-align: center; }
</div> </body> </html><div class="item">Box 1</div> <div class="item">Box 2</div> <div class="item">Box 3</div>
Explanation:
display: flex;
arranges items horizontally.gap
: spacing between items.flex: 1;
makes items share equal space.