question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Media Template Trick

See original GitHub issue

Hi 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:closed
  • Created 7 years ago
  • Reactions:21
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
erikrascommented, Nov 13, 2016

@valscion Thanks for that link. With CSS it’s literally possible to learn some new quirk every single day of your life. 😂

2reactions
valscioncommented, Nov 12, 2016

Would you be OK with changing the breakpoint example values from px unit to em? It would work properly cross-browser and support users changing their browsers font-size accordingly.

Source: https://zellwk.com/blog/media-query-units/

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found