-
Notifications
You must be signed in to change notification settings - Fork 4
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
Comments
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: 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
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 Good luck and let me know if you have any questions using these solutions :D |
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.
The text was updated successfully, but these errors were encountered: