Advanced CSS Properties and Techniques
CSS Custom Properties (Variables)
Useful for theming and maintaining consistent design values across components.
:root {
--primary-color: #6c5ce7;
--border-radius: 12px;
--shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
}
.card {
background-color: var(--primary-color);
border-radius: var(--border-radius);
box-shadow: var(--shadow);
}
Variables can be overridden inside media queries or themes:
.dark {
--primary-color: #1e1e2f;
}
Advanced CSS Grid Layout
Responsive auto-fitting layout:
.container {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 24px;
}
Grid template areas for complex layouts:
.layout {
display: grid;
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
grid-template-columns: 250px 1fr;
}
.header { grid-area: header; }
.sidebar { grid-area: sidebar; }
.main { grid-area: main; }
.footer { grid-area: footer; }
Advanced Flexbox Patterns
Equal-height cards with sticky footer:
.card {
display: flex;
flex-direction: column;
}
.card-body {
flex: 1;
}
Perfect centering:
.center {
display: flex;
justify-content: center;
align-items: center;
}
Fluid Typography with clamp()
h1 {
font-size: clamp(1.5rem, 4vw, 3rem);
}
This sets:
Minimum size
Fluid scaling
Maximum size
Aspect Ratio
.media {
aspect-ratio: 16 / 9;
overflow: hidden;
}
Removes the need for padding hacks.
Object Fit for Media Control
.media img {
width: 100%;
height: 100%;
object-fit: cover;
}
Maintains image proportions while filling containers.
Scroll Snap
.slider {
display: flex;
overflow-x: auto;
scroll-snap-type: x mandatory;
}
.slide {
flex: 0 0 100%;
scroll-snap-align: start;
}
Creates native slider-like behavior.
Advanced Selectors
Attribute selector:
input[type="email"] {
border: 1px solid #ccc;
}
Pseudo-class selector:
li:nth-child(odd) {
background-color: #f5f5f5;
}
PHP Card Rendering
Rendering Cards from an Array
<?php
$cards = [
[
"title" => "Web Development",
"desc" => "Learn HTML, CSS, JS",
"image" => "web.jpg"
],
[
"title" => "PHP Backend",
"desc" => "Server-side programming",
"image" => "php.jpg"
]
];
?>
<div class="container">
<?php foreach ($cards as $card): ?>
<div class="card">
<img src="<?= $card['image']; ?>" alt="">
<h3><?= $card['title']; ?></h3>
<p><?= $card['desc']; ?></p>
</div>
<?php endforeach; ?>
</div>
Secure Rendering with htmlspecialchars
<?php foreach ($cards as $card): ?>
<div class="card">
<img src="<?= htmlspecialchars($card['image']); ?>" alt="">
<h3><?= htmlspecialchars($card['title']); ?></h3>
<p><?= htmlspecialchars($card['desc']); ?></p>
</div>
<?php endforeach; ?>
Prevents XSS by escaping dynamic content.
Rendering Cards from Database Using PDO
<?php
$pdo = new PDO("mysql:host=localhost;dbname=test", "root", "");
$stmt = $pdo->query("SELECT title, description, image FROM courses");
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo "
<div class='card'>
<img src='".htmlspecialchars($row['image'])."' alt=''>
<h3>".htmlspecialchars($row['title'])."</h3>
<p>".htmlspecialchars($row['description'])."</p>
</div>
";
}
?>
Prepared statement example:
$stmt = $pdo->prepare("SELECT * FROM courses WHERE category = ?");
$stmt->execute([$category]);
Reusable Card Component Function
<?php
function renderCard($title, $desc, $image) {
$title = htmlspecialchars($title);
$desc = htmlspecialchars($desc);
$image = htmlspecialchars($image);
return "
<div class='card'>
<img src='$image' alt=''>
<h3>$title</h3>
<p>$desc</p>
</div>
";
}
echo renderCard("Advanced CSS", "Modern layout techniques", "css.jpg");
?>
Output Buffering for Clean Component Templates
<?php
function renderCardComponent($card) {
ob_start();
?>
<div class="card">
<img src="<?= htmlspecialchars($card['image']); ?>" alt="">
<h3><?= htmlspecialchars($card['title']); ?></h3>
<p><?= htmlspecialchars($card['desc']); ?></p>
</div>
<?php
return ob_get_clean();
}
?>
Separating Logic and View (Simple MVC Pattern)
Controller:
$cards = fetchCourses();
include 'views/cards.php';
View (views/cards.php):
<?php foreach ($cards as $card): ?>
<div class="card">
<h3><?= htmlspecialchars($card['title']); ?></h3>
</div>
<?php endforeach; ?>