4
Fine tune which is the right practice, use CSS with Shorthand or Longhand? I looked, but I couldn’t find a definitive answer...
When we declare the class with Shorthand we are saying that all other values should be set as initial
. I will demonstrate just a few situations that illustrate my interest and doubt.
A value which is not specified is set to its
initial
value. Source: https://developer.mozilla.org/en-US/docs/Web/CSS/Shorthand_properties
Just when we declare our BG color like this:
div {
background: red;
}
We’re actually saying, or at least that’s how the browser is reading it, this:
div {
background-image: initial;
background-position-x: initial;
background-position-y: initial;
background-size: initial;
background-repeat-x: initial;
background-repeat-y: initial;
background-attachment: initial;
background-origin: initial;
background-clip: initial;
background-color: red; /* mas eu só queria o bg :) */
}
- That’s good for performance?
- It is best to declare all values individually or let Browser process all values that we are not declaring?
Possible problems with Shorthand (one more example with background)
Example of simple problem of shorthand with background
making override classes defined by the default component.
.base-class {
height: 50px;
width: 100%;
background-position: 0 0;
background-repeat: no-repeat;
}
.base-class--modifier {
background: radial-gradient(...)
}
<div class="base-class base-class--modifier"></div>
Should be used background-image: radial-gradient(...)
Today as the Emmet and software that already has autocomplit almost everything is still worth writing CSS with shortcuts? Or is it better to be more concise in style statements?
Is there any good practice? Considering performance, maintainability, milkability, etc
Reference links:
Why does there need to be a "right practice"? CSS offers both options, to be used as is most convenient in each case.
– bfavaretto
@bfavaretto am asking pq for example in the style guide of Google they encourage the use of Shorthand, whereas Mozilla seems to prefer Longhand. If to use on a website single page better the CSS shorts or***long***, or if it’s to a web application with a lot of people working on the project it’s better shorts or long? Often it’s not just up to us to choose. So I asked you if you have any more correct practice in the world of development or if each of you who put your hand does the way you want it...
– hugocsl
It’s not "everyone does as they please," it has to have discretion. I’m thinking of a possible answer for you. I found Google reference on shorthand, but not the Mozilla that you say you prefer longhand. Has a link?
– bfavaretto
@bfavaretto Direct link on the Mozilla website: https://developer.mozilla.org/en-US/docs/Web/CSS/Shorthand_properties (attention on Tricky edge cases). CSS Guide: http://codeguide.co/#css-shorthand . I’d like to follow a pattern, but it doesn’t look like... In large projects it complicates for everyone. That’s why I was looking for a more consistent practice. But perhaps "personal taste" is the current answer although not yet the best solution.
– hugocsl
It is more common sense than personal taste. Unfortunately I have no time to write a more elaborate answer. But the basic principle is to be economical. I would follow what Google says, taking care of edge cases like the ones pointed out on the Mozilla website. In fact I do not think that there they take sides for always using longhand, only point out the cases where being economic can cause problems.
– bfavaretto
From what I read from the first link in the question: you should use the short hand to save time and energy (maybe even gain readability). Mozilla’s people then prefer the long hand when they need a more granular control of the properties. Thicker control would be with short hand
– Jefferson Quesado