Media Template Trick
See original GitHub issueHi all. An issue might not be the best place for this, but I wanted to share a little trick I discovered with the styled-components
community. It’s really too simple to be a feature or utility function in the library itself, but I am finding it extremely useful and wanted to give back.
const sizes = {
giant: 1170,
desktop: 992,
tablet: 768,
phablet: 572,
phone: 376
}
export const media = Object.keys(sizes).reduce((accumulator, label) => {
accumulator[label] = (...args) => css`
@media (max-width: ${sizes[label]}px) {
${css(...args)}
}
`
return accumulator
}, {})
This creates a media template for each of the sizes, so you can do this in your components:
const Container = styled.div`
color: #333;
${media.desktop`padding: 0 18px;`}
${media.tablet`padding: 0 18px;`}
${media.phablet`padding: 0 10px;`}
${media.phone`padding: 0 3px;`}
`
Thanks for all the work you are doing. styled-components
is the most elegant styling solution I’ve seen so far.
Issue Analytics
- State:
- Created 7 years ago
- Reactions:21
- Comments:5 (5 by maintainers)
Top Results From Across the Web
ONE TRICK TEMPLATE FOR SOCIAL MEDIA ...
In this book, I will be teaching you how to solve problems of social media management and personal Branding. The average person has...
Read more >30 Free Social Media Templates to Save You Hours of Work
These 30 social media templates will save you a ton of time and effort, from creating content, to publishing posts and measuring results....
Read more >Social media tutorial tip trick post template Vector Image
Social media tutorial, tip, trick, quick tips, layout template with geometric background design in blue, white, yellow color. Vector illustration.
Read more >22 Tips, tricks, and templates. ideas - Pinterest
Aug 29, 2016 - Explore HubSpot's board "Tips, tricks, and templates.", followed by 54729 people on ... 50 Social Media Image Templates |...
Read more >One Trick Template for Social Media Management : A Step by ...
Arrives by Tue, Nov 1 Buy One Trick Template for Social Media Management : A Step by Step Guide For Business Owners Who...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@valscion Thanks for that link. With CSS it’s literally possible to learn some new quirk every single day of your life. 😂
Would you be OK with changing the breakpoint example values from
px
unit toem
? It would work properly cross-browser and support users changing their browsersfont-size
accordingly.Source: https://zellwk.com/blog/media-query-units/