r/badcode • • Jul 26 '19

css You had one job

Post image
1.2k Upvotes

31 comments sorted by

101

u/Zambeeni Jul 26 '19

You were supposed to bring balance to the style sheet, not destroy it!

104

u/deceze Jul 26 '19

And this, kids, is why you use semantic class names, not names that describe the presentation. Your class names shall be post-title, upvote-button or content-body, not blue, right or tall.

18

u/thisimpetus Jul 26 '19

I remain kind of on the fence about this, especially with SASS making it possible to progressively build elements from simpler components; my mixins tend to describe style whereas class definitions are more likely to be semantic. Don’t suppose you know of any resources that explore this debate thoroughly? I’d like to be convinced, or at least have a deeper sense of why what’s the right call based in something other than my own intuition.

32

u/maffoobristol Jul 26 '19

I agree and disagree, it depends on the situation.

Say you have a CMS and there's a select field to say whether the text should be on the left or right, adding a class of .text-left is perfectly fine. However I am generally against defining colours in classes, although again you can only avoid that if you have truly "primary" and "secondary" button types. Some designs call for a number of variations and you can't really avoid having the classes describe the styling to some degree.

9

u/Audiblade Jul 26 '19

The example you gave is still semantic, though. The semantics are, when the user chooses the text align left option.

6

u/emeaguiar Jul 26 '19

Class name defines what, properties define how

4

u/mejicat Jul 26 '19 edited Jul 26 '19

Utility classes have their place, there’s actually a few frameworks (like Tailwind) that take that concept to 11 and allow you to write close to zero CSS. The utility classes are generated from a configuration file, so there’s no chance of misnaming like here.

Edit: here you can find an explanation of the concept and a few examples.

9

u/DCzajkowski Jul 26 '19

I couldn't disagree more.

17

u/Mzsickness Jul 26 '19

Yeah I number mine. Who uses letters?

0

u/N0W0rk Jul 26 '19

Happy cake day

2

u/Jonno_FTW shameless Jul 26 '19

.disagree-more

2

u/sac_boy Jul 26 '19

.disagree-more-more

2

u/Jonno_FTW shameless Jul 27 '19

{... !important}

1

u/dakta Jul 27 '19

!important

0

u/[deleted] Jul 26 '19

[deleted]

3

u/Tynach Jul 26 '19

This is what I came here to say. Sure I read it as 'supposed to be aligned right but they made it left' at first, but then I was like.. "Wait, this... Depends a lot on their naming convention, doesn't it?"

It could also be for the answer key to a quiz or something, and the right answer should be aligned to the left, as opposed to the wrong answer.

20

u/breadnpicklejuice Jul 26 '19

Stage right. Obviously.

1

u/SpaceshipOperations Jul 26 '19

Underrated comment.

13

u/FranzXaverWack Jul 26 '19

17

u/maffoobristol Jul 26 '19

Simply, text on the right does not mean text aligned to the right. You can have two columns, an image on the right and a photo on the left, but the text is still left-aligned within that column. Of course, that's the default, so it's pointless to define it unless it's overriding something else...

4

u/ProbablyUndefined Jul 26 '19

I mean in that case I would at least write something akin to `.right-hand` or `.right-hand-text`.

1

u/Saltman42 Jul 27 '19

I think I need to give some context to that snippet, as some commenters think this may not be that bad. tl;dr: it is.

The element in question contains text that is left-aligned on mobile and right-aligned on desktop-viewports. The screenshot is from a media-query that only targets viewports < 768px, so not mobile-first.

The .text-right class is a global utility-class with the single purpose of aligning text to the right, similar to the Bootstrap-utility-class. I just didn't add responsive utilities, with which .text-md-right would have conveyed the behaviour in a better way.

So, .text-right ,unfortunately, is not "a text-element on the right side", it was added in the prototyping phase which also was desktop-first to align the text to the right.

Then the mobile view came up, and this... thing happened. It's not a sudden change of mind of the client, we had designs for 5 different breakpoints. Instead of adding a semantic class-name to the element in question, the utility class to align text to the right was overwritten to align it to the left (and only on mobile).

However, I need to give the dev some slack, since it happened during a prototype-phase to test out some functional approaches to a problem, where proper CSS wasn't that big of a concern yet. I also adopted the code during the prototyping, so this code hasn't seen a day of production yet.

It just looked so horribly wrong in so many ways, that I had to post it here.

5

u/organically_human Jul 26 '19

text-right { text-align: center !important; }

3

u/randfur Jul 27 '19

Maybe text-right is for text on the right side of the screen that needs to be left aligned.

1

u/squidly-diddly97 Jul 26 '19

Kansas City Shuffle... When they look right, we go left...

1

u/sintos-compa Jul 26 '19

Apparently they had two jobs

1

u/[deleted] Jul 26 '19

I’m sorry can we take a moment to appreciate that 2-space indentation

1

u/[deleted] Jul 26 '19

I did this today with float-right

1

u/ccccpancakes Jul 27 '19

Possibly a css only change after client feedback and for whatever reason the markup couldn’t be edited

0

u/jocull Jul 26 '19

The equivalent of #define true false in C. Almost.