Skip to content

[3.0] Unexpected line breaks in CSS values with commas #15070

Description

@gpoitch

Any css value with commas started being formatted with line breaks in 3.0 no matter its length. This was an unexpected (or undocumented) change in version 3.0. I believe this should respect the the print-width setting.

Prettier 3.0.0
Playground link

--parser css

Input:

.foo {
  transition: transform 1s, opacity 2s;
}

Output:

.foo {
  transition:
    transform 1s,
    opacity 2s;
}

Expected behavior:
Do not break lines in css that do not exceed the print-width

Activity

  1. MatanelGordon commented on Jul 10, 2023

    @MatanelGordon
    Contributor

    Why should that happen?
    In box-shadow, for example, I would expect Prettier to break the lines for me in case of multiple shadows.

    Have you considered increasing your --print-width setting?

  2. gpoitch commented on Jul 10, 2023

    @gpoitch
    Author

    It doesn't respect the print width setting. It breaks lines after the comma no matter what.
    This is an unexpected or undocumented change from 2.0 -> 3.0

  3. alex-kinokon commented on Jul 14, 2023

    @alex-kinokon

    Likely this is broken by #14208.

    Here’s an extreme case from the 3.0 line breaking:

    code {
      font-family:
        code,
        SF Mono,
        mono;
    }

    with the print width significantly higher than the actual width.

  4. thorn0 commented on Jul 14, 2023

    @thorn0
    Member

    I proposed a solution here: #6024 (comment)

    "wrap if any list item contains more than 2 spaces"

  5. alex-kinokon commented on Jul 14, 2023

    @alex-kinokon

    Can we just bail out if the line length is smaller than print width?

  6. mvorisek commented on Jul 14, 2023

    @mvorisek
    Contributor

    See changelog example:

    // Prettier stable
    .myclass {
      box-shadow: inset 0 0 10px #555, 0 0 20px black;
    }
    
    // Prettier main
    .myclass {
      box-shadow:
        inset 0 0 10px #555,
        0 0 20px black;
    }
    

    CSS directives separated by space are tuples formatted as single line optionally put on multiple lines if print width is shorter.

    CSS directives separated by space are typically a list of subdirectives/tuples. Formatting the list items on multiple lines is much more readable and sensual.

    Can we just bail out if the line length is smaller than print width?

    For these reasons, I do not think this will work for the example above which needs to be put on (at least) 2 lines no matter the print width.

  7. alex-kinokon commented on Jul 14, 2023

    @alex-kinokon

    That makes sense for box-shadow, but definitely doesn’t for font-family which is usually a long list of single word items. Take this example:

    body {
      font-family:
        system-ui,
        -apple-system,
        "Segoe UI";
    }

    which is the current output. I don’t think anyone wants this version.

    I think it makes sense if it behaves like the JavaScript printer that considers existing line break, e.g.

    const a = {a: 1,b:2}

    is formatted as

    const a = { a: 1, b: 2 };

    while

    const a = {
      a: 1,b:2}

    is formatted into

    const a = {
      a: 1,
      b: 2,
    };

    so if the user has a line break, we format the rest of the property into multiple lines.

  8. mvorisek commented on Jul 14, 2023

    @mvorisek
    Contributor

    I am the author of the fix. This is how I originally implemented it, but based on #6024 (comment) discussion there was a consensus to force the line break based on the AST structure rather than line break present. And I personally support this final approach.

    This is more or less prettier base idea and it makes sense, as prettier is not a fixer, it is an opinionated formatter.

    I personally propose to close this issue as won't fix.

    If someone wants to pursue this, I think the only option is to enumerate CSS directives that are:

    • shorthand - like transition
    • list with single value items - like font-family
    • list with tuples/multiple value items - like box-shadow

    For the first two groups of CSS directives the line breaks does not have to be forced. But the 3rd group can probably contain in many cases single value items only too, and the styling will be inconsistent...

  9. alex-kinokon commented on Jul 14, 2023

    @alex-kinokon

    #6024 (comment) already mentioned opting out for certain CSS properties, which I agree is a good starting point for fixing this.

    Prettier is an opinionated formatter.

    I am aware, but I have never seen anyone consider font-family broken down as 5 lines each consisting of only one word as intended behavior. This is a bug.

  10. changed the title [-]CSS comma line breaks in 3.0[/-] [+][3.0] Unexpected line breaks in CSS values with commas[/+] on Jul 14, 2023
  11. gpoitch commented on Jul 14, 2023

    @gpoitch
    Author

    I like @alex-kinokon's idea to respect the author's structure, same as how js works.

    Another idea: only break if one of the comma separated values is greater than some number (18?) or percentage of the print-width. That would satisfy both parties.

    Another inconsistency is this one actually doesn't add breaks, so there needs to be a space in-play as well:

    h1 {
      font-family: arial, sans-serif;
    }
  12. added
    lang:css/scss/less
    on Jul 16, 2023
  13. sosukesuzuki commented on Jul 16, 2023

    @sosukesuzuki
    Member

    I like @thorn0's proposal

    "wrap if any list item contains more than 2 spaces"

  14. andersk commented on Jul 20, 2023

    @andersk
    Contributor

    This makes even less sense with the font shorthand, because the font-size part is illogically grouped with the first item of font-family:

    body {
      font:
        86%/1.33 "Lucida Grande",
        Arial,
        sans-serif;
    }
  15. ghiscoding commented on Jul 24, 2023

    @ghiscoding

    Facing similar issue because it doesn't respect the --print-width config which makes it even worst on my side because after running prettier --write it makes ESLint unhappy because it conflicts because of the print width setting (basically, the plugins are cancelling each other out). So I can't even upgrade to Prettier 3 unless this issue is fixed or if I add prettier-ignore to all my box-shadow (which I don't want to do, so I guess I'll postpone the update until this issue is fixed).

    My perspective on the subject is the same as many of you, if it doesn't exceed --print-width then why split in multiple lines?

    image

  16. thorn0 commented on Jul 24, 2023

    @thorn0
    Member

    @ghiscoding You definitely can't update to Prettier 3 and keep using an older version of Prettier via ESLint. How did you expect that to work?

  17. ghiscoding commented on Jul 24, 2023

    @ghiscoding

    I actually managed to make it work by revisiting eslint-plugin-prettier installation docs and followed their recommended setting (also upgraded to "eslint-plugin-prettier": "^5.0.0" which supports Prettier 3)
    https://github.com/prettier/eslint-plugin-prettier#recommended-configuration

    Then you need to add plugin:prettier/recommended as the last extension in your .eslintrc.json:

    {
      "extends": ["plugin:prettier/recommended", "prettier"]
    }

    I was using eslint-config-prettier before and that caused the problem, replacing it with plugin:prettier/recommended worked, so at least now ESLint and Prettier are aligned.

    @ghiscoding You definitely can't update to Prettier 3 and keep using an older version of Prettier via ESLint. How did you expect that to work?

    @thorn0
    How do you know it was using an older version of Prettier? What hint do we have to find that out?

  18. Maks9 commented on Dec 1, 2023

    @Maks9

    Have to put prettier-ignore for the similar declaration font: 12px Arial, sans-serif; as @andersk mentioned previously. No much sence in line break here.

    body {
      font:
        12px Arial,
        sans-serif;
    }
    
  19. added theissue type on Mar 3, 2025
  20. jasonhibbs commented on Mar 12, 2025

    @jasonhibbs

    Landed here because of font-family, it looks wrong.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    lang:css/scss/less

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions