- CSS Tutorial
- CSS - Home
- CSS - Introduction
- CSS - Syntax
- CSS - Selectors
- CSS - Inclusion
- CSS - Measurement Units
- CSS - Colors
- CSS - Backgrounds
- CSS - Fonts
- CSS - Text
- CSS - Images
- CSS - Links
- CSS - Tables
- CSS - Borders
- CSS - Border Block
- CSS - Border Inline
- CSS - Margins
- CSS - Lists
- CSS - Padding
- CSS - Cursor
- CSS - Outlines
- CSS - Dimension
- CSS - Scrollbars
- CSS - Inline Block
- CSS - Dropdowns
- CSS - Visibility
- CSS - Overflow
- CSS - Clearfix
- CSS - Float
- CSS - Arrows
- CSS - Resize
- CSS - Quotes
- CSS - Order
- CSS - Position
- CSS - Hyphens
- CSS - Hover
- CSS - Display
- CSS - Focus
- CSS - Zoom
- CSS - Translate
- CSS - Height
- CSS - Hyphenate Character
- CSS - Width
- CSS - Opacity
- CSS - Z-Index
- CSS - Bottom
- CSS - Navbar
- CSS - Overlay
- CSS - Forms
- CSS - Align
- CSS - Icons
- CSS - Image Gallery
- CSS - Comments
- CSS - Loaders
- CSS - Attr Selectors
- CSS - Combinators
- CSS - Root
- CSS - Box Model
- CSS - Counters
- CSS - Clip
- CSS - Writing Mode
- CSS - Unicode-bidi
- CSS - min-content
- CSS - All
- CSS - Inset
- CSS - Isolation
- CSS - Overscroll
- CSS - Justify Items
- CSS - Justify Self
- CSS - Tab Size
- CSS - Pointer Events
- CSS - Place Content
- CSS - Place Items
- CSS - Place Self
- CSS - Max Block Size
- CSS - Min Block Size
- CSS - Mix Blend Mode
- CSS - Max Inline Size
- CSS - Min Inline Size
- CSS - Offset
- CSS - Accent Color
- CSS - User Select
- CSS Advanced
- CSS - Grid
- CSS - Grid Layout
- CSS - Flexbox
- CSS - Visibility
- CSS - Positioning
- CSS - Layers
- CSS - Pseudo Classes
- CSS - Pseudo Elements
- CSS - @ Rules
- CSS - Text Effects
- CSS - Paged Media
- CSS - Printing
- CSS - Layouts
- CSS - Validations
- CSS - Image Sprites
- CSS - Important
- CSS - Data Types
- CSS3 Tutorial
- CSS3 - Tutorial
- CSS - Rounded Corner
- CSS - Border Images
- CSS - Multi Background
- CSS - Color
- CSS - Gradients
- CSS - Box Shadow
- CSS - Box Decoration Break
- CSS - Caret Color
- CSS - Text Shadow
- CSS - Text
- CSS - 2d transform
- CSS - 3d transform
- CSS - Transition
- CSS - Animation
- CSS - Multi columns
- CSS - Box Sizing
- CSS - Tooltips
- CSS - Buttons
- CSS - Pagination
- CSS - Variables
- CSS - Media Queries
- CSS - Functions
- CSS - Math Functions
- CSS - Masking
- CSS - Shapes
- CSS - Style Images
- CSS - Specificity
- CSS - Custom Properties
- CSS Responsive
- CSS RWD - Introduction
- CSS RWD - Viewport
- CSS RWD - Grid View
- CSS RWD - Media Queries
- CSS RWD - Images
- CSS RWD - Videos
- CSS RWD - Frameworks
- CSS References
- CSS - Questions and Answers
- CSS - Quick Guide
- CSS - References
- CSS - Color References
- CSS - Web browser References
- CSS - Web safe fonts
- CSS - Units
- CSS - Animation
- CSS Resources
- CSS - Useful Resources
- CSS - Discussion
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>
To Continue Learning Please Login
Login with Google