Skip to content

Instantly share code, notes, and snippets.

@icodesido
Created May 22, 2017 11:42
Show Gist options
  • Save icodesido/402db6e363a9b5c464f2bbb41448c9fd to your computer and use it in GitHub Desktop.
Save icodesido/402db6e363a9b5c464f2bbb41448c9fd to your computer and use it in GitHub Desktop.
mixin for smooth responsiveness
/**
* Computes a CSS calc function that betweens a value from
* A to B over viewport-width A to viewport-width B.
* Requires a media query to cap the value at B.
*/
@function between($to, $from, $toWidth, $fromWidth) {
$slope: ($to - $from) / ($toWidth - $fromWidth);
$base: $from - $slope * $fromWidth;
@return calc(#{$base} + #{100vw * $slope});
}
GALLERY
SNIPPETS
FORUMS
NEWSLETTER
JOBS
GUIDES
Between the Lines
BY DAVID BACHMANN ON APRIL 18, 2017
CALC, RWD
Media queries are great for changing values in sudden snaps at different screen sizes. But, combining the power of calc() and viewport units like vw and vh , we can get an awful lot of fluidity across our layouts. For this we'll use a technique called linear interpolation.
Linear interpolation is a formula used to find a value between two points on a line. In our case those two points are CSS values, like font-sizes, margins or widths, that we want to interpolate between over a set of viewport widths.
The reason we might want to interpolate between values over a set of viewport widths is to avoid having to create multiple breakpoints to control the flow of our content when the viewport changes. Instead, we let the user's browser calculate, according to our instructions, what values it gets. Let me explain.
The following snippet is a Sass function based on linear interpolation that we, at Aranja, have been calling between.
HelloSign eSign APIEmbed docs directly on your website with a few lines of code.
/**
* Computes a CSS calc function that betweens a value from
* A to B over viewport-width A to viewport-width B.
* Requires a media query to cap the value at B.
*/
@function between($to, $from, $toWidth, $fromWidth) {
$slope: ($to - $from) / ($toWidth - $fromWidth);
$base: $from - $slope * $fromWidth;
@return calc(#{$base} + #{100vw * $slope});
}
The function is used like so:
$small: 400px;
$large: 1000px;
.Container {
/* The base (smallest) value. */
padding: 20px;
/* In $small it should be 20px and in $large it should be 100px, */
/* In viewports between that its padding should be calculated */
@media (min-width: $small) {
padding: between(20px, 100px, $small, $large);
}
/* In $large we cap the value at 100px */
@media (min-width: $large) {
padding: 100px;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment