Closed
Description
Environment
Rule details
Flag the use of px
What type of rule is this?
Warns about a potential problem
Example code
.card {
font-size: 12px;
}
Participation
I am willing to submit a pull request to implement this rule.
Additional comments
Needing !important indicates there may be a larger underlying issue.
What problem do you want to solve?
Rem and em are more accessibility-friendly than px.
What do you think is the correct solution?
The option to enable accessibility mode and warn about usage of px.
Participation
- I am willing to submit a pull request for this change.
Additional comments
I think it's very useful for improving accessibility.
https://www.a11y-collective.com/blog/what-is-rem-in-css/
Metadata
Metadata
Assignees
Labels
Type
Projects
Status
Complete
Activity
nzakas commentedon Feb 26, 2025
Thanks for the suggestion. This seems a bit too specific to be included as a general rule. (We typically don't create rules that simply disallow specific syntax.)
I think we'd be open to a rule that was a bit broader, maybe something that encouraged the use of specific font size types? So rather than just disallowing
px
, maybe it would look for a small set of "good" size types?nasty23-star commentedon Feb 26, 2025
Thank you for the clarification. Your suggestion sounds very good.
Tanujkanti4441 commentedon Apr 14, 2025
How about a rule called
required-font-unit
orrequired-font-size-unit
that will take a set of strings such asrem
,px
etc. as option, so rule would allow only these units for font-size?nzakas commentedon Apr 15, 2025
I think this is heading in the correct direction.
require-font-size-unit
(without the "d" in require) seems like it would make the most sense as a name, although I wonder if this rule needs an allow list or a deny list?Tanujkanti4441 commentedon Apr 16, 2025
I think for consistency, using an allowlist would be a good idea?
nzakas commentedon Apr 18, 2025
Makes sense. I'd like to find an article or something online with recommendations for which font size units are considered best practices so we can have a logical default setting.
Tanujkanti4441 commentedon Apr 19, 2025
Sure! these are some articles that suggest
rem
for font size:https://www.freecodecamp.org/news/css-units-when-to-use-each-one/
https://www.w3schools.com/css/css_units.asp
https://medium.com/design-bootcamp/css-font-sizing-beyond-pixels-for-better-design-35457c2650d3
nzakas commentedon Apr 22, 2025
Thanks! Going to post on social media to see what other responses we get.
bradzacher commentedon Apr 22, 2025
The thing we found while working with typescript-eslint is that almost any time you create a
no-
prefixed rule, someone will want to go against the "no" and instead enforce the inverse.I'd personally suggest a name like
consistent-font-sizes
which would then give you room to add an option for each style of font size whilst still adding a default value to enforce the perceived best practice.nzakas commentedon Apr 22, 2025
We had decided on
require-font-size-units
. #73 (comment)The title of the issue just wasn't updated.
[-]New Rule: no-px font sizing[/-][+]New Rule: Require specific font size units[/+]3 remaining items
nzakas commentedon Apr 24, 2025
I understand what you're saying. The reality is that trying to capture all of the ways font sizes interact with other measurements is both a massive amount of work and would be incredibly buggy. At that point, we're not just talking about flagging
font-size
units, we're now talking about what units for other measurements are appropriate for use withfont-size
units across all possible properties that contain measurements. Even if we accomplished all of that in a single rule, we would end up needing a ton of options for all of the different variations people would want supported.Our approach is to make rules small and self-contained. We could very well add another rule or set of rules around using relative measurements for other properties, and that would allow people to fine-tune their experience.
And of course, if you really want a rule as you've described it, you can always create your own rule that does exactly that.
nzakas commentedon May 8, 2025
After digging into this a bit, I'm wondering if what we really want is a
relative-font-units
rule that enforces the use of relative units?Let's say by default it enforces the use of
rem
, but you can optionally specify other units to allow, like this:Here's a list of relative units I found:
ic
unit on the root element's font.lh
unit on the root element's font.We could limit the options in
allow
to just those known relative units to ensure that people don't accidentally include absolute units.Thoughts?
kylemh commentedon May 8, 2025
This is a really well-written article about relative units of CSS and when to use them from the perspective of accessibility and maintainability from @joshwcomeau.
Just my opinion: I don't think all relative units should be acceptable. I think specifically and exclusively
rem
s should be allowed, at least with the default. If the mindset is to improve accessibility, allowing % or em units are much more fragile to implementation details. During development, a dev could take a UI from accessible to not accessible without knowing it if units with parent-relative values are used. Withrem
, they'd be affecting the entire site in - hopefully - a more obvious manner.nzakas commentedon May 8, 2025
@kylemh thanks for the feedback. Indeed, allowing just
rem
by default is exactly what I proposed in the previous comment. That said, I know from 12 years of ESLint development that we'll likely get asked to add a way to allow other units, too, and we should provide for that.illarionvk commentedon May 9, 2025
Consistent units across the project, not only font-size, would be more important for my team.
For example, we require all px values to be wrapped in a
rem-calc()
Sass function.We also use em values when it makes sense to inherit parent value
nzakas commentedon May 9, 2025
@illarionvk that would be beyond the scope of the type of rule we're looking to add here. You could definitely create a custom rule that does what you're asking.
Tanujkanti4441 commentedon May 10, 2025
I am in favor of
relative-font-units
, would like to implement this if accepted.nzakas commentedon May 12, 2025
Marking as accepted. @Tanujkanti4441 you got it!
relative-font-units
rule #133