DEV Community

Discussion on: Why do some popular websites have cryptic page sources

Collapse
 
merri profile image
Vesa Piittinen

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:

<div class="css-1dbjc4n r-18u37iz r-1h0z5md">
    <div aria-label="5 Likes. Like" role="button" data-focusable="true" tabindex="0" class="css-18t94o4 css-1dbjc4n r-1777fci r-11cpok1 r-1ny4l3l r-bztko3 r-lrvibr" data-testid="like">
        <div dir="ltr" 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">
            <div class="css-1dbjc4n r-xoduu5">
                <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>
                <svg viewBox="0 0 24 24" class="r-4qtqp9 r-yyyyoo r-1xvli5t r-dnmrzs r-bnwqim r-1plcrui r-lrvibr r-1hdv0qi">
                    <g>
                        <path
                            d="M12 21.638h-.014C9.403 21.59 1.95 14.856 1.95 8.478c0-3.064 2.525-5.754 5.403-5.754 2.29 0 3.83 1.58 4.646 2.73.814-1.148 2.354-2.73 4.645-2.73 2.88 0 5.404 2.69 5.404 5.755 0 6.376-7.454 13.11-10.037 13.157H12zM7.354 4.225c-2.08 0-3.903 1.988-3.903 4.255 0 5.74 7.034 11.596 8.55 11.658 1.518-.062 8.55-5.917 8.55-11.658 0-2.267-1.823-4.255-3.903-4.255-2.528 0-3.94 2.936-3.952 2.965-.23.562-1.156.562-1.387 0-.014-.03-1.425-2.965-3.954-2.965z"
                        ></path>
                    </g>
                </svg>
            </div>
            <div class="css-1dbjc4n r-xoduu5 r-1udh08x">
                <span class="css-901oao css-16my406 r-1qd0xha r-n6v787 r-1sf4r6n r-1j6idkz r-utggzx r-d3hbe1 r-1wgg2b2 r-axxi2z r-qvutc0"><span class="css-901oao css-16my406 r-1qd0xha r-ad9z0x r-bcqeeo r-qvutc0">5</span></span>
            </div>
        </div>
    </div>
</div>
Enter fullscreen mode Exit fullscreen mode

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 with tabindex="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:

<button class="transparent-button no-focus-outline no-user-select center-content overflow-visible">
    <span class="sr-only">5 Likes. Like</span>
    <!-- more elements here -->
</button>
Enter fullscreen mode Exit fullscreen mode

Of course you'd leave out some of "pointless" styles such as center-content, no-user-select and overflow-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" /> elements

These 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 /> elements

These 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:

<li class="inline-reset">
    <button class="transparent-button flex flex-center system-font">
        <span aria-hidden="true" class="icon-container circle"><svg ... /></span>
        <span aria-label="5 Likes. Like" class="">5</span>
    </button>
</li>
Enter fullscreen mode Exit fullscreen mode

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.

Collapse
 
vzla0094 profile image
Eliezer Valenzuela

Very detailed explanation, thanks for the effort

Collapse
 
phase_seven profile image
Ratul Roy

Thank you for this detailed rundown!

Collapse
 
daveson217 profile image
David Babalola

😲😲😲. This is really enlightening. Thanks, Vesa.