Skip to content

Instantly share code, notes, and snippets.

@1-800-jono
Last active July 5, 2018 18:54
Show Gist options
  • Save 1-800-jono/fdbe35ed6ddfe28c953165d9fdc74b14 to your computer and use it in GitHub Desktop.
Save 1-800-jono/fdbe35ed6ddfe28c953165d9fdc74b14 to your computer and use it in GitHub Desktop.
JavaScript Fluid Property function for CSS - best with Styled-components. Example: `fp('font-size', 25, 40)`
//The fp() “Fluid Property” function
export function fp(property, min, max, start = 320, end = 1400, clip = true, clipAtStart = true, clipAtEnd = true) {
const multiplier = (max - min) / (end - start) * 100;
const adder = (min * end - max * start) / (end - start);
const formula = `calc((${multiplier}vw) + (${adder}px))`;
return`
@media (max-width: ${start}px) {
${property}: ${min}px;
}
@media (min-width: ${end}px) {
${property}: ${max}px;
}
${property}: ${formula};
`
}
//Example use of this function with Styled-Components library
//You can use any css property - margin, padding, width, height...
const Title = styled.h2`
${ fp('font-size', 25, 40) };
color: #fff;
margin-bottom: 25px;
`
const SubText = styled.h2`
${ fp('margin-bottom', 25, 40) };
`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment