Skip to content

No line break insertions in short CSS selectors #5086

Open

Description

@janosh

Prettier 1.14.2
Playground link

--parser babylon
--no-semi

I suggest reconfiguring the changes made in #1962 so as to only insert line breaks in CSS selectors when they exceed a given length. Otherwise, you end up with unnecessarily long files in cases such as the one below.

Input:

import styled from 'styled-components'

import mediaQuery from '../../utils/mediaQuery'

const Participants = styled.section`
  padding: 2rem;
  background: ${props => props.theme.mainWhite};
  display: grid;
  grid-gap: 2rem;
  h1, h2, h3, h4 {
    width: 100%;
    padding: 0.5rem 2rem;
    background: ${props => props.theme.lightGreen};
  }
  ${mediaQuery.phone} {
    h1, h2, h3, h4 {
      font-size: 1rem;
    }
  }
  ${mediaQuery.tablet} {
    grid-gap: 1rem;
    h1, h2, h3, h4 {
      grid-column: 1/-1;
    }
  }
  ${mediaQuery.minTablet} {
    h1, h2, h3, h4 {
      white-space: nowrap;
      grid-row: 2;
    }
  }
`

export default Participants

Output:

import styled from "styled-components"

import mediaQuery from "../../utils/mediaQuery"

const Participants = styled.section`
  padding: 2rem;
  background: ${props => props.theme.mainWhite};
  display: grid;
  grid-gap: 2rem;
  h1,
  h2,
  h3,
  h4 {
    width: 100%;
    padding: 0.5rem 2rem;
    background: ${props => props.theme.lightGreen};
  }
  ${mediaQuery.phone} {
    h1,
    h2,
    h3,
    h4 {
      font-size: 1rem;
    }
  }
  ${mediaQuery.tablet} {
    grid-gap: 1rem;
    h1,
    h2,
    h3,
    h4 {
      grid-column: 1/-1;
    }
  }
  ${mediaQuery.minTablet} {
    h1,
    h2,
    h3,
    h4 {
      white-space: nowrap;
      grid-row: 2;
    }
  }
`

export default Participants

Expected behavior:

No insertion of line breaks in CSS selectors of length less than, say, 80.

Activity

  1. added
    lang:css/scss/less
    status:needs discussion
    on Sep 13, 2018
  2. j-f1 commented on Sep 13, 2018

    @j-f1
    Member

    Perhaps we shouldn’t break if all of the sectors are bare element selectors.

  3. lydell commented on Sep 13, 2018

    @lydell
    Member

    @j-f1 Sounds reasonable. Thinking back of the previous issues, I think that’s the primary use case.

  4. janosh commented on Jan 20, 2019

    @janosh
    Author

    Just out of curiosity, was this issue addressed by any PR?

  5. lydell commented on Jan 20, 2019

    @lydell
    Member

    @janosh No PR for this so far!

  6. added
    help wanted
    and removed
    status:needs discussion
    on Mar 5, 2019
  7. j-f1 commented on Mar 5, 2019

    @j-f1
    Member

    Let’s go with the heuristic of not automatically breaking selectors that consist of just element selectors.

  8. syntag commented on Mar 5, 2019

    @syntag

    Let’s go with the heuristic of not automatically breaking selectors that consist of just element selectors.

    This would undeniably be better default behavior than the current implementation. And since this is a relevant topic, I still think it's worth considering offering a config for this, whether it be toggling multiline for style selectors or setting some kind of max-len as referenced here #5937

    It can sometimes be more preferable to read/write:

    .heading, h1, h2 {
       margin: 0;
    }

    as opposed to:

    .heading
    h1,
    h2 {
      margin: 0;
    }

    Prettier has handled this for Javascript, so what I am proposing is something similar for CSS too. For example, it's generally more preferable to read/write the following:

    const { Header, Footer, Nav } = require('./layout');

    as opposed to:

    const {
      Header,
      Footer,
      Nav,
    } = require('./layout');
  9. lydell commented on Mar 5, 2019

    @lydell
    Member

    I think we should keep things as they are. Keep it simple! As of this writing, there are no 👍s on this issue.

  10. removed
    help wanted
    on Mar 5, 2019
  11. alexander-akait commented on Mar 5, 2019

    @alexander-akait
    Member

    I think we can close issue, printing in one line unreadable

  12. syntag commented on Mar 5, 2019

    @syntag

    I think we can close issue, printing in one line unreadable

    To say the one-line code examples given above are unreadable is a bit of an over exaggeration.

    I think @j-f1 idea would be a great solution to this.

  13. lydell commented on Mar 6, 2019

    @lydell
    Member

    Feel free to provide more examples! 👍

  14. 14 remaining items

  15. aravindvnair99 commented on Nov 25, 2019

    @aravindvnair99

    I feel it makes better sense to split to multiple lines when it goes beyond a certain number of characters as pointed out by @syntag at #5086 (comment), @janosh at #5086 (comment), @j-f1 at #5086 (comment)

    Also, as per the official Prettier - Options page, it says

    For readability we recommend against using more than 80 characters:

    In code styleguides, maximum line length rules are often set to 100 or 120. However, when humans write code, they don't strive to reach the maximum number of columns on every line. Developers often use whitespace to break up long lines for readability. In practice, the average line length often ends up well below the maximum.

    Prettier, on the other hand, strives to fit the most code into every line. With the print width set to 120, prettier may produce overly compact, or otherwise undesirable code.

    So why not apply the same logic to CSS as well? Put as many as 80 characters and only split when the code is untidy instead of splitting for each selector which makes it tough to scroll or decreases readability in the case of long CSS files. I feel an option to choose between the two styles would be nice to have.

  16. Jason-2020 commented on May 26, 2020

    @Jason-2020

    Is there any workaround?

  17. nickbclifford commented on Mar 4, 2021

    @nickbclifford

    Hello from 2021! Have there been any updates or workarounds for this since the last post?

  18. janosh commented on Mar 8, 2021

    @janosh
    Author

    @nickbclifford It seems unlikely that Prettier will change its behavior here. But a simple workaround is to use the CSS pseudo-classes :is and :where (they only differ in specificity).

    Prettier will not break inside these selectors:

    :is(h1, h2, h3, h4, h5, h6) {
      margin: 0;
    }

    If your selectors have a common parent or child, I wouldn't even consider this a workaround but the new best practice:

    Old CSS

    section h1,
    section h2,
    section h3,
    section h4,
    section h5,
    section h6 {
      margin: 0;
    }

    New CSS

    section :is(h1, h2, h3, h4, h5, h6) {
      margin: 0;
    }

    Browser support is 90% for :is and 81% for :where so unless you still care about IE, you can start using at least :is right away. 😎

  19. shirobachi commented on May 1, 2021

    @shirobachi

    It looks like I've the same problem..
    I've

          p, h3, ul {
            margin: 0;
            padding: 0;
          }

    Prettier make:

          p,
          h3,
          ul {
            margin: 0;
            padding: 0;
          }

    I want to have:

          p, h3, ul {
            margin: 0;
            padding: 0;
          }
    

    Is it possible?

  20. AlexandroPerez commented on Jun 15, 2021

    @AlexandroPerez

    @Jason-2020 I was having the same issue, and was about to have all my .css files be ignored by prettier, when I stumbled upon the ignore comment, when I was looking for a way to do it.

    @shirobachi So we can have the following in css:

    /* prettier-ignore */
    p, h3, ul {
      margin: 0;
      padding: 0;
    }

    @janosh and the following in javascript:

    import styled from "styled-components";
    
    import mediaQuery from "../../utils/mediaQuery";
    
    // prettier-ignore
    const Participants = styled.section`
      padding: 2rem;
      background: ${props => props.theme.mainWhite};
      display: grid;
      grid-gap: 2rem;
      h1, h2, h3, h4 {
        width: 100%;
        padding: 0.5rem 2rem;
        background: ${props => props.theme.lightGreen};
      }
      ${mediaQuery.phone} {
        h1, h2, h3, h4 {
          font-size: 1rem;
        }
      }
      ${mediaQuery.tablet} {
        grid-gap: 1rem;
        h1, h2, h3, h4 {
          grid-column: 1/-1;
        }
      }
      ${mediaQuery.minTablet} {
        h1, h2, h3, h4 {
          white-space: nowrap;
          grid-row: 2;
        }
      }
    `
    
    export default Participants;

    Is a bit annoying to have to add a comment, and it has to be added before every block of code, or abstract syntax tree as referred to by prettier docs, that is to be excluded from formatting.

  21. bdlowery commented on Jul 24, 2021

    @bdlowery

    @Jason-2020 I was having the same issue, and was about to have all my .css files be ignored by prettier, when I stumbled upon the ignore comment, when I was looking for a way to do it.

    THANK YOU. Now my meyer-reset doesn't look like complete shit every-time I add it to a project.

    before (disgusting)
    Pasted_Image_7_23_21__11_54_PM

    after (better)
    Pasted_Image_7_23_21__11_52_PM

  22. arimgibson commented on Aug 5, 2021

    @arimgibson

    Just a thought, has anyone tried to PR this idea? I wasn't able to find anything and 10000% understand the want to make sure a PR will be accepted before writing the code for it, but I wonder if that's the main thing holding this up. Clearly there's support for this being a setting, which also means those who are fans of the current use could still have their way. Even keep it as the default.

  23. ShahriarKh commented on Dec 8, 2021

    @ShahriarKh

    Any updates?

  24. uzbeki commented on Mar 28, 2022

    @uzbeki

    any updates? Beautify extension already does this.

  25. heppokofrontend commented on Sep 23, 2023

    @heppokofrontend

    Similar problems
    #15070

  26. lethargistic commented on Aug 14, 2026

    @lethargistic

    any updates on this in 2026?
    would highly appreciate the option

  27. bdlowery commented on Aug 14, 2026

    @bdlowery
  28. IArnaut2 commented on Nov 29, 2026

    @IArnaut2

    /* prettier-ignore */ does not work in internal CSS inside HTML.

    EDIT: Nevermind. Changed HTML formatter to Prettier.

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions