I think you go a bit too far about it.
Please don’t fall in the banal rigidity about “rules”.
That’s actually wrong and impractical.
I am afraid this mindset leads to over-quality and other bad things.
We strive for the best but there is no absolute.
I particularly say that because as a learning person your code style won’t be “perfect” and that’s alright. Worrying too much about those things will actually slow you down. For you might more or less fall in the perverse mindset “perfect” or nothing. The secret sauce to waste your time. Accept being a learning person. Even myself with a few years of code, my style has room for improvement. I can clearly see the gap between me and the project manager I work with.
With that being said, I am not saying you shouldn’t care. Just go your pace
Push a bit, but not too much
I think there is at least a part that indeed do not need (from what I see) to be repeated. background-size
is the same in both parts of the code.
Now imagine you’d have an extra media query where the value would have been different. Then you must repeat it to make sure the rule is properly applied when the conditions changes.
I would say some would write it anyway “in case of” they fall in this situation because they know this property would be mandatory to work as expected (and then some would argue it does not respect YAGNI principle )
CSS is maybe not that much a language where you need to be so rigid about DRY. Especially when you seem to look at statement granularity.
CSS is not a programming language but a syntax to describe the visual aspect of a web page primarily.
There are ways to mitigate code duplication and I can see it in the very code snippet you shared.
The following factorize the property for several rulesets for instance.
h1, .tagline {
margin:0.5rem;
}
There are other technologies to produce CSS such as SASS or LESS (did not work with that last one myself). You can nest properties and IMHO it makes more sense at coding step. But thenafter the code generated (back to CSS) is likely to have repetitions anyway. And to be honest we don’t care as long as it works.
CSS allows to import other CSS files too. For instance I usually setup a CSS for constants when I work with it. Especially for colours it is handy.
Going back to the statement about granularity, it is a bit like worrying about code duplication for the following code
Consider each instance of // Any code
is different.
public void DisplayMessage1()
{
// Any code
Console.WriteLine($"This is message 1");
// Any code
}
public void DisplayMessage2()
{
// Any code
Console.WriteLine($"This is message 2");
// Any code
}
Though very close, there is no code duplication here. The messages are different. That’s it. Just like the url in the background
property is close yet different.
Hope this helps.
Cheers