CSS Functions

Frontend SELF EN
Level 31 , Lesson 4
Available

5.1 The calc() Function

CSS functions give you powerful tools to perform various operations directly within your style sheets. The calc(), min(), max(), and clamp() functions are super handy for creating responsive and flexible designs because they let you do math operations and conditional value calculations.

The calc() function is used for performing math operations in CSS. It allows you to mix different units of measurement, like pixels (px), percentages (%), em, rem, and others, in one expression.

Syntax:

    
      selector {
        property: calc(expression);
      }
    
  

Usage Example

In this example, the width of the block is calculated as 50% of the parent element's width minus 20 pixels:

CSS
    
      /* Width of the block is half of the parent element minus 20px */

      .block {
        width: calc(50% - 20px);
      }
    
  

5.2 Functions min(), max(), and clamp()

The min(), max(), and clamp() functions in CSS

The min() Function

The min() function takes several values and returns the smallest one. It's useful for creating flexible sizes that change based on the context.

Syntax:

    
      selector {
        property: min(value1, value2);
      }
    
  

Example

In this example, the container width will be either 50% or 300 pixels, whichever is smaller:

CSS
    
      .container {
        width: min(50%, 300px);
      }
    
  

The max() Function

The max() function takes several values and returns the largest one. It's useful for setting minimum sizes and ensuring responsiveness.

The clamp() Function

The clamp() function takes three values: a minimum, a preferred, and a maximum. It constrains the value between the minimum and maximum, leaning towards the preferred value.

5.3 Advantages of Using CSS Functions

Advantages of using CSS functions:

  • Flexibility. CSS functions let you create more flexible and responsive styles as they allow dynamic value calculations.
  • Simplifying Responsive Design. The functions min(), max(), and clamp() make it way easier to create responsive designs by allowing values to be bound by specific limits.
  • Reducing the Need for Media Queries. Using these functions can cut down on the number of media queries needed to adapt styles for different screen sizes.

5.4 Full Implementation Example

CSS
    
      .container {
        display: flex;
        flex-direction: column;
        gap: 10px;
        padding: 20px;
      }

      .block {
        background-color: #3498db;
        color: white;
        padding: 10px;
        text-align: center;
      }

      .block:nth-child(1) {
        width: calc(50% - 20px);
      }

      .block:nth-child(2) {
        width: min(300px, 100%);
      }

      .block:nth-child(3) {
        width: max(200px, 50%);
      }

      .text {
        font-size: clamp(12px, 2vw, 24px);
      }
    
  
HTML
    
      <!DOCTYPE html>
      <html lang="en">
        <head>
          <meta charset="UTF-8">
          <meta name="viewport" content="width=device-width, initial-scale=1.0">
          <title>CSS Functions Usage Example</title>
          <link rel="stylesheet" href="styles.css">
        </head>
        <body>
          <div class="container">
            <div class="block">Block with width calc(50% - 20px)</div>
            <div class="block">Block with width min(300px, 100%)</div>
            <div class="block">Block with width max(200px, 50%)</div>
            <p class="text">Text with font size clamp(12px, 2vw, 24px)</p>
          </div>
        </body>
      </html>
    
  
1
Task
Frontend SELF EN, level 31, lesson 4
Locked
Width with calc()
Width with calc()
1
Task
Frontend SELF EN, level 31, lesson 4
Locked
Adaptive Width with min()
Adaptive Width with min()
1
Опрос
CSS Variables,  31 уровень,  4 лекция
недоступен
CSS Variables
CSS Variables
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION