I recently decided to inspect the page sources of some popular sites to check out the CSS used on the avatars. I noticed two things in particular:
- The class names used are just cryptic. They just have random letters e.g.
s-v7te9w
. - They use so many divs and other tags to achieve a desired effect (like avatars).
In contrast, Dev.to does not have cryptic class names (e.g crayons-avatar__image
), and also does not use many tags to achieve its own avatar(for instance) styling.
These are my questions:
- Why are the class names so cryptic? Is it because of a framework? Are they dynamically generated?
- Why are there so many divs(and other tags) just to do one task? Aren't they unnecessary?
Thanks a lot 😁
Top comments (15)
The many divs are a result of abstracting every little detail into their own element or class. Taking Twitter's like button as an example:
Digesting this is an awful lot of work, but here goes!
<div class="css-1dbjc4n r-18u37iz r-1h0z5md" />
One element in a group of many similar ones (outer
role="group"
div is missing here). There are multiple different renders of this "component" which is why there are more than one class. In BEM with CSS utilities this would have been something like<li class="like-list__item flex-start flex-direction-row">
.<div class="css-18t94o4 css-1dbjc4n r-1777fci r-11cpok1 r-1ny4l3l r-bztko3 r-lrvibr" />
The second div appears to be dedicated to accessibility and functionality, and re-implements HTML's native
<button>
element. It is easier to style a div than a button so that is probably the reasoning for the re-implementation. They've had to add custom event handlers to match native button behavior (such as enter and space working like a click).data-focusable="true"
is probably somewhat unnecessary workaround to something, the true functionality of tabbability is achieved withtabindex="0"
. This also only exists because of not using a button element.Targetting elements when everything is a div is hard they also use
data-testid="like"
so that they can find the element in their tests.Equivalent semantic HTML could be something like:
Of course you'd leave out some of "pointless" styles such as
center-content
,no-user-select
andoverflow-visible
as these don't have real effect to styles in a native button.Whether to use
aria-label
or element with a screen reader accessibility class is a matter of taste.<div class="css-901oao r-1awozwy r-1re7ezh r-6koalj r-1qd0xha r-a023e6 r-16dba41 r-1h0z5md r-ad9z0x r-bcqeeo r-o7ynqc r-clp7b1 r-3s2u2q r-qvutc0" />
This element appears to be a generic layout component. The first class defines a few resets while all the other classes are single purpose utilities that control things such as font-family, font-size, white-space, transitions, flexbox, and so on.
In a normal HTML approach this element would not exists at all and button would have all the needed classes.
Two
<div class="css-1dbjc4n r-xoduu5" />
elementsThese are child elements for the generic layout element. Essentially contains a lot of style resets. The first element contains icon, and the second element contains the number of likes.
<div class="css-1dbjc4n r-sdzlij r-1p0dtai r-xoduu5 r-1d2f490 r-xf4iuw r-1ny4l3l r-u8s1d r-zchlnj r-ipm5af r-o7ynqc r-6416eg"></div>
This element creates a circle that is placed behind icon. You can see it transitioning when liking a post. The circle is created by combining many classes.
<svg />
It's the icon!
<span />
elementsThese two elements wrap the number. Again contain a lot of style resets and then use of single purpose classes some of which grant duplicate styles that already exist.
How could it be written?
If written more from the point of HTML and CSS things could look like this:
It is an oversimplification as far as used classes go, but the reduction of HTML elements is notable and the same visual end result can be achieved with just this.
Twitter ends up with what they have for multiple reasons, for example they can have productive devs working on the frontend who have no knowledge of CSS. They can keep total amount of CSS somewhat minimal even if there are classes or styles that are technically unnecessary. The price they pay is the need of re-implementing native browser features and the resulting DOM/HTML is verbose.
Very detailed explanation, thanks for the effort
Thank you for this detailed rundown!
😲😲😲. This is really enlightening. Thanks, Vesa.
I would encourage you to take a look at the HTML output from tools like Adobe Dreamwever or other similar WYSIWYG HTML editors. It's quite often horrendous unintelligible code that's functionally useless for anything except being used in the editor that generated it or displayed in a web browser.
As a general rule, when you see that type excessive tag usage, it's a code generator of some sort that's to blame. Sometimes you do need a lot of
<div>
elements (see for example Bootstrap dialogs, they actually do need all the involved<div>
's to properly handle styling in a uniform manner), but most of the time it's a case of lazy code generation, not actual need.Styles are often a more complicated case to concretely diagnose though, but most cases seem to be people trying to circumvent some of the behavior of CSS (usually because they don't understand how to actually use it to do what they want in a much more efficient way).
Thanks, Austin. I would check it out.
One added benefit is to make it diffult for the script kiddies to write automation tools to extract data or automate spamming behaviors.
yeah I've always figured obfuscation was the most likely motivation. I've seen what WYSIWYG code and the like turns out like, and that's probably also in large part buy to how dynamic they need to be, but it also occurred to me that if they shipped any html to potential customers that was relatively easy to reuse/rework/customize/manipulate while maintaining functionality they may have a hard time selling anything.
for twitter and other big name tech its hard to for me to attribute much probability to inexperience, laziness or anything unintentional. although I guess I could be wrong about that. seems to me its mostly abstraction and optics. trying to make it seem like way more then it is just ends up being way more in the end. or they have some kind of dope unreleased tech that explains in part for some of the gib.
😀😀😀
Good point
I am actually trying to parse a table that is made entirely of Divs like in the example above.
What would you recommend to achieve that goal ? Is there any tool that could help me ?
Thanks again Kieran. I surely would love the book(The Lean Web). I'm a fan of keeping things simple 😀
🤔 Thanks a lot
Obfuscation
🤔🤔🤔 Thanks, John
Thanks, Kieran. I now get it.