I don't agree with multiple paddings / margins. Using the shorthand only when appropriate is a sign of a more experienced developer. You shouldn't be setting properties you don't actually want to set just so that you can use shorthand syntax. What if you want to change just one of the sides? You have to overwrite the whole declaration again, repeating 3 of the values just to change the 4th one.
Verbose can be better than concise where verbose is easier for humans to understand. There is sometimes too much of a drive towards concise over anything else.
Hi, my name is Kars van Iersel.
I'm a Software Engineer, located in The Netherlands. I enjoy creating high quality and delightful websites and web applications.
Thanks for your input! Ye in some cases it can be best to only do 1 value. It will be very dependable on the scenario. In general tho, I see a lot of juniors just staying from it all together.
Also a problem could arise with doing a specific value. Let's say on dekstop it's padding-top: 20px; but on mobile it's: padding-bottom: 20px; This means you would need to write a media query to make padding-top: 0, and padding-bottom: 20px; So cases like this would be better to have a 1 liner.
But I do agree that you should not blindly go into 1 liners for every scenario. In most cases it is the better option, but for sure, as you said, not in all cases.
Tech Lead/Team Lead. Senior WebDev.
Intermediate Grade on Computer Systems-
High Grade on Web Application Development-
MBA (+Marketing+HHRR).
Studied a bit of law, economics and design
Location
Spain
Education
Higher Level Education Certificate on Web Application Development
You'll need the media query anyway, and there's different ways to reach it.
From the example I'll reply it using different approaches:
1
p {
padding-top: 20px;
}
@media only screen and (max-width: $screen-sm) {
p {
padding-top: 0;
padding-bottom: 20px;
}
}
2
@media only screen and (min-width: $screen-sm) {
p {
padding-top: 20px;
}
}
@media only screen and (max-width: $screen-sm) {
p {
padding-bottom: 20px;
}
}
3
p {
padding: 20px 0 0;
}
@media only screen and (max-width: $screen-sm) {
p {
padding: 0 0 20px;
}
}
4
@media only screen and (min-width: $screen-sm) {
p {
padding-top: 20px 0 0;
}
}
@media only screen and (max-width: $screen-sm) {
p {
padding: 0 0 20px;
}
}
From this 4 options the most correct one is the second one and depending on the context I can say the fourth.
Why?
On the 1 and 3 there are unnecessary overrides. Also shorthands are less specific in CSS, so padding: 10px 5px 8px 16px; will be easily overwritten by any padding-[direction] property declaration such as padding-top: 15px;.
If you know these properties must keep like this (otherwise edit this same declarations) keep the option 2, if you are building a component and you are aware that this properties will be (or could be) context-dependant get the option 4 to avoid using !important statements and fall to padding-[direction] instead.
Knowing a language and its details is what makes a developer a Senior developer, using one think or another because it seems more trendy or fashion to you it's not.
Hi, my name is Kars van Iersel.
I'm a Software Engineer, located in The Netherlands. I enjoy creating high quality and delightful websites and web applications.
"Now, this CSS will work, but is it well written? No! You should aim to have all the margins or paddings grouped up." - says who?
A lot of this seems like subjective preferences. A developer could have patterns of habit established by previous coding environments that favor one method over another. Don't be so quick to judge someone's level of experience just because they do something different from what you think is right
Hi, my name is Kars van Iersel.
I'm a Software Engineer, located in The Netherlands. I enjoy creating high quality and delightful websites and web applications.
That is true, never judge a book by its cover! However I felt the need to write about it because it both makes the CSS cleaner and faster to change in media queries. Of course there are still cases where you don't need them in one line.
More often than not, I see the juniors use padding/margin-top, -right, -bottom, -left over 1 liners. So I figured this is something they struggle with, that is why I tried to explain as best I can about how the 1 liner is structured and how they could use it to their advantage.
Tech Lead/Team Lead. Senior WebDev.
Intermediate Grade on Computer Systems-
High Grade on Web Application Development-
MBA (+Marketing+HHRR).
Studied a bit of law, economics and design
Location
Spain
Education
Higher Level Education Certificate on Web Application Development
It's not faster to change in media queries, nothing to do with that.
For the "cleaner" statement I've to disagree, the most clean code is that one that is wrote in the manner that makes just what it needs to make.
padding-top: 20px;
is much more cleaner (and precise in computational terms -> take a look at how CSS is evaluated and rendered - painted by the browsers) than
padding: 20px 0 0;
like it or not.
If you are used to use the shorthand it's ok as long as it does not put you in trouble when needing to override something but that's all, there are 0 extra-advantages of using that.
Tech Lead/Team Lead. Senior WebDev.
Intermediate Grade on Computer Systems-
High Grade on Web Application Development-
MBA (+Marketing+HHRR).
Studied a bit of law, economics and design
Location
Spain
Education
Higher Level Education Certificate on Web Application Development
There's a bit of misconception about what cleaner means and depends on the context, but keeping with the example, using the shorthand you will be setting 4 properties when only 1 is needed which is bad from any point of view, specially when the project grows up and you need to override property values instead of declaring them, like it or not it's true computationally, and will be opinionated for human reading (unless you are a heavy user of shorthands and are used to them the padding-[direction] will be understood by any developer regardless of their knowledge level) but this second point is less important than the computational point of view one
Tech Lead/Team Lead. Senior WebDev.
Intermediate Grade on Computer Systems-
High Grade on Web Application Development-
MBA (+Marketing+HHRR).
Studied a bit of law, economics and design
Location
Spain
Education
Higher Level Education Certificate on Web Application Development
Tech Lead/Team Lead. Senior WebDev.
Intermediate Grade on Computer Systems-
High Grade on Web Application Development-
MBA (+Marketing+HHRR).
Studied a bit of law, economics and design
Location
Spain
Education
Higher Level Education Certificate on Web Application Development
selects all elements so it parses multiple times to the CSSOM, as more specific a selector is, more performance when applying it.
li+li will apply to all li parent elements of another li, where + will apply to any element being parent of whatever other element.
Hi, my name is Kars van Iersel.
I'm a Software Engineer, located in The Netherlands. I enjoy creating high quality and delightful websites and web applications.
Hi, my name is Kars van Iersel.
I'm a Software Engineer, located in The Netherlands. I enjoy creating high quality and delightful websites and web applications.
Tech Lead/Team Lead. Senior WebDev.
Intermediate Grade on Computer Systems-
High Grade on Web Application Development-
MBA (+Marketing+HHRR).
Studied a bit of law, economics and design
Location
Spain
Education
Higher Level Education Certificate on Web Application Development
Hi, my name is Kars van Iersel.
I'm a Software Engineer, located in The Netherlands. I enjoy creating high quality and delightful websites and web applications.
I don't agree with multiple paddings / margins. Using the shorthand only when appropriate is a sign of a more experienced developer. You shouldn't be setting properties you don't actually want to set just so that you can use shorthand syntax. What if you want to change just one of the sides? You have to overwrite the whole declaration again, repeating 3 of the values just to change the 4th one.
Verbose can be better than concise where verbose is easier for humans to understand. There is sometimes too much of a drive towards concise over anything else.
Thanks for your input! Ye in some cases it can be best to only do 1 value. It will be very dependable on the scenario. In general tho, I see a lot of juniors just staying from it all together.
Also a problem could arise with doing a specific value. Let's say on dekstop it's padding-top: 20px; but on mobile it's: padding-bottom: 20px; This means you would need to write a media query to make padding-top: 0, and padding-bottom: 20px; So cases like this would be better to have a 1 liner.
But I do agree that you should not blindly go into 1 liners for every scenario. In most cases it is the better option, but for sure, as you said, not in all cases.
You'll need the media query anyway, and there's different ways to reach it.
From the example I'll reply it using different approaches:
1
p { padding-top: 20px; } @media only screen and (max-width: $screen-sm) { p { padding-top: 0; padding-bottom: 20px; } }2
@media only screen and (min-width: $screen-sm) { p { padding-top: 20px; } } @media only screen and (max-width: $screen-sm) { p { padding-bottom: 20px; } }3
p { padding: 20px 0 0; } @media only screen and (max-width: $screen-sm) { p { padding: 0 0 20px; } }4
@media only screen and (min-width: $screen-sm) { p { padding-top: 20px 0 0; } } @media only screen and (max-width: $screen-sm) { p { padding: 0 0 20px; } }From this 4 options the most correct one is the second one and depending on the context I can say the fourth.
Why?
On the 1 and 3 there are unnecessary overrides. Also shorthands are less specific in CSS, so
padding: 10px 5px 8px 16px;will be easily overwritten by any padding-[direction] property declaration such aspadding-top: 15px;.If you know these properties must keep like this (otherwise edit this same declarations) keep the option 2, if you are building a component and you are aware that this properties will be (or could be) context-dependant get the option 4 to avoid using !important statements and fall to padding-[direction] instead.
Knowing a language and its details is what makes a developer a Senior developer, using one think or another because it seems more trendy or fashion to you it's not.
Amazing examples, ye that is what I ment with based on the context. Great input man! Thanks :D
"Now, this CSS will work, but is it well written? No! You should aim to have all the margins or paddings grouped up." - says who?
A lot of this seems like subjective preferences. A developer could have patterns of habit established by previous coding environments that favor one method over another. Don't be so quick to judge someone's level of experience just because they do something different from what you think is right
That is true, never judge a book by its cover! However I felt the need to write about it because it both makes the CSS cleaner and faster to change in media queries. Of course there are still cases where you don't need them in one line.
More often than not, I see the juniors use padding/margin-top, -right, -bottom, -left over 1 liners. So I figured this is something they struggle with, that is why I tried to explain as best I can about how the 1 liner is structured and how they could use it to their advantage.
It's not faster to change in media queries, nothing to do with that.
is much more cleaner (and precise in computational terms -> take a look at how CSS is evaluated and rendered - painted by the browsers) than like it or not.For the "cleaner" statement I've to disagree, the most clean code is that one that is wrote in the manner that makes just what it needs to make.
If you are used to use the shorthand it's ok as long as it does not put you in trouble when needing to override something but that's all, there are 0 extra-advantages of using that.
In his original example, the person is using margin-top and margin-bottom. In that case, I think the shorthand looks cleaner.
There's a bit of misconception about what cleaner means and depends on the context, but keeping with the example, using the shorthand you will be setting 4 properties when only 1 is needed which is bad from any point of view, specially when the project grows up and you need to override property values instead of declaring them, like it or not it's true computationally, and will be opinionated for human reading (unless you are a heavy user of shorthands and are used to them the padding-[direction] will be understood by any developer regardless of their knowledge level) but this second point is less important than the computational point of view one
That li-thing i've always done as
li+li { margin-top: 1em }.May be useful when different tags are mixed and we want spacing just between same things.
I prefer using lobotomized owls *+*
this makes your code less optimized, so in performance terms it's an issue and must be avoided
How so? What's less performant about
*+*compared to the other ways you could express it?Was unaware of the :not() selector! Wow!
I wrote an article for it, hope it helps you out! :)
Haha ye it can be a real game changer! Soon I will write a complete post about the :not() selector. You can achieve a lot of things with it
nice, i would be glad if you share more css snippets from time to time. not() was new to me.
Take a look at the guide , you'll find nice tips for sure 😄
I just wrote a dedicated article about! Hope it can help you out
Also possibly using pixels I much prefer rems :)