Manual talk:Coding conventions/CSS/Archive 1

Comments about colors
Does it make sense to anybody but me to demand writing comments about numbered color values in CSS? --Amir E. Aharoni (talk) 19:35, 25 July 2012 (UTC)


 * Everyone who writes CSS probably knows what #000 and #FFF stand for, but I for one don't know that #C71585 stands for "MediumVioletRed" without looking it up on the English Wikipedia (or some other CSS resource). So yes, I wholeheartedly support using comments to write out the color name in plain English. After all, comments are cheap, just like space. --Jack Phoenix (Contact) 13:55, 26 July 2012 (UTC)

LESS
The LESS section is more a guide about LESS than a coding conventions set of guidelines.

Someone has an objection to move most of the section to a Manual:LESS page? --Dereckson (talk) 21:32, 30 October 2013 (UTC)

Prefix mixins with mixin- ?
It's hard to distinguish .foo { } with .foo { .foo; } especially if you are new to LESS. I think it would be in everyones interest to prefix all mixins with mixin- to help with readability and to place mixins at the top of a file. e.g. .mixin-foo { } .foo { .mixin-foo; }

Thoughts? Jdlrobson (talk) 22:25, 7 August 2014 (UTC)
 * +1. You've convinced me this is a good idea. I don't have a strong preference on the prefix, or whether mixins have to go in the top of the file.  But it seems like a useful convention overall. Superm401 - Talk 00:03, 9 September 2014 (UTC)

Add spaces around mixin parameters ?
I'm not sure about one of the existing code conventions. I think it is useful to be able to more clearly distinguish between and In the second case we are dealing with a mixin and I would recommend that using spaces would make it clearer. e.g.

Just a suggestion. Jdlrobson (talk) 15:57, 9 April 2014 (UTC)


 * I think that's a very good idea and that, because ResourceLoader is minifying the files anyways, we should also discuss about applying whitespace for CSS property values with brackets like URIs to align CSS brackets notation with PHP and JS Coding Conventions.
 * Citing from W3C – "The format of a URI value is 'url(' followed by optional whitespace followed by an optional single quote (') or double quote (") character followed by the URI itself, followed by an optional single quote (') or double quote (") character followed by optional whitespace followed by ')'. The two quote characters must be the same." Resulting in:
 * --VEckl (WMF) (talk) 14:53, 21 July 2015 (UTC)
 * Agreed, let's update. ESanders (WMF) (talk) 18:19, 2 October 2015 (UTC)
 * --VEckl (WMF) (talk) 14:53, 21 July 2015 (UTC)
 * Agreed, let's update. ESanders (WMF) (talk) 18:19, 2 October 2015 (UTC)

Less and future CSS variable naming convention
We are currently missing a naming convention for Less and somewhen future CSS variables (custom properties). We already have conventions for PHP variables or JavaScript variables. It's time to agree on and implement a CSS/Less variable naming convention.

Throughout projects there's currently a pretty wild mix, for example in Vector:

or mediawiki.ui:

or Minerva:

A new proposal
The duplication of property name and property as variable name in applied property values is IMHO verbose and redundant. I'd propose to take popular and widely-used Emmet (section CSS) as base for shortening of property into consideration. Variables are (with the very exception of calculations and size values) almost always belonging to a certain CSS property, therefore the property doesn't need to be repeated. Even when the variable's value change, it will always be connected to a certain property!

When it comes to the variable names, I'd suggest to follow the same formatting regimen like CSS' built-in and the CSS vars standards document draft and use  as separators. Although camelCase is used across PHP and JS coding conventions, it's not common in CSS. Here's some more food for thought. To the structure of the variable naming, I think it's a good idea, to follow the HTML5 classification, so no, but   and.

CSS namespaces useful?
Naming is hard and designing CSS for scalability is even harder. CSS namespaces (example) have in my experience a great potential to gain speed working with disperse people and projects on front-end. — Preceding unsigned comment added by VEckl (WMF) (talk • contribs) 04:43, 12 September 2015 (UTC)
 * Would "Hungarian notation for CSS" be an appropriate TL;DR summary of that link? Anomie (talk) 13:24, 14 September 2015 (UTC)
 * Unlike in the CSS-/Less variable naming proposal, I would actually not advocate for abbreviations like in the csswizardry example. Because MW is for the broadest range of developers often with very minimal CSS/Less experience. And abbreviations make development for the inexperienced harder. See f.e. current side-navigation in Vector:
 * Looking into  I see ID  . At that point I can just widely guess what p stands for. The next sibling   has   and   applied. So is p an abbreviation for portal then? But wait,   is outside of it, so maybe it's something else…   probably?
 * But, I would wanna see clearer naming patterns in CSS handlers (predominantly classes) to speed up development for front-end devs. As an example instead of
 * better:
 * Might look verbose/not too inviting in the beginning, but speeds up orientation and clarification code-wise in CSS modules impactfully from my experience.
 * Be cautious, that this is result of a very short code review process, I haven't looked into the JS connected with #centralNotice, nor at which places it is used. As it's an ID this might be the only place of it being in use.
 * To explain in a few words, I'm following BEM (Block Element Modifier) notation here, a  signifies a child (an Element) of the Block. And   signifies a Modified state of the Element.
 * With that I can easily separate CSS styles for elements and denote in another rule very few modifier specifics. In JS I can easily care about getting/setting modified states only.
 * I'm aware that this is a big conversation with lots of dependecies and problems. And it will not help to come up with another standard -- ahem, approach besides the historically grown and therefore understandable, but still horrifying mix of ID/class naming variations out there. --VEckl (WMF) (talk) 19:03, 6 November 2015 (UTC)
 * With that I can easily separate CSS styles for elements and denote in another rule very few modifier specifics. In JS I can easily care about getting/setting modified states only.
 * I'm aware that this is a big conversation with lots of dependecies and problems. And it will not help to come up with another standard -- ahem, approach besides the historically grown and therefore understandable, but still horrifying mix of ID/class naming variations out there. --VEckl (WMF) (talk) 19:03, 6 November 2015 (UTC)

Gzip heatmap of lowercase color values
Out of curiosity I wanted to see for myself how lowercased, shorthand notated color values of 2015-08's Vector CSS would look like in gzthermal heatmap, mentioned on color property section on the guidelines article:

Gathering ideas of what base unit values to use where/when?
Copy from IRC for now. Will work on some ideas soon. 01:05  we're talking about the content's font-size? 01:05  in which case, it's already compatible. it works on Vector and MonoBook, which have different font-size 01:05  if we're talking about the font-size for VE's own UI, then no one has tried yet, it's still using 0.8em (=12.8px) on both Vector and MonoBook, afaik [...] 01:06  (MonoBook uses 0.8em for content, Vector uses 0.875em=14px for content) 01:09  MatmaRex: trying to experiment with ideas getting out of the for me not-satisfying pixel rounding of applied 'em's 01:10  Volker_E: oh, hmm 01:10  Volker_E: i'm not sure if you realize, but thesituation there is far more interesting 01:10  :D 01:11  MatmaRex: in which way? :} 01:11  Volker_E: the MediaWiki theme of OOjs UI is currently developed with font-size of 0.8em (12.8px) in mind 01:11  since that was the font-size of Vector when it started to be developed 01:11  then font-size of Vector was changed, but the theme did not get updated. 01:12  so right now, in the demo, we still have 0.8em, but in the actual MediaWiki, we have 0.875em. this is easily noticeable as blurry icons (unless you have an absurdly retina screen) 01:12  MatmaRex: yeah, great scheme of hard-to-maintain dependency failures 01:12 — Volker_E shudders 01:13  but not in VisualEditor! it has some custom CSS to make the font-size inside its UI 0.8em, because no one wanted to update the theme itself to 0.875em 01:13  this was not a problem, until we started using the theme outside of VE [...] 01:13 <MatmaRex> but if we try to fix the theme now, VE will look weird, so we have to update it simultaneously 01:13 <James_F> Ideally we should make everything use a font-size of 1em and just change what the base font size is. 01:14 <James_F> (Heresy!) 01:14 <MatmaRex> and then other things will no doubt break. Flow also uses OOUI, and also has font-size hacks, but only in some places, because it was done inconsistently [...] 01:14 <Volker_E> Ideally would be relying on `rem` but as we have no superpowers people come up with weirdest default settings 01:14 <Volker_E> *we would 01:15 <MatmaRex> the only real problem with the font-size changing under us is that icons become blurry, evertything else should scale fine 01:15 <MatmaRex> but the nested hacks upon hacks are a problem, the last time we touched it, in VE, inspectors inside dialogs balooned to twice the usual size for some reason 01:15 <Volker_E> MatmaRex: I disagree, there are (due to shortcomings in browser rendering engines) several issues around pixel rounding errors 01:16 <Volker_E> MatmaRex: like box-shadows, or border-radius weird effects 01:16 <MatmaRex> oh yeah, that's true, but i personally think that is just hopeless. :) 01:17 <Volker_E> MatmaRex: what hacks upon hacks are you referring to? 01:18 <Volker_E> MatmaRex: hacks to get inherited em in balance again? 01:18 <MatmaRex> yes 01:18 <MatmaRex> in VE, modules/ve-mw/init/styles/ve.init.mw.DesktopArticleTarget-vector.css and similar 01:19 <MatmaRex> Volker_E: there is at least one bug in VE caused by that, if you look closely, the page heading/title is smaller in edit mode. 01:20 <Volker_E> MatmaRex: for a UI (here a theme) where I have full control about root elements `rem` is my best bet from current perspective 01:21 <Volker_E> MatmaRex: for an extension it's much more difficult 01:21 <Volker_E> MatmaRex: did anyone ever look into using px for all browsers aside from IE 01:22 <MatmaRex> Volker_E: no idea. 01:22 <Volker_E> MatmaRex: which would mean that modern zoom applies in modern browsers 01:22 <Volker_E> MatmaRex: but in text-zoom only mode you're targeting size adaption with em... 01:24 <MatmaRex> Volker_E: i don't know. the decision to use ems predates me, it looked reasonable to me and i never questioned it. if anyone knows more about this, it's Trevor 01:26 <Volker_E> MatmaRex: ok, hopefully I've got enough time to prepare some historical anecdotes, code examples and pro&con summaries. ;) That should prob get on the next FE std group list. 01:26 <Volker_E> MatmaRex: Thanks for the insights!

How much of this is actually true?
Looking at core and what seem to be best practices in current extensions and skins, a lot of this just doesn't apply, and sometimes for very good reason. So how much of these conventions are, er, actually correct? -— Isarra ༆ 17:42, 30 October 2015 (UTC)
 * What are "sometimes very good reasons" for not applying on which of the topics? Knowledge and best-practices have to be shared before being applied. And even if they are shared, on best-practices like color name guidelines, there's currently no strict enforcement of those conventions. If you find problems within the current conventions, here on the talk page seems to be a good place to raise them. --VEckl (WMF) (talk) 04:39, 5 November 2015 (UTC)
 * So... this page is supposed to be real? -— Isarra ༆ 07:38, 5 November 2015 (UTC)
 * Well, you know how it is with reality and its perception… it's a wide field. But yes, that's IMHO the place to raise general issues/ideas on best approaches/guidelines around CSS/Less outside of Phabricator. --VEckl (WMF) (talk) 01:28, 6 November 2015 (UTC)

Okay, some things:


 * ''"One space after a starting and before an ending parentheses (( and )) in selectors (ex. :not) and properties (ex. rgba)."
 * This does not apply to any core or extension code I have seen. Why does core not follow this/does this differ here?
 * Yeah it seems like an extension of our 'liberal use of whitespace' convention in JS, but this specifically it's not a current practice anywhere. But I don't mind making it one really. (DJ)


 * ''"Quotes are unnecessary in the url parameter"
 * They may be unnecessary in most contexts, but they are simpler to do and a more consistent solution to strange characters, and are also useful for syntax highlighters. Is there any reason to use quotes?
 * In my experience, single quotes are most commonly used.
 * This is a pre-existing practice and followed reasonably well. (DJ)


 * ''"Repeat the same selector to increase weight, like .foo.foo.foo.foo"
 * ''"Add or repeat attribute selectors, like [class]."
 * Don't do this. This is bad. Even worse than having generally unnecessarily specific selectors in the first place. Which is also bad. (Every subsequent level needs to override it in an even worse fashion than the previous. Don't start something horrible that will then need to be continued... say, on-wiki.)
 * No fan of [class] either, doubling up classnames, I sort of can appreciate as an elegant hack if you want to avoid having to refer to ancestors. (DJ)


 * ''"No quotes around parameter values (same as for url( image.png ) in CSS)"
 * This is important in order to avoid breaking the parameters, not because it is unnecessary.
 * Haven't looked into this myself (DJ)


 * ''"Mixin names ... should be prefixed with mixin-"
 * No. We do not prefix function names with 'function' in any language; this is inappropriate with mixins too. The syntax is similar for classes, but not the same.
 * I have no objections to this. It's too much praise to call the less syntax a function call here in my opinion. I'd appreciate them standing out more.


 * ''"If you need to call a mixin with one or more arguments that contain a comma use a semicolon to separate the arguments"
 * This is inconsistent and also partially incorrect: it arbitrarily differs from normal practice (using a comma), and also requires a semicolon at the end as well to actually work when only using a single parameter. Either we should be using semicolons everywhere, or not recommending this at all. Optional parameters are a thing.
 * YUCK YUCK YUCK, this really is the best that less has to offer ? (DJ)

-— Isarra ༆ 03:01, 10 November 2015 (UTC)
 * Original points and opinions by Isarra, 2nd level opinions by —Th e DJ (Not WMF) (talk • contribs) 13:35, 20 November 2015 (UTC)