CSS 媒体特性 - aspect-ratio



CSS 媒体特性 **aspect-ratio** 用于检查视口或渲染表面的纵横比。纵横比是盒子宽度与高度的比率。此媒体查询允许您定位特定的纵横比并相应地应用不同的样式。

可能的值

  • **ratio** − 这是所需宽度与高度的比率,以分数或单个整数的形式表示。

  • **min-aspect-ratio** − 指定样式应用的最小纵横比。

  • **max-aspect-ratio** − 指定样式应用的最大纵横比。

语法

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

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

CSS aspect-ratio - ratio 值

以下示例演示了在传递比率值时 **aspect-ratio** 的用法

  • 当视口的纵横比等于 2/2(宽度和高度为 120)时,div 元素的背景颜色变为黄色。

  • 移动滑块时,将调用 **updateSize** 函数。此函数更改 iframe 的大小并更新标签。

Open Compiler
<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 时 −

Open Compiler
<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 时 −

Open Compiler
<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>
广告