Socket
Socket
Sign inDemoInstall

responsive-aspect-ratio

Package Overview
Dependencies
0
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    responsive-aspect-ratio

Responsive elements with a fixed aspect ratio


Version published
Maintainers
1
Created

Readme

Source

Responsive Aspect Ratio

responsive-aspect-ratio

Responsive elements with a fixed aspect ratio.

Reserve the vertical space needed by responsive elements on your web document such as images or videos when they are still loading so you can avoid the cumulative layout shift.

Install

$ npm i responsive-aspect-ratio
<script src="https://unpkg.com/responsive-aspect-ratio"></script>

Usage

Simply include the script as described above and add to the parent container of your responsive element a data attribute aspect-ratio with the desired aspect ratio (16:9, 1:1, 4:3...) as a value. For example: data-aspect-ratio="16:9", data-aspect-ratio="1:1", data-aspect-ratio="4:3"...

<div data-aspect-ratio="21:9">
    <video controls autoplay loop src="https://test-videos.co.uk/vids/bigbuckbunny/mp4/h264/360/Big_Buck_Bunny_360_10s_1MB.mp4"></video>
</div>
<picture data-aspect-ratio="16:9">
    <source srcset="https://placekitten.com/720/405" type="image/jpeg" />
    <img src="https://placekitten.com/720/405" alt="Kitten">
</picture>
<div data-aspect-ratio="4:3">
    <p>
        Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque sed ante ac erat rutrum ornare auctor vitae nibh. Nam posuere euismod dui, mattis maximus augue molestie nec, in condimentum sem consectetur ut.
    </p>
</div>

Examples

Go to this CodePen to see live example: https://codepen.io/alterebro/full/ZEBKQrY

Responsive Aspect Ratio Example

Info

This is based in the technique also known as the "padding hack" which defines the height of an element as a relative size to the width, taking advantage of the padding intrinsic properties. You probably have already seen it somewhere in some of its forms:

[container] {
    position: relative;
}
[container]:before {
    display: inherit;
    content: '';
    padding-top: 56.25%; /* 16:9 ratio */
}
[content] {
    position: absolute;
    top: 0;
}
[container] {
    position: relative;
    padding: ~"0 0 calc(100% / (16 / 9))"; // 16:9 ratio

    [content] {
        position: absolute;
        top: 0;
    }
}

Further reading

License

MIT License © Jorge Moreno @alterebro, 2021.

Keywords

FAQs

Last updated on 16 Feb 2021

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc