CSS 媒體特性 - aspect-ratio



CSS 媒體特性 **aspect-ratio** 用於檢查視口或渲染表面的縱橫比。縱橫比是盒子寬度與高度的比率。此媒體查詢允許您定位特定的縱橫比並相應地應用不同的樣式。

可能的值

  • **ratio** − 這是所需寬度與高度的比率,以分數或單個整數的形式表示。

  • **min-aspect-ratio** − 指定樣式應用的最小縱橫比。

  • **max-aspect-ratio** − 指定樣式應用的最大縱橫比。

語法

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

CSS aspect-ratio - ratio 值

以下示例演示了在傳遞比率值時 **aspect-ratio** 的用法

  • 當視口的縱橫比等於 2/2(寬度和高度為 120)時,div 元素的背景顏色變為黃色。

  • 移動滑塊時,將呼叫 **updateSize** 函式。此函式更改 iframe 的大小並更新標籤。

<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 值

以下示例演示了 **max-aspect-ratio** 媒體特性如何將 **div** 元素的背景顏色更改為紫色,當視口的縱橫比小於或等於 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 值

以下示例演示了 **min-aspect-ratio** 媒體特性如何將 **div** 元素的背景顏色更改為黃色,當視口的縱橫比大於或等於 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>      
廣告

© . All rights reserved.