Repository navigation
Open
Labels
lang:css/scss/lessIssues affecting CSS, Less or SCSS
Description
Prettier 1.14.2
Playground link
--parser babylon
--no-semiI 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 ParticipantsOutput:
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 ParticipantsExpected behavior:
No insertion of line breaks in CSS selectors of length less than, say, 80.
Reacted by Jed Fox, Steven Psomas, Roman Morozov, Damian Othar, Aravind Nair, Alvaro Fernandez, Jonathan del Strother, Ryan Leichty, metapone, Jason-2020 and 27 more
Metadata
Metadata
Assignees
Labels
lang:css/scss/lessIssues affecting CSS, Less or SCSS
Activity
j-f1 commentedon Sep 13, 2018
Perhaps we shouldn’t break if all of the sectors are bare element selectors.
lydell commentedon Sep 13, 2018
@j-f1 Sounds reasonable. Thinking back of the previous issues, I think that’s the primary use case.
janosh commentedon Jan 20, 2019
Just out of curiosity, was this issue addressed by any PR?
lydell commentedon Jan 20, 2019
@janosh No PR for this so far!
j-f1 commentedon Mar 5, 2019
Let’s go with the heuristic of not automatically breaking selectors that consist of just element selectors.
syntag commentedon Mar 5, 2019
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-lenas referenced here #5937It can sometimes be more preferable to read/write:
as opposed to:
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:
as opposed to:
lydell commentedon Mar 5, 2019
I think we should keep things as they are. Keep it simple! As of this writing, there are no 👍s on this issue.
alexander-akait commentedon Mar 5, 2019
I think we can close issue, printing in one line unreadable
syntag commentedon Mar 5, 2019
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.
lydell commentedon Mar 6, 2019
Feel free to provide more examples! 👍
14 remaining items
fisker commentedon Aug 22, 2019
Code Guide suggest:
https://codeguide.co/#css
airbnb/css
https://github.com/airbnb/css#selectors
stylelint-config-standardhashttps://github.com/stylelint/stylelint-config-standard/blob/5d1531025526324abdfc6d1fbf40b5fb9f2d7c2f/index.js#L95
normalize.cssfollows the same stylehttps://github.com/necolas/normalize.css/blob/fc091cce1534909334c1911709a39c22d406977b/normalize.css#L125-L126
bootstraphttps://github.com/twbs/bootstrap/blob/66e9ab3e4dd0a5fe501e8499e45bbd7488b80fd0/scss/_reboot.scss#L14-L16
https://github.com/twbs/bootstrap/blob/ecaefed92043fa889a0fb4c6100450ca6c29e9ef/site/static/docs/4.3/assets/scss/_component-examples.scss#L140-L145
aravindvnair99 commentedon Nov 25, 2019
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
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.
Jason-2020 commentedon May 26, 2020
Is there any workaround?
nickbclifford commentedon Mar 4, 2021
Hello from 2021! Have there been any updates or workarounds for this since the last post?
janosh commentedon Mar 8, 2021
@nickbclifford It seems unlikely that Prettier will change its behavior here. But a simple workaround is to use the CSS pseudo-classes
:isand:where(they only differ in specificity).Prettier will not break inside these selectors:
If your selectors have a common parent or child, I wouldn't even consider this a workaround but the new best practice:
Old CSS
New CSS
Browser support is 90% for
:isand 81% for:whereso unless you still care about IE, you can start using at least:isright away. 😎shirobachi commentedon May 1, 2021
It looks like I've the same problem..
I've
Prettier make:
I want to have:
Is it possible?
AlexandroPerez commentedon Jun 15, 2021
@Jason-2020 I was having the same issue, and was about to have all my
.cssfiles 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:
@janosh and the following in javascript:
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.
bdlowery commentedon Jul 24, 2021
THANK YOU. Now my meyer-reset doesn't look like complete shit every-time I add it to a project.
before (disgusting)

after (better)

arimgibson commentedon Aug 5, 2021
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.
ShahriarKh commentedon Dec 8, 2021
Any updates?
uzbeki commentedon Mar 28, 2022
any updates? Beautify extension already does this.
heppokofrontend commentedon Sep 23, 2023
Similar problems
#15070
lethargistic commentedon Aug 14, 2026
any updates on this in 2026?
would highly appreciate the option
bdlowery commentedon Aug 14, 2026
yes. for 2026 these are your best options: https://openai.com/codex/ or https://code.claude.com/docs/en/overview#get-started or https://cursor.com/
Then you won't need to worry about these inconsequential problems anymore
IArnaut2 commentedon Nov 29, 2026
/* prettier-ignore */does not work in internal CSS inside HTML.EDIT: Nevermind. Changed HTML formatter to Prettier.