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.

1px gap with breakpoints & media queries

See original GitHub issue

@carbon/elements@10.34.0

I am mainly using the grid classes for responsive behavior. But for some elements, I am using media queries that listens to the grid breakpoints:

@media (max-width: $carbon--grid-breakpoint-large-width) {
   // ...
}
@media (min-width: $carbon--grid-breakpoint-large-width) {
   // ...
}

I extract the variables like so

@import '~@carbon/elements/scss/layout/_breakpoint.scss';
$carbon--grid-breakpoint-large: map-get($carbon--grid-breakpoints, 'lg');
$carbon--grid-breakpoint-large-width: map-get($carbon--grid-breakpoint-large, 'width');

The problem is, both media queries are applied on one exact witdh (1056px, which is for the large breakpoint).

I simply would like to have a -1px calculation on the max-width media query. But I am receiving a rem value from the variable. I’m also not sure if rem/px calculation is even possible, i did not found a concrete working example for this. When I do a calc($carbon--grid-breakpoint-large-width - 1px), the media query does not apply on any browser width. Tried also with calc(#{$carbon--grid-breakpoint-large-width} - 1px).

As a workaround I hardcoded the breakpoint px value in a new variable, and subtracted 1px, generating a new variable.

Can you maybe just serve an extractable px value from the breakpoint variable tree, instead of only a calculated rem value? Or serve a reverse calculator like carbon--px($remValue)?

// '~@carbon/elements/scss/layout/_breakpoint.scss';
$carbon--grid-breakpoints: (
// ...
  lg: (
    columns: 16,
    margin: carbon--rem(16px),
    width: carbon--rem(1056px),
  ),
// ...

Or have you other suggestions?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
fabpicocommented, May 18, 2021

Yes that worked indeed

@import '~@carbon/elements/scss/layout/_breakpoint.scss';
@import '~carbon-components/src/globals/scss/_typography.scss';
$carbon--grid-breakpoint-large: map-get($carbon--grid-breakpoints, 'lg');
$carbon--grid-breakpoint-large-width: map-get($carbon--grid-breakpoint-large, 'width');
$carbon--grid-breakpoint-large-width-px: (($base-font-size / 1px) * ($carbon--grid-breakpoint-large-width / 1rem)) * 1px;
$carbon--grid-breakpoint-medium-width-end-px: $carbon--grid-breakpoint-large-width-px - 1px;

$carbon--grid-breakpoint-medium-width-end-px then is compiled to 1055px as expected.

Are you considering to add a function for reuse, in a next release?

0reactions
fabpicocommented, May 26, 2021

@tay1orjones Unfortunately this didn’t work for me. How can I reopen the issue?

When I do

@import '~@carbon/elements/scss/layout/breakpoint';

@include carbon--breakpoint-between(md, lg, $carbon--grid-breakpoints) {
   // ..
}

it is compiled to

@media (min-width: null) and (max-width: null) {
   // ..
}

I debugged the calculations https://github.com/carbon-design-system/carbon/blob/d4512961a83cf1465fc4de536a3ce8e82be39ba5/packages/layout/scss/_breakpoint.scss#L201

https://github.com/carbon-design-system/carbon/blob/d4512961a83cf1465fc4de536a3ce8e82be39ba5/packages/layout/scss/_breakpoint.scss#L206

$min-width/$max-width are both null. When I remove the + 0.02 / - 0.02 calculations, the results are 42rem/66rem. For me this looks like a bug in the calculation. Or am I using the mixin wrong?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Responsive display settings have 1px gap in breakpoints #5221
The fix would be to ensure the frontend styles all use min-width for the toggle styles media queries, rather than a mixture of...
Read more >
the 1px gap when using min and max with media queries
Using Bootstrap to create a mega menu and between breakpoints 991px (down) and 992px (up), I get a moment of bs default menu...
Read more >
Prevent double breakpoint | tzi.fr - Thomas Zilliox
Use a gap of 1px or 0.01em between your media-queries or use the sass-mq project. Happy coding, Thomas. That's my face! Thomas ZILLIOX....
Read more >
Responsive Layouts, Fewer Media Queries | CSS-Tricks
We can use max(400px, 100%/(N + 1) + 0.1%) . The logic is: we tell the browser that each item has a width...
Read more >
Beginner's guide to media queries - Learn web development
The CSS Media Query gives you a way to apply CSS only when the browser and device environment matches a rule that you...
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