CSS Media Features - aspect-ratio



CSS media feature aspect-ratio is used to check the aspect ratio of the viewport or the rendering surface. The aspect ratio is the ratio of the width to the height of a box. This media query allows you to target specific aspect ratios and apply different styles accordingly.

Possible Values

  • ratio − This is the desired ratio of the width to the height in the form of a fractional value or a single integer.

  • min-aspect-ratio − Specifies the minimum aspect ratio for the styles to apply.

  • max-aspect-ratio − Specifies the maximum aspect ratio for the styles to apply.

Syntax

   @media (aspect-ratio: 4/3){
      //css-style to be applied
   }

CSS aspect-ratio - ratio Value

The following example demonstrates use of the aspect-ratio when a ratio value is passed:

  • Here the background color of the div element changes to yellow when the viewport's aspect ratio is equal to 2/2 (at width and height 120).

  • When you move the sliders, the updateSize function is called. This function changes the size of the iframe and updates the labels.

<html>
<head>
<style>
   iframe {
      display: block;
   }
</style>
</head>
<body>
   <label id="labelWidth" for="width">Width:150</label>
   <input id="width" name="width" type="range" min="120" max="200" step="4" value="150" />
   <label id="labelHeight" for="width">Height:150</label>
   <input id="height" name="height" type="range" min="120" max="200" step="4" value="150" />
   
   <iframe
   id="block"
   srcdoc="<style> @media (aspect-ratio: 2/2) { div { background: lightgreen; } } </style><div>To see the effect resize your viewport's width and height.</div>">
   </iframe>
   
   <script>
      const updateSize = (size, label) => {
         block.style[size] = `${eval(size).value}px`;
         label.textContent = `${size}: ${eval(size).value}`;
      };

      width.oninput = () => updateSize("width", labelWidth);
      height.oninput = () => updateSize("height", labelHeight);
   </script>
</body>
</html>   

CSS aspect-ratio - max-aspect-ratio Value

The following example demonstrates how max-aspect-ratio media feature changes the background color of the div element to violet when the viewport's aspect ratio is less than or equal to 3/2 −

<html>
<head>
<style>
   iframe {
      display: block;
   }
</style>
</head>
<body>
   <label id="labelWidth" for="width">Width:150</label>
   <input id="width" name="width" type="range" min="120" max="200" step="4" value="150" />
   <label id="labelHeight" for="width">Height:150</label>
   <input id="height" name="height" type="range" min="120" max="200" step="4" value="150" />
   
   <iframe
   id="block"
   srcdoc="<style> @media (max-aspect-ratio: 3/2) { div { background:violet; } } </style><div>To see the effect resize your viewport's width and height.</div>">
   </iframe>
      
   <script>
      const updateSize = (size, label) => {
         block.style[size] = `${eval(size).value}px`;
         label.textContent = `${size}: ${eval(size).value}`;
      };

      width.oninput = () => updateSize("width", labelWidth);
      height.oninput = () => updateSize("height", labelHeight);
   </script>
</body>
</html>        

CSS aspect-ratio - min-aspect-ratio Value

The following example demonstrates how min-aspect-ratio media feature changes the background color of the div element to yellow when the viewport's aspect ratio is greater than or equal to 4/3 −

<html>
<head>
<style>
   iframe {
      display: block;
   }
</style>
</head>
<body>
   <label id="labelWidth" for="width">Width:150</label>
   <input id="width" name="width" type="range" min="120" max="200" step="4" value="150" />
   <label id="labelHeight" for="width">Height:150</label>
   <input id="height" name="height" type="range" min="120" max="200" step="4" value="150" />
   
   <iframe
   id="block"
   srcdoc="<style> @media (min-aspect-ratio: 4/3) { div { background: yellow; } } </style><div>To see the effect resize your viewport's width and height.</div>">
   </iframe>
      
   <script>
      const updateSize = (size, label) => {
         block.style[size] = `${eval(size).value}px`;
         label.textContent = `${size}: ${eval(size).value}`;
      };

      width.oninput = () => updateSize("width", labelWidth);
      height.oninput = () => updateSize("height", labelHeight);
   </script>
</body>
</html>      
Advertisements