Modern CSS has evolved significantly, offering powerful features that make web styling more flexible and maintainable than ever before. Let's explore the most important modern CSS techniques.
.container {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 1rem;
}
.layout {
display: grid;
grid-template-areas:
"header header header"
"sidebar main main"
"footer footer footer";
}
.card-container {
display: flex;
flex-wrap: wrap;
gap: 1rem;
justify-content: center;
}
.card {
flex: 1 1 300px;
max-width: 400px;
}
:root {
--primary-color: #007bff;
--secondary-color: #6c757d;
--spacing-unit: 8px;
}
.button {
background-color: var(--primary-color);
padding: calc(var(--spacing-unit) * 2);
}
/* Direct children */
.container > * {
margin-bottom: 1rem;
}
/* Siblings */
.card + .card {
margin-left: 1rem;
}
/* State-based */
.input:has(:focus) {
border-color: var(--primary-color);
}
.fade-in {
animation: fadeIn 0.3s ease-in;
}
@keyframes fadeIn {
from {
opacity: 0;
transform: translateY(10px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
.card {
container-type: inline-size;
}
@container (min-width: 400px) {
.card-content {
display: grid;
grid-template-columns: 2fr 1fr;
}
}
const Button = styled.button`
background: ${props => props.primary ? 'var(--primary-color)' : 'white'};
color: ${props => props.primary ? 'white' : 'var(--primary-color)'};
padding: 0.5em 1em;
border: 2px solid var(--primary-color);
border-radius: 4px;
`;
.animated-element {
will-change: transform;
transition: transform 0.3s ease;
}
.long-content {
content-visibility: auto;
contain-intrinsic-size: 0 500px;
}
.element {
/* Base styles for mobile */
width: 100%;
/* Tablet and up */
@media (min-width: 768px) {
width: 50%;
}
/* Desktop */
@media (min-width: 1024px) {
width: 33.333%;
}
}
.container {
margin-block: 1rem;
padding-inline: 2rem;
}
Modern CSS provides powerful tools for creating responsive, maintainable, and performant web applications. Understanding and utilizing these features is crucial for modern web development.