Menu
Coddy logo textTech

שימוש במשתני CSS

חלק מהיחידה פיתוח Frontend מעשי במסלול ה-HTML של Coddy. שיעור 2 מתוך 35.

משתני CSS, שנקראים גם מאפיינים מותאמים אישית, מאפשרים לך לשמור ערכים תחת שם ולהשתמש בהם מחדש בכל רחבי ה-CSS שלך.
משתנים מוגדרים בתוך בורר (בדרך כלל :root, כך שהמשתנה פועל בכל מקום בדף; במקרה כזה הוא נקרא גלובלי) באמצעות התחביר:

:root {
 --main-color: midnightblue;
 --padding: 16px;
}

כדי להשתמש בערך שמאוחסן במשתנה, השתמש בפונקציה var():

.button {
  background-color: var(--main-color);
  padding: var(--padding);
}
challenge icon

אתגר

קל

יש לך דף אינטרנט שמציג כמה סרטים המוקרנים בקולנוע. לכל סרט יש כפתור קניית כרטיס.

המשימה שלך:

  1. צור משתנה CSS בשם --main-color בתוך הבורר :root והגדר את הערך שלו ל-seagreen.
  2. השתמש במשתנה הזה כדי להגדיר את צבע הרקע של כל רכיבי <button> בדף.

נסו בעצמכם

<html>
<head>
    <title>How to declare a variable</title>
    <style>
        :root {
            --bg-color: #4c87f5;
             /* Write CSS rules here */
             
          }
        body {
            font-family: Arial, sans-serif;
            background: var(--bg-color);
            padding: 20px;
          }
        .movie {
            background: white;
            padding: 15px;
            margin-bottom: 15px;
            border-radius: 8px;
          }
        .movie h2 {
            margin: 0 0 10px;
          }
        .movie p {
            margin: 0 0 10px;
          }
        button {
            color: black;
            border: none;
            padding: 8px 12px;
            border-radius: 4px;
            cursor: pointer;
             /* Write CSS rules here */
            
          }
    </style>
</head>
<body>
    <h1>Today's Movies</h1>
    <div class="movie">
      <h2>Ocean Whispers</h2>
      <p>A sailor discovers a hidden island and the secrets of the sea.</p>
      <button>Buy Ticket</button>
    </div>
    
    <div class="movie">
      <h2>Midnight Chase</h2>
      <p>A detective races against the clock to stop a mysterious heist.</p>
      <button>Buy Ticket</button>
    </div>
    
    <div class="movie">
      <h2>Starlight Bakery</h2>
      <p>A warm comedy about friendship and following your dreams.</p>
      <button>Buy Ticket</button>
</div>
</body>
</html>
quiz iconבחנו את עצמכם

השיעור הזה כולל חידון קצר. התחילו את השיעור כדי לענות עליו ולעקוב אחרי ההתקדמות.

כל השיעורים ביחידה פיתוח Frontend מעשי

תרגלו בעצמכם: Web אונליין