npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

responsive-aspect-ratio

v1.2.0

Published

Responsive elements with a fixed aspect ratio

Downloads

24

Readme

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

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.