C
Bonus: CSS Variables ve Modern Özellikler
Genel Bakış

Bonus: CSS Variables ve Modern Özellikler

15 Haziran 2025
9 dk okuma süresi
css-variables-modern-ozellikler

CSS Variables (Custom Properties)

CSS Variables, dinamik ve yeniden kullanılabilir değerler tanımlamanıza olanak sağlar. Bu özellik ile daha sürdürülebilir ve esnek CSS yazabilirsiniz.

Temel Syntax

/* Tanımlama */
:root {
    --primary-color: #007acc;
    --secondary-color: #6c757d;
    --font-size-base: 1rem;
    --spacing-unit: 1rem;
}
 
/* Kullanım */
.button {
    background-color: var(--primary-color);
    font-size: var(--font-size-base);
    padding: var(--spacing-unit);
}
 
/* Fallback değer */
.element {
    color: var(--text-color, #333);
    /* Eğer --text-color tanımlı değilse #333 kullan */
}

CSS Variables vs Sass Variables

/* CSS Variables - Runtime'da değiştirilebilir */
:root {
    --primary: #007acc;
}
 
.button {
    background: var(--primary);
}
 
/* JavaScript ile değiştirilebilir */
/* document.documentElement.style.setProperty('--primary', '#ff0000'); */
 
/* Sass Variables - Compile time'da sabit */
$primary: #007acc;
 
.button {
    background: $primary; /* Compile sonrası: background: #007acc; */
}

Theming Sistemi

Dark Mode Implementation

:root {
    /* Light theme (default) */
    --bg-primary: #ffffff;
    --bg-secondary: #f8f9fa;
    --text-primary: #212529;
    --text-secondary: #6c757d;
    --border-color: #dee2e6;
    --shadow: rgba(0, 0, 0, 0.1);
}
 
[data-theme="dark"] {
    /* Dark theme */
    --bg-primary: #121212;
    --bg-secondary: #1e1e1e;
    --text-primary: #ffffff;
    --text-secondary: #b3b3b3;
    --border-color: #333333;
    --shadow: rgba(255, 255, 255, 0.1);
}
 
/* Component styles */
body {
    background-color: var(--bg-primary);
    color: var(--text-primary);
    transition: background-color 0.3s, color 0.3s;
}
 
.card {
    background-color: var(--bg-secondary);
    border: 1px solid var(--border-color);
    box-shadow: 0 2px 4px var(--shadow);
    color: var(--text-primary);
}
 
.text-muted {
    color: var(--text-secondary);
}

JavaScript Theme Toggle

<button id="theme-toggle">🌙</button>
 
<script>
const themeToggle = document.getElementById('theme-toggle');
const currentTheme = localStorage.getItem('theme') || 'light';
 
document.documentElement.setAttribute('data-theme', currentTheme);
 
themeToggle.addEventListener('click', () => {
    const newTheme = document.documentElement.getAttribute('data-theme') === 'dark' ? 'light' : 'dark';
    document.documentElement.setAttribute('data-theme', newTheme);
    localStorage.setItem('theme', newTheme);
    themeToggle.textContent = newTheme === 'dark' ? '☀️' : '🌙';
});
</script>

Advanced Color System

:root {
    /* Color palette */
    --blue-50: #e3f2fd;
    --blue-100: #bbdefb;
    --blue-200: #90caf9;
    --blue-300: #64b5f6;
    --blue-400: #42a5f5;
    --blue-500: #2196f3;
    --blue-600: #1e88e5;
    --blue-700: #1976d2;
    --blue-800: #1565c0;
    --blue-900: #0d47a1;
    
    /* Semantic colors */
    --primary: var(--blue-600);
    --primary-light: var(--blue-400);
    --primary-dark: var(--blue-800);
    
    /* State colors */
    --success: #28a745;
    --warning: #ffc107;
    --danger: #dc3545;
    --info: var(--blue-500);
    
    /* Neutral colors */
    --gray-50: #f8f9fa;
    --gray-100: #e9ecef;
    --gray-200: #dee2e6;
    --gray-300: #ced4da;
    --gray-400: #adb5bd;
    --gray-500: #6c757d;
    --gray-600: #495057;
    --gray-700: #343a40;
    --gray-800: #212529;
    --gray-900: #000000;
}
 
/* Dynamic color functions */
.button-primary {
    background-color: var(--primary);
    border-color: var(--primary);
    color: white;
}
 
.button-primary:hover {
    background-color: var(--primary-dark);
    border-color: var(--primary-dark);
}
 
.button-primary:focus {
    box-shadow: 0 0 0 0.2rem rgba(var(--primary-rgb), 0.25);
}

Responsive Variables

Contextual Variables

:root {
    --container-max-width: 1200px;
    --grid-columns: 1;
    --gap: 1rem;
    --font-size-h1: 1.5rem;
}
 
@media (min-width: 768px) {
    :root {
        --grid-columns: 2;
        --gap: 1.5rem;
        --font-size-h1: 2rem;
    }
}
 
@media (min-width: 1024px) {
    :root {
        --grid-columns: 3;
        --gap: 2rem;
        --font-size-h1: 2.5rem;
    }
}
 
.grid {
    display: grid;
    grid-template-columns: repeat(var(--grid-columns), 1fr);
    gap: var(--gap);
}
 
h1 {
    font-size: var(--font-size-h1);
}

Component-Level Variables

.card {
    --card-padding: 1rem;
    --card-radius: 0.5rem;
    --card-shadow: 0 2px 4px rgba(0,0,0,0.1);
    
    padding: var(--card-padding);
    border-radius: var(--card-radius);
    box-shadow: var(--card-shadow);
}
 
.card--large {
    --card-padding: 2rem;
    --card-radius: 1rem;
    --card-shadow: 0 4px 8px rgba(0,0,0,0.15);
}
 
.card--compact {
    --card-padding: 0.5rem;
    --card-radius: 0.25rem;
    --card-shadow: 0 1px 2px rgba(0,0,0,0.05);
}

CSS Calc() ile Dynamic Values

:root {
    --sidebar-width: 250px;
    --header-height: 60px;
    --spacing: 1rem;
}
 
.main-content {
    /* Dynamic width calculation */
    width: calc(100% - var(--sidebar-width));
    /* Dynamic height calculation */
    height: calc(100vh - var(--header-height));
    /* Dynamic padding */
    padding: calc(var(--spacing) * 2);
}
 
/* Responsive calculations */
.responsive-grid {
    --columns: 3;
    --gap: 1rem;
    
    display: grid;
    grid-template-columns: repeat(var(--columns), 1fr);
    gap: var(--gap);
}
 
.grid-item {
    /* Fluid width with gap consideration */
    width: calc((100% - (var(--columns) - 1) * var(--gap)) / var(--columns));
}

Modern CSS Functions

clamp() Function

/* Responsive typography without media queries */
h1 {
    font-size: clamp(1.5rem, 4vw, 3rem);
    /* min: 1.5rem, preferred: 4vw, max: 3rem */
}
 
h2 {
    font-size: clamp(1.25rem, 3vw, 2.5rem);
}
 
/* Responsive spacing */
.section {
    padding: clamp(1rem, 5vw, 3rem);
}
 
/* Responsive container width */
.container {
    width: clamp(320px, 90%, 1200px);
    margin: 0 auto;
}

min() ve max() Functions

/* Responsive width with max constraint */
.sidebar {
    width: min(300px, 100%);
    /* Never wider than 300px or 100% of parent */
}
 
/* Responsive height with min constraint */
.hero {
    height: max(400px, 50vh);
    /* At least 400px or 50% of viewport height */
}
 
/* Combining functions */
.flexible-box {
    width: min(calc(50% - 1rem), 400px);
    height: max(200px, 30vh);
    padding: clamp(0.5rem, 2vw, 2rem);
}

Container Queries (Yakında Gelen Özellik)

/* Container setup */
.card-container {
    container-type: inline-size;
    container-name: card;
}
 
/* Container-based responsive design */
@container card (min-width: 400px) {
    .card {
        display: flex;
        flex-direction: row;
    }
    
    .card-image {
        width: 40%;
    }
    
    .card-content {
        width: 60%;
    }
}
 
@container card (min-width: 600px) {
    .card {
        padding: 2rem;
    }
    
    .card-title {
        font-size: 1.5rem;
    }
}

CSS Logical Properties

/* Traditional properties */
.box-traditional {
    margin-top: 1rem;
    margin-right: 2rem;
    margin-bottom: 1rem;
    margin-left: 2rem;
    border-left: 2px solid blue;
    text-align: left;
}
 
/* Logical properties (international support) */
.box-logical {
    margin-block-start: 1rem;    /* margin-top */
    margin-inline-end: 2rem;     /* margin-right (LTR) or margin-left (RTL) */
    margin-block-end: 1rem;      /* margin-bottom */
    margin-inline-start: 2rem;   /* margin-left (LTR) or margin-right (RTL) */
    border-inline-start: 2px solid blue;
    text-align: start;
}
 
/* Shorthand logical properties */
.box-shorthand {
    margin-block: 1rem;          /* margin-top + margin-bottom */
    margin-inline: 2rem;         /* margin-left + margin-right */
    padding-block: 1rem;
    padding-inline: 1.5rem;
}

CSS Nesting (Yakında Gelen)

/* Traditional CSS */
.card { }
.card .title { }
.card .content { }
.card:hover { }
 
/* CSS Nesting */
.card {
    background: white;
    border-radius: 8px;
    padding: 1rem;
    
    & .title {
        font-size: 1.25rem;
        margin-bottom: 0.5rem;
    }
    
    & .content {
        line-height: 1.6;
        color: var(--text-secondary);
    }
    
    &:hover {
        box-shadow: 0 4px 8px rgba(0,0,0,0.1);
    }
    
    &.featured {
        border-left: 4px solid var(--primary);
    }
    
    @media (min-width: 768px) {
        padding: 1.5rem;
        
        & .title {
            font-size: 1.5rem;
        }
    }
}

CSS Cascade Layers

/* Layer declaration */
@layer reset, base, components, utilities;
 
/* Reset layer */
@layer reset {
    * {
        margin: 0;
        padding: 0;
        box-sizing: border-box;
    }
}
 
/* Base layer */
@layer base {
    body {
        font-family: Arial, sans-serif;
        line-height: 1.6;
    }
    
    h1, h2, h3 {
        line-height: 1.2;
    }
}
 
/* Components layer */
@layer components {
    .button {
        padding: 0.5rem 1rem;
        border: none;
        border-radius: 4px;
        cursor: pointer;
    }
    
    .card {
        background: white;
        border-radius: 8px;
        box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    }
}
 
/* Utilities layer (highest priority) */
@layer utilities {
    .hidden { display: none !important; }
    .text-center { text-align: center !important; }
    .text-red { color: red !important; }
}

CSS Scope (Experimental)

/* Scoped styles */
@scope (.card) to (.card .footer) {
    p {
        color: blue;
        /* Only affects <p> elements inside .card but not inside .card .footer */
    }
}
 
@scope (.component) {
    .title {
        font-size: 1.5rem;
        /* Only affects .title inside .component */
    }
    
    @scope (&.large) {
        .title {
            font-size: 2rem;
            /* Only affects .title inside .component.large */
        }
    }
}

Utility-First Design System

:root {
    /* Spacing scale */
    --space-0: 0;
    --space-1: 0.25rem;
    --space-2: 0.5rem;
    --space-3: 0.75rem;
    --space-4: 1rem;
    --space-5: 1.25rem;
    --space-6: 1.5rem;
    --space-8: 2rem;
    --space-10: 2.5rem;
    --space-12: 3rem;
    --space-16: 4rem;
    --space-20: 5rem;
    
    /* Font size scale */
    --text-xs: 0.75rem;
    --text-sm: 0.875rem;
    --text-base: 1rem;
    --text-lg: 1.125rem;
    --text-xl: 1.25rem;
    --text-2xl: 1.5rem;
    --text-3xl: 1.875rem;
    --text-4xl: 2.25rem;
    --text-5xl: 3rem;
}
 
/* Utility classes */
.p-0 { padding: var(--space-0); }
.p-1 { padding: var(--space-1); }
.p-2 { padding: var(--space-2); }
.p-3 { padding: var(--space-3); }
.p-4 { padding: var(--space-4); }
 
.m-0 { margin: var(--space-0); }
.m-1 { margin: var(--space-1); }
.m-2 { margin: var(--space-2); }
.m-auto { margin: auto; }
 
.text-xs { font-size: var(--text-xs); }
.text-sm { font-size: var(--text-sm); }
.text-base { font-size: var(--text-base); }
.text-lg { font-size: var(--text-lg); }
 
/* Responsive utilities */
@media (min-width: 768px) {
    .md\:p-6 { padding: var(--space-6); }
    .md\:text-xl { font-size: var(--text-xl); }
}

Modern CSS Best Practices

1. Progressive Enhancement

/* Base styles for all browsers */
.grid {
    display: block;
}
 
.grid-item {
    margin-bottom: 1rem;
}
 
/* Enhanced styles for modern browsers */
@supports (display: grid) {
    .grid {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
        gap: 1rem;
    }
    
    .grid-item {
        margin-bottom: 0;
    }
}
 
@supports (container-type: inline-size) {
    .responsive-card {
        container-type: inline-size;
    }
}

2. Custom Media Queries

/* Define custom media queries (future feature) */
@custom-media --small-viewport (max-width: 30em);
@custom-media --large-viewport (min-width: 60em);
 
/* Usage */
@media (--small-viewport) {
    .sidebar {
        display: none;
    }
}
 
@media (--large-viewport) {
    .container {
        max-width: 1200px;
    }
}

3. Environment Variables

/* Safe area insets for mobile devices */
.app {
    padding-top: env(safe-area-inset-top);
    padding-left: env(safe-area-inset-left);
    padding-right: env(safe-area-inset-right);
    padding-bottom: env(safe-area-inset-bottom);
}
 
/* Keyboard height */
.chat-input {
    bottom: env(keyboard-inset-height, 0);
}

CSS Tools ve Workflow

PostCSS Plugins

/* autoprefixer - otomatik vendor prefix */
.box {
    display: flex; /* -webkit-flex eklenir */
    transform: scale(1.1); /* -webkit-transform eklenir */
}
 
/* postcss-custom-properties - CSS variables fallback */
:root {
    --primary: #007acc;
}
 
.button {
    background: var(--primary); /* background: #007acc; eklenir */
}
 
/* postcss-nested - Sass-like nesting */
.card {
    padding: 1rem;
    
    .title {
        font-size: 1.25rem;
    }
    
    &:hover {
        box-shadow: 0 4px 8px rgba(0,0,0,0.1);
    }
}

CSS-in-JS Alternative

/* Styled components approach with CSS */
.button {
    --variant: primary;
    --size: medium;
    
    padding: var(--btn-padding, 0.5rem 1rem);
    background: var(--btn-bg, var(--primary));
    color: var(--btn-color, white);
    border: var(--btn-border, none);
    border-radius: var(--btn-radius, 4px);
}
 
.button[data-variant="secondary"] {
    --btn-bg: transparent;
    --btn-color: var(--primary);
    --btn-border: 1px solid var(--primary);
}
 
.button[data-size="large"] {
    --btn-padding: 0.75rem 1.5rem;
    --btn-radius: 6px;
}
Ipucu

Gelecek İpucu: CSS sürekli gelişiyor. Container Queries, CSS Nesting ve Cascade Layers gibi özellikler yakında tüm tarayıcılarda desteklenecek!

Pratik Egzersizler

  1. Dark mode toggle sistemi oluşturun
  2. Component-based design system tasarlayın
  3. Fluid typography sistemi kurun
  4. CSS-only theme switcher yapın
  5. Modern utility classes oluşturun

CSS’in Geleceği

Yakında Gelen Özellikler

  • Container Queries: Element tabanlı responsive tasarım
  • CSS Nesting: Sass benzeri nesting desteği
  • CSS Scope: Stil kapsamını sınırlama
  • CSS Modules: Native modül desteği
  • CSS Color Level 4: Gelişmiş renk fonksiyonları

Öğrenmeye Devam Edin

  • CSS Working Group: W3C CSS spesifikasyonları
  • Can I Use: Browser desteği kontrolü
  • MDN Web Docs: Kapsamlı CSS dokumentasyonu
  • CSS Tricks: Pratik CSS teknikleri
  • CSS Grid Garden: Eğlenceli CSS öğrenimi

Seri Tamamlandı! 🎉

CSS eğitim serimizde öğrendikleriniz:

  1. CSS Temellerini - Syntax, seçiciler ve temel kavramlar
  2. Kutu Modelini - Layout’un temel yapı taşı
  3. Flexbox’ı - Modern tek boyutlu layout sistemi
  4. CSS Grid’i - Güçlü iki boyutlu layout sistemi
  5. Responsive Tasarımı - Tüm cihazlara uyumlu web siteleri
  6. Animasyonları - Etkileşimli ve dinamik deneyimler
  7. Modern CSS’i - Variables, functions ve gelecek özellikleri

Bundan sonrası…

CSS öğrenme yolculuğunuz burada bitmez! Şimdi:

  • Projeler yapın: Öğrendiklerinizi gerçek projelerde uygulayın
  • Framework’leri keşfedin: Tailwind CSS, Bootstrap gibi araçları inceleyin
  • Preprocessor’ları öğrenin: Sass, Less gibi araçları deneyin
  • Topluluğa katılın: CSS topluluklarında aktif olun
  • Güncel kalın: CSS’in sürekli gelişimini takip edin

CSS ustası olma yolunda artık güçlü temellere sahipsiniz! Haydi projelerinizde bu bilgileri kullanın! 🚀