CSS Basics (Single Page)
CSS Basics (Single Page)
CSS selectors are patterns used to select and style HTML elements.
📌 Basic Syntax:
css
selector {
property: value;
}
Example:
css
p {
color: blue;
}
Here:
1. Simple Selectors
h1 {
font-size: 24px;
}
.my-class {
color: red;
}
#unique-id {
background-color: yellow;
}
css
button:hover {
background-color: green;
}
Example:
css
ul li:nth-child(2) {
color: purple;
}
3. Multiple Selectors
css
h1, h2, p {
color: darkblue;
}
References:
CSS selectors help target and style specific HTML elements. They
allow developers to apply styles efficiently and maintain cleaner
code.
tagname {
property: value;
}
✅ Example:
css
p {
font-size: 18px;
color: #333;
}
💡 Best Use: Apply styles when all elements of a specific type should
look the same.
.classname {
property: value;
}
✅ Example:
css
.highlight {
background-color: yellow;
font-weight: bold;
}
🔹 Usage in HTML:
html
#idname {
property: value;
}
✅ Example:
css
#main-heading {
font-size: 24px;
color: blue;
}
🔹 Usage in HTML:
html
<h1 id="main-heading">Welcome</h1>
📌 Syntax:
css
selector:pseudo-class {
property: value;
}
✅ Example:
css
button:hover {
background-color: green;
color: white;
}
📌 Syntax:
css
selector1, selector2 {
property: value;
}
✅ Example:
css
h1, h2, p {
font-family: Arial, sans-serif;
}
🔹 Additional Notes
✔ CSS Specificity: IDs ( ) > Classes (
`#id` ) > Elements`.class`
( ).
✔ Best Practices: Prefer classes over IDs for styling.
`tagname`
📌 References:
🔗 MDN Docs: https://developer.mozilla.org/en-US/docs/Web/CSS
🔗 W3Schools: https://www.w3schools.com/css/
🔗 CSS Tricks: https://css-tricks.com
Let me know if you need further improvements! 🚀
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
✅ Example:
css
* {
font-family: Arial, sans-serif;
}
parent child {
property: value;
}
✅ Example:
css
nav ul li {
list-style: none;
}
💡 Best Use: Ideal for menus, forms, and layouts with structured
elements.
element[attribute="value"] {
property: value;
}
✅ Example:
css
input[type="text"] {
border: 2px solid blue;
}
📌 Syntax:
html
📌 Syntax:
html
<head>
<style>
h1 {
color: blue;
text-align: center;
}
</style>
</head>
✅ Example:
html
html
css
h1 {
color: blue;
font-size: 24px;
}
✅ Example:
html
/project-folder
├── index.html
├── styles.css
html
<head>
<link rel="stylesheet" href="styles.css">
</head>
🔹 Additional Notes
✔ Use External CSS for better scalability.
✔ Avoid Inline Styles unless necessary.
✔ Attribute Selectors are great for styling input fields, links, and
buttons.
✔ Nested Selectors keep code structured and organized.
✔ Universal Selector is useful for CSS resets.
📌 References:
🔗 MDN Docs: https://developer.mozilla.org/en-US/docs/Web/CSS
🔗 W3Schools: https://www.w3schools.com/css/
🔗 CSS Tricks: https://css-tricks.com
Let me know if you need more details! 🚀
📌 Example:
css
✅ HTML:
html
📌 CSS Example:
css
.box {
width: 200px;
height: 100px;
padding: 20px;
border: 5px solid black;
margin: 10px;
}
💡 Best Practice:
✔ Use `box-sizing: border-box;` to include padding and border in
width/height calculations.
css
🔹 2. RGB Colors
📌 Uses `rgb(red, green, blue)`, where values range from 0 to 255.
css
color: red;
color: blue;
color: yellow;
css
css
💡 Best Practice:
✔ Use RGBA or HSLA for transparency effects.
✔ Use Hex or RGB for solid colors.
🔹 Additional Notes
✔ Specificity matters – ID selectors are stronger than classes.
✔ Box Model controls layout – margin, padding, border affect
spacing.
✔ Use Hex, RGB, or HSL for colors – pick based on readability and
flexibility.
📌 References:
🔗 MDN Docs: https://developer.mozilla.org/en-US/docs/Web/CSS
🔗 W3Schools: https://www.w3schools.com/css/
🔗 CSS Tricks: https://css-tricks.com
Let me know if you need more details! 🚀
🎨 Colors in CSS
Colors in CSS can be defined in multiple ways, each with its own
advantages. The most commonly used methods are Hexadecimal,
RGB, and Predefined Color Names.
🔹 Example:
css
3 characters.
css
💡 Best Practice:
✔ Use hex colors for compact and widely supported color
definitions.
✔ Use shorter hex format when possible to reduce file size.
2️⃣ RGB Colors ( `rgb(red, green, blue)`)
📌 RGB values allow you to specify colors using red, green, and
blue intensity levels between and .
📌 The format is:
`0` `255`
.
`rgb(red, green, blue)`
🔹 Example:
css
css
💡 Best Practice:
✔ Use RGB when you need clearer color intensity control.
✔ Percentages are less common but can be useful for dynamic
color changes.
🔹 Example:
css
💡 Best Practice:
✔ Use RGBA for transparency effects like overlays and hover
effects.
✔ Avoid excessive transparency to maintain readability.
4️⃣ Predefined Color Names
📌 CSS has 140+ predefined color names that work across all
browsers.
📌 These names are human-readable and don't require code
interpretation.
🔹 Example:
css
color: red;
color: blue;
color: green;
color: yellow;
color: pink;
color: cyan;
color: magenta;
color: orange;
color: brown;
color: purple;
color: gray;
💡 Best Practice:
✔ Use predefined names for quick styling without remembering
hex codes.
✔ They are easy to read, but less flexible compared to hex/RGB.
5️⃣ HSL Colors ( `hsl(hue, saturation,
lightness)`)
🔹 Example:
css
💡 Best Practice:
✔ Use HSL for better human readability and easy color
adjustments.
✔ Perfect for designers who think in terms of hue, saturation, and
brightness.
🔹 Example:
css
💡 Best Practice:
✔ Use HSLA for transparency effects without modifying
hue/saturation.
✔ Ideal for backgrounds, overlays, and hover effects.
📝 Summary & Best Practices
✔ Use Hex ( ) for compact and widely supported color
`#RRGGBB`
definitions.
✔ Use RGB ( ) when you need precise color intensity
`rgb()`
control.
✔ Use RGBA ( ) for transparency effects.
✔ Use Predefined Colors for quick styling.
`rgba()`
📌 References
🔗 MDN Docs: https://developer.mozilla.org/en-
US/docs/Web/CSS/color
🔗 W3Schools: https://www.w3schools.com/css/css_colors.asp
🔗 CSS Tricks: https://css-tricks.com
Let me know if you need further explanations! 🚀
🔹 Syntax:
css
🔹 Example:
css
p {
font-family: "Times New Roman", Times, serif;
}
💡 Best Practice:
✔ Always use multiple fonts to ensure availability.
✔ Use web-safe fonts for compatibility across devices.
🔗 Web-safe font list:
https://www.w3schools.com/cssref/css_websafe_fonts.php
🔹 Syntax:
css
🔹 Example:
css
h1 {
font-weight: bold;
}
p {
font-weight: 300;
}
✅ Common values:
`100` (Thin)
`300` (Light)
`400` (Normal)
`700` (Bold)
`900` (Extra Bold)
💡 Best Practice:
✔ Use numeric values for better control over weight.
✔ Ensure proper contrast for readability.
3️⃣ Font Style ( `font-style`)
🔹 Example:
css
p {
font-style: italic;
}
💡 Best Practice:
✔ Use italic for emphasis (quotes, citations).
✔ is rarely used—prefer
`oblique` . `italic`
🔹 CSS Equivalent:
css
strong {
font-weight: bold;
}
em {
font-style: italic;
}
✅ SEO Note:
`<b>` and `<i>` are visual only.
`<strong>` and `<em>` add meaning & importance, improving
SEO.
💡 Best Practice:
✔ Use instead of , and instead of `<i>`.
✔ Helps with screen readers & accessibility.
`<strong>` `<b>` `<em>`
html
<head>
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght
</head>
css
body {
font-family: "Roboto", sans-serif;
}
css
@import url('https://fonts.googleapis.com/css2?family=Roboto:wght@
body {
font-family: "Roboto", sans-serif;
}
💡 Best Practice:
✔ Use for better loading speed.
✔ Use multiple font weights for design flexibility.
`<link>`
🔹 Example:
css
@font-face {
font-family: "MyCustomFont";
src: url("fonts/myfont.woff2") format("woff2"),
url("fonts/myfont.woff") format("woff");
}
p {
font-family: "MyCustomFont", sans-serif;
}
✅ Formats:
`.woff2` (Best for modern browsers)
`.woff` (Better compatibility)
`.ttf` (Larger size, use only if necessary)
💡 Best Practice:
✔ Use for best performance.
✔ Always include a fallback font.
`.woff2`
📌 References
🔗 MDN Docs: https://developer.mozilla.org/en-
US/docs/Web/CSS/font
🔗 W3Schools: https://www.w3schools.com/css/css_font.asp
🔗 Google Fonts: https://fonts.google.com
🔗 CSS Tricks: https://css-tricks.com/snippets/css/using-font-face/
Let me know if you need more details! 🚀
🔹 Example:
css
.box {
width: 100px; /* Fixed width */
height: 50mm; /* Fixed height */
}
🔹 Example:
css
.container {
width: 80%; /* 80% of the parent */
height: 50%; /* 50% of the parent */
}
🔹 Example:
css
body {
font-size: 16px;
}
p {
font-size: 2em; /* 32px (16px × 2) */
}
h1 {
font-size: 1.5rem; /* 24px (Root: 16px × 1.5) */
}
❌ Avoid `em` for global styles (it may cause unexpected scaling).
🔹 Example:
css
.fullscreen {
width: 100vw; /* Full screen width */
height: 100vh; /* Full screen height */
}
📌 References
🔗 MDN Docs: https://developer.mozilla.org/en-
US/docs/Web/CSS/length
🔗 W3Schools: https://www.w3schools.com/cssref/css_units.asp
🔗 CSS Tricks: https://css-tricks.com/fun-viewport-units/
Let me know if you need more details! 🚀
To create a card like the one in the image, we will use HTML and CSS.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-sca
<title>Course Card</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="card">
<img src="course-image.jpg" alt="Course Thumbnail" class=
<div class="card-content">
<h3 class="course-title">WEB DEVELOPMENT <span>BOOTCAM
<p class="course-subtitle">Web Development Master Cou
<div class="card-footer">
<span class="price">₹4500</span>
<span class="old-price">₹6999</span>
<span class="discount">42% off</span>
</div>
</div>
</div>
</body>
</html>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Poppins', sans-serif;
}
body {
background-color: #121212;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.card {
width: 300px;
background-color: #fff;
border-radius: 10px;
overflow: hidden;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
transition: transform 0.3s ease-in-out;
}
.card:hover {
transform: scale(1.05);
}
.card-image {
width: 100%;
height: auto;
}
.card-content {
padding: 15px;
text-align: center;
}
.course-title {
font-size: 18px;
font-weight: 700;
background: black;
color: white;
padding: 5px;
border-radius: 5px;
}
.course-title span {
color: yellow;
}
.course-subtitle {
font-size: 14px;
color: #444;
margin-top: 5px;
}
.card-footer {
display: flex;
justify-content: center;
align-items: center;
gap: 10px;
margin-top: 10px;
}
.price {
font-size: 18px;
font-weight: bold;
color: #2ecc71;
}
.old-price {
font-size: 14px;
text-decoration: line-through;
color: #999;
}
.discount {
font-size: 14px;
font-weight: bold;
color: white;
background-color: #2ecc71;
padding: 3px 6px;
border-radius: 4px;
}
badge.
📌 References
🔗 MDN Docs: https://developer.mozilla.org/en-US/docs/Web/CSS
🔗 W3Schools: https://www.w3schools.com/css
🔗 CSS Tricks: https://css-tricks.com/
Let me know if you need improvements! 🚀
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 1/1