Skip to content

Commit c35991b

Browse files
Always use 1rem = 16px for pixel equivalents in media queries (#1190)
Fixes #1189 From [the CSS spec](https://drafts.csswg.org/mediaqueries/#units): > Relative length units in media queries are based on the initial value, which means that units are never based on results of declarations. For example, in HTML, the em unit is relative to the initial value of font-size, defined by the user agent or the user’s preferences, not any styling on the page. We should hide these because `1rem` everywhere else will be one value but `1rem` in a media query might still be 16px (it's not changeable by websites — only browsers / users)
1 parent ae2c6d9 commit c35991b

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

packages/tailwindcss-language-service/src/util/pixelEquivalents.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,12 @@ export function addPixelEquivalentsToValue(
4040
return value
4141
}
4242

43-
function getPixelEquivalentsForMediaQuery(params: string, rootFontSize: number): Comment[] {
43+
function getPixelEquivalentsForMediaQuery(params: string): Comment[] {
44+
// Media queries in browsers are not affected by the font size on the `html` element but the
45+
// initial value of font-size as provided by the browser. This is defaults to 16px universally
46+
// so we'll always assume a value of 16px for media queries for correctness.
47+
let rootFontSize = 16
48+
4449
let comments: Comment[] = []
4550

4651
try {
@@ -65,9 +70,9 @@ function getPixelEquivalentsForMediaQuery(params: string, rootFontSize: number):
6570
return comments
6671
}
6772

68-
export function addPixelEquivalentsToMediaQuery(query: string, rootFontSize: number): string {
73+
export function addPixelEquivalentsToMediaQuery(query: string): string {
6974
return query.replace(/(?<=^\s*@media\s*).*?$/, (params) => {
70-
let comments = getPixelEquivalentsForMediaQuery(params, rootFontSize)
75+
let comments = getPixelEquivalentsForMediaQuery(params)
7176
return applyComments(params, comments)
7277
})
7378
}
@@ -88,12 +93,10 @@ export function equivalentPixelValues({
8893
}
8994

9095
comments.push(
91-
...getPixelEquivalentsForMediaQuery(atRule.params, rootFontSize).map(
92-
({ index, value }) => ({
93-
index: index + atRule.source.start.offset + `@media${atRule.raws.afterName}`.length,
94-
value,
95-
}),
96-
),
96+
...getPixelEquivalentsForMediaQuery(atRule.params).map(({ index, value }) => ({
97+
index: index + atRule.source.start.offset + `@media${atRule.raws.afterName}`.length,
98+
value,
99+
})),
97100
)
98101
},
99102
},

packages/vscode-tailwindcss/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- Fix parsing of `@custom-variant` shorthand in Tailwind CSS language mode ([#1183](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/1183))
1010
- Make sure custom regexes apply in Vue `<script>` blocks ([#1177](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/1177))
1111
- Fix suggestion of utilities with slashes in them in v4 ([#1182](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/1182))
12+
- Assume 16px font size for `1rem` in media queries ([#1190](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/1190))
1213

1314
## 0.14.3
1415

0 commit comments

Comments
 (0)