Last active
July 5, 2018 18:54
-
-
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)`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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}; | |
` | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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