/* BEM - Block Element Modifier */
/* BLOCK - Independent component */
.card {
padding: 1rem;
border: 1px solid #ddd;
border-radius: 8px;
background: white;
}
/* ELEMENT - Part of block (uses __) */
.card__header {
padding-bottom: 0.5rem;
border-bottom: 1px solid #eee;
}
.card__title {
margin: 0;
font-size: 1.25rem;
font-weight: bold;
}
.card__subtitle {
margin: 0.25rem 0 0;
font-size: 0.875rem;
color: #666;
}
.card__body {
padding: 1rem 0;
}
.card__footer {
padding-top: 0.5rem;
border-top: 1px solid #eee;
display: flex;
justify-content: space-between;
align-items: center;
}
.card__image {
width: 100%;
height: auto;
border-radius: 4px;
}
.card__button {
padding: 0.5rem 1rem;
background: #3b82f6;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
/* MODIFIER - Variation of block/element (uses --) */
.card--featured {
border-color: #3b82f6;
box-shadow: 0 4px 8px rgba(59, 130, 246, 0.2);
}
.card--compact {
padding: 0.5rem;
}
.card--horizontal {
display: flex;
flex-direction: row;
}
.card__title--large {
font-size: 1.75rem;
}
.card__button--secondary {
background: #6b7280;
}
.card__button--disabled {
opacity: 0.5;
cursor: not-allowed;
}
/* More BEM examples */
.navigation {
display: flex;
background: #1f2937;
}
.navigation__list {
display: flex;
list-style: none;
margin: 0;
padding: 0;
}
.navigation__item {
margin: 0;
}
.navigation__link {
display: block;
padding: 1rem;
color: white;
text-decoration: none;
}
.navigation__link--active {
background: #3b82f6;
font-weight: bold;
}
.navigation--vertical .navigation__list {
flex-direction: column;
}
/* Form with BEM */
.form {
max-width: 500px;
}
.form__group {
margin-bottom: 1rem;
}
.form__label {
display: block;
margin-bottom: 0.25rem;
font-weight: 500;
}
.form__input {
width: 100%;
padding: 0.5rem;
border: 1px solid #ddd;
border-radius: 4px;
}
.form__input--error {
border-color: #ef4444;
}
.form__error {
color: #ef4444;
font-size: 0.875rem;
margin-top: 0.25rem;
}
.form__submit {
padding: 0.75rem 1.5rem;
background: #3b82f6;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
.form--inline .form__group {
display: inline-block;
margin-right: 1rem;
}
<!-- Utility-First CSS Approach -->
<!DOCTYPE html>
<html>
<head>
<style>
/* Utility classes - single purpose */
/* Display */
.block { display: block; }
.inline-block { display: inline-block; }
.flex { display: flex; }
.grid { display: grid; }
.hidden { display: none; }
/* Flexbox utilities */
.flex-row { flex-direction: row; }
.flex-col { flex-direction: column; }
.items-center { align-items: center; }
.items-start { align-items: flex-start; }
.justify-center { justify-content: center; }
.justify-between { justify-content: space-between; }
.gap-1 { gap: 0.25rem; }
.gap-2 { gap: 0.5rem; }
.gap-4 { gap: 1rem; }
/* Spacing - Padding */
.p-0 { padding: 0; }
.p-2 { padding: 0.5rem; }
.p-4 { padding: 1rem; }
.p-6 { padding: 1.5rem; }
.px-4 { padding-left: 1rem; padding-right: 1rem; }
.py-2 { padding-top: 0.5rem; padding-bottom: 0.5rem; }
.pt-4 { padding-top: 1rem; }
/* Spacing - Margin */
.m-0 { margin: 0; }
.m-2 { margin: 0.5rem; }
.m-4 { margin: 1rem; }
.mx-auto { margin-left: auto; margin-right: auto; }
.mt-4 { margin-top: 1rem; }
.mb-2 { margin-bottom: 0.5rem; }
/* Width */
.w-full { width: 100%; }
.w-1\/2 { width: 50%; }
.w-1\/3 { width: 33.333%; }
.max-w-lg { max-width: 1024px; }
/* Typography */
.text-center { text-align: center; }
.text-left { text-align: left; }
.text-sm { font-size: 0.875rem; }
.text-base { font-size: 1rem; }
.text-lg { font-size: 1.125rem; }
.text-xl { font-size: 1.25rem; }
.font-bold { font-weight: 700; }
.font-semibold { font-weight: 600; }
.uppercase { text-transform: uppercase; }
/* Colors */
.text-gray-600 { color: #6b7280; }
.text-blue-600 { color: #3b82f6; }
.bg-white { background-color: white; }
.bg-gray-100 { background-color: #f3f4f6; }
.bg-blue-500 { background-color: #3b82f6; }
/* Borders */
.border { border: 1px solid #e5e7eb; }
.border-2 { border: 2px solid; }
.border-blue-500 { border-color: #3b82f6; }
.rounded { border-radius: 0.25rem; }
.rounded-lg { border-radius: 0.5rem; }
.rounded-full { border-radius: 9999px; }
/* Shadows */
.shadow-sm { box-shadow: 0 1px 2px 0 rgba(0,0,0,0.05); }
.shadow { box-shadow: 0 1px 3px 0 rgba(0,0,0,0.1); }
.shadow-lg { box-shadow: 0 10px 15px -3px rgba(0,0,0,0.1); }
/* Interactive */
.cursor-pointer { cursor: pointer; }
.hover\:bg-blue-600:hover { background-color: #2563eb; }
.hover\:shadow-lg:hover {
box-shadow: 0 10px 15px -3px rgba(0,0,0,0.1);
}
/* Responsive utilities */
@media (min-width: 768px) {
.md\:flex { display: flex; }
.md\:w-1\/2 { width: 50%; }
.md\:p-8 { padding: 2rem; }
}
@media (min-width: 1024px) {
.lg\:grid-cols-3 { grid-template-columns: repeat(3, 1fr); }
}
</style>
</head>
<body class="bg-gray-100">
<!-- Card using utility classes -->
<div class="max-w-lg mx-auto mt-4 p-4">
<div class="bg-white rounded-lg shadow p-6">
<h2 class="text-xl font-bold mb-2">Card Title</h2>
<p class="text-gray-600 mb-4">
This card is built entirely with utility classes.
</p>
<div class="flex gap-2">
<button class="bg-blue-500 text-white px-4 py-2 rounded hover:bg-blue-600 cursor-pointer">
Primary
</button>
<button class="border border-blue-500 text-blue-600 px-4 py-2 rounded hover:bg-blue-50 cursor-pointer">
Secondary
</button>
</div>
</div>
</div>
<!-- Grid layout with utilities -->
<div class="max-w-lg mx-auto mt-4 p-4 grid gap-4 md:grid-cols-2 lg:grid-cols-3">
<div class="bg-white p-4 rounded shadow-sm hover:shadow-lg">
<h3 class="font-semibold mb-2">Item 1</h3>
<p class="text-sm text-gray-600">Description</p>
</div>
<div class="bg-white p-4 rounded shadow-sm hover:shadow-lg">
<h3 class="font-semibold mb-2">Item 2</h3>
<p class="text-sm text-gray-600">Description</p>
</div>
<div class="bg-white p-4 rounded shadow-sm hover:shadow-lg">
<h3 class="font-semibold mb-2">Item 3</h3>
<p class="text-sm text-gray-600">Description</p>
</div>
</div>
<!-- Flexbox layout -->
<div class="flex items-center justify-between p-4 bg-white border-2 border-blue-500 rounded-lg mx-auto max-w-lg mt-4">
<div class="flex items-center gap-4">
<div class="w-12 h-12 bg-blue-500 rounded-full"></div>
<div>
<h4 class="font-bold">Username</h4>
<p class="text-sm text-gray-600">Role</p>
</div>
</div>
<button class="px-4 py-2 bg-blue-500 text-white rounded hover:bg-blue-600">
Follow
</button>
</div>
</body>
</html>
BEM (Block Element Modifier) naming uses .block__element--modifier pattern for scalable CSS. I define blocks as independent components like .card. Elements within blocks use .card__title and .card__body. Modifiers indicate variations with .card--featured or .card__title--large. BEM prevents specificity conflicts and naming collisions. Utility-first CSS uses single-purpose classes like .flex, .p-4, .text-center. I compose utilities for rapid development without writing custom CSS. Frameworks like Tailwind CSS popularized utility-first methodology. The SMACSS methodology organizes CSS into Base, Layout, Module, State, and Theme categories. ITCSS (Inverted Triangle CSS) structures from generic to specific. Understanding architecture patterns improves large-scale CSS maintainability.