Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Aspect ratio works only when when width is changed not for height #4

Open
slash851 opened this issue Feb 7, 2019 · 1 comment
Open

Comments

@slash851
Copy link

slash851 commented Feb 7, 2019

All works fine for width changes.
Could you extend the property to handle as well when the height is changed to adjust width - keeping the aspect ratio.

@tomhodgins
Copy link
Owner

tomhodgins commented Feb 7, 2019

Hi @slash851! I am not sure I'll modify this plugin, but we can create a new plugin fairly easily that will work for defining an aspect ratio to determine the width, based on the rendered height of an element:

<div class=width style=' --aspect-ratio: ["width", 16, 9]; '>Width-based</div>
<div class=height style=' --aspect-ratio: ["height", 16, 9]; '>Height-based</div>

<style>
  .width {
    min-width: 500px;
    height: var(--aspect-ratio);
    background-color: red;
  }
  .height {
    min-height: 500px;
    width: var(--aspect-ratio);
    background-color: blue;
  }
</style>

<script type=module>
  import computedVariables from 'https://unpkg.com/computed-variables/index.es.js'

  computedVariables(
    '--aspect-ratio',
    ([feature, w, h], event, tag) => {
      const features = {

        // Function for finding correct height
        width: tag => {
          tag.style.width = 'auto'
          const size = tag.offsetWidth / (w / h)
          tag.style.width = ''
          return size
        },

        // Function for finding correct width
        height: tag => {
          tag.style.height = 'auto'
          const size = tag.offsetHeight / (w / h)
          tag.style.height = ''
          return size
        }
      }

      // Return value as px unit
      return features[feature](tag) + 'px'
    }
  )
</script>

And the result:

screen shot 2019-02-07 at 12 17 44 pm

This demo makes use of the computed-variables which is a lot more flexible than these CSSplus plugins.

There are a couple more demos for defining aspect ratios, these both work with widths, but based on the code above it should be easy to see how translate a width-based solution to a height-based one:

and the demo above we've used --aspect-ratio: ["height", 16, 9] so we can pass in the direction, width and height separately - but you also have flexibility to structure this however suits you - here are some other ideas:

  • --aspect-ratio: {"height": {"width": 16, "height": 9}};
  • --aspect-ratio: [16, 9];

The only thing to keep in mind when exploring these solutions with height instead of width, is that layout in the browser is width-based, so the height usually ends up fitting the content. If you want to set a width based on the height you may find yourself having to give elements a min-height or max-height to help give them a size from which the width can be calculated!

Good luck and let me know if you have any questions using these solutions :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants