Ultimate CSS Masterclass (Part 2: Examples)
1. Basic CSS Examples
1.1 Inline CSS
<p style="color:red; font-size:20px;">This is inline styled text.</p>
1.2 Internal CSS
<head>
<style>
p {
color: blue;
font-size: 18px;
font-family: Arial, sans-serif;
}
</style>
</head>
<body>
<p>This is a paragraph with internal CSS styling.</p>
</body>
1.3 External CSS
styles.css
body {
background-color: lightyellow;
}
h1 {
color: darkgreen;
text-align: center;
}
index.html
<link rel="stylesheet" href="styles.css"> <h1>Welcome to My Website</h1>
2. Selectors Examples
2.1 Element Selector
p {
color: purple;
}
2.2 Class Selector
.highlight {
background-color: yellow;
font-weight: bold;
}
<p class="highlight">Highlighted paragraph</p>
2.3 ID Selector
#main {
border: 2px solid black;
padding: 10px;
}
<div id="main">Content inside ID container</div>
2.4 Descendant Selector
div p {
color: green;
}
- Targets all
<p>inside<div>
2.5 Child Selector
div > p {
font-size: 18px;
}
- Only targets direct child
<p>
2.6 Pseudo-classes
a:hover {
color: red;
}
p:first-child {
font-weight: bold;
}
3. Colors and Fonts Examples
body {
background-color: #f0f0f0;
color: #333;
font-family: "Arial", sans-serif;
}
h1 {
color: rgb(0, 128, 255);
}
p {
color: rgba(255, 0, 0, 0.7);
font-size: 16px;
line-height: 1.6;
letter-spacing: 1px;
text-align: justify;
}
4. Box Model Examples
div {
width: 200px;
height: 100px;
padding: 10px;
border: 3px solid black;
margin: 20px;
box-sizing: border-box;
}
- The box includes padding and border inside the total width
5. Background and Border Examples
div {
background-color: lightblue;
background-image: url('bg.jpg');
background-size: cover;
background-position: center;
border: 2px solid #333;
border-radius: 10px;
padding: 20px;
}
6. Layout Examples
6.1 Display
span {
display: inline;
}
div {
display: block;
}
div.inline-block {
display: inline-block;
width: 150px;
height: 100px;
border: 1px solid black;
}
6.2 Position
.relative {
position: relative;
top: 10px;
left: 20px;
}
.absolute {
position: absolute;
top: 50px;
left: 100px;
}
.fixed {
position: fixed;
bottom: 0;
right: 0;
background-color: red;
color: white;
padding: 10px;
}
6.3 Flexbox
.container {
display: flex;
justify-content: space-around;
align-items: center;
height: 150px;
background-color: lightgray;
}
.box {
width: 50px;
height: 50px;
background-color: blue;
}
<div class="container">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
</div>
6.4 Grid
.grid-container {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-gap: 10px;
}
.grid-item {
background-color: coral;
padding: 20px;
text-align: center;
}
<div class="grid-container">
<div class="grid-item">1</div>
<div class="grid-item">2</div>
<div class="grid-item">3</div>
<div class="grid-item">4</div>
<div class="grid-item">5</div>
<div class="grid-item">6</div>
</div>
7. Lists and Navigation Examples
ul {
list-style-type: none;
padding: 0;
}
ul li {
display: inline-block;
margin-right: 20px;
}
ul li a {
text-decoration: none;
color: darkblue;
}
ul li a:hover {
color: red;
}
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Services</a></li>
<li><a href="#">Contact</a></li>
</ul>
8. Forms Styling Examples
input, select, textarea {
padding: 10px;
margin: 5px 0;
border-radius: 5px;
border: 1px solid gray;
}
input:focus, textarea:focus {
border-color: blue;
outline: none;
}
button {
background-color: green;
color: white;
padding: 10px 20px;
cursor: pointer;
}
button:hover {
background-color: darkgreen;
}
9. Pseudo-elements and Transitions Examples
p::first-letter {
font-size: 2em;
color: red;
}
a::after {
content: " →";
}
button {
background-color: blue;
transition: background-color 0.5s ease, transform 0.3s;
}
button:hover {
background-color: orange;
transform: scale(1.1);
}
10. Animations Examples
@keyframes move {
0% { transform: translateX(0); }
50% { transform: translateX(100px); }
100% { transform: translateX(0); }
}
.box {
width: 50px;
height: 50px;
background-color: red;
animation: move 2s infinite;
}
11. Media Queries and Responsive Design
body {
background-color: white;
}
@media (max-width: 768px) {
body {
background-color: lightgray;
}
nav ul li {
display: block;
margin-bottom: 10px;
}
}
- Adjusts layout and style for tablets and mobile devices
12. Advanced CSS Examples
12.1 CSS Variables
:root {
--main-color: #3498db;
}
h1 {
color: var(--main-color);
}
12.2 Clip-path
img {
clip-path: circle(50%);
}
12.3 Filter
img {
filter: grayscale(100%);
}
12.4 Custom Fonts
@font-face {
font-family: "MyFont";
src: url("MyFont.ttf");
}
p {
font-family: "MyFont", sans-serif;
}