Stories
Slash Boxes
Comments

SoylentNews is people

posted by cmn32480 on Tuesday June 27 2017, @12:46AM   Printer-friendly
from the everything-should-be-in-plain-text dept.

Though somewhat DRY reading, the author provides salient commentary on how many declarations appear in style sheets, how many of those are unique, and offers perspectives on what that means for developing and maintaining web sites. From https://meiert.com/en/blog/70-percent-css-repetition/:

Teaser: Check on how many declarations you use in your style sheets, how many of those declarations are unique, and what that means.

In 2008 I've argued that using declarations just once makes for a key method to DRY up our style sheets (this works because avoiding declaration repetition is usually more effective than avoiding selector repetition—declarations are often longer). I've later raised the suspicion that the demand for variables would have been more informed and civilized had we nailed style sheet optimization. What I haven't done so far is gather data to show how big the problem really is. That's what I've done in the last few weeks.

In a Google spreadsheet I've collected the Top 200 of content sites in the The Moz Top 500, and taken another 20 sites related to web development for comparison. (I've also added 3 of my sites out of curiosity.) I've then used the extremely useful CSS Stats to determine the total number of CSS declarations, as well as the number of unique declarations, to calculate ratios as well as averages: You get the idea as soon as you check out said spreadsheet.

[...] Before we dissect the numbers, let's first quickly establish what one can consider good CSS development practice:

  • As with all code, don't repeat yourself (DRY).

  • In CSS, an effective way to not repeat yourself is to use declarations just once. The resulting repetition of selectors is less of a problem because declarations are usually longer than selectors—and yet variable selector length makes using declarations once a soft rule that still requires thinking. (More as follows.)

  • No repetition of declarations is theoretically attainable, but in practice, two things interfere: Not just in cases when selector order is mandated (another oft-forgotten subject, yet there are detailed proposals on how to standardize selector sorting), the cascade may not permit grouping of all selectors; and when strict separation of modules (CSS sections) is important, one may also tolerate some repetition. These two pieces deserve more elaboration, but relevant here is the note that strict separation of modules should not be used as a blanket refusal to curb declaration repetition—as the data show, that would be foolish.

Now, what is a reasonable amount of repetition? From my experience, 10–20%; in other words, 80–90% of a style sheet should consist of unique declarations. Reality, however, looks vastly different, and here we get to the 70%.

I found the analysis to be informative and eye-opening. What have you found to be best practices when it comes to Cascading Style Sheets?


Original Submission

 
This discussion has been archived. No new comments can be posted.
Display Options Threshold/Breakthrough Mark All as Read Mark All as Unread
The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
  • (Score: 2) by kaszz on Tuesday June 27 2017, @02:41AM (2 children)

    by kaszz (4211) on Tuesday June 27 2017, @02:41AM (#531710) Journal

    The problem is that people perceive the need for style and runnable code for aspects that will not improve the core purpose of mediate information.

    Starting Score:    1  point
    Karma-Bonus Modifier   +1  

    Total Score:   2  
  • (Score: 0) by Anonymous Coward on Tuesday June 27 2017, @08:55AM (1 child)

    by Anonymous Coward on Tuesday June 27 2017, @08:55AM (#531824)

    No, styling actually does improve your access to information. It is somewhat an information as well, as long as your idea of styling does not include gray on grey blinking text with bulbbing cursors and video in the background.

    • (Score: 1, Funny) by Anonymous Coward on Tuesday June 27 2017, @10:15AM

      by Anonymous Coward on Tuesday June 27 2017, @10:15AM (#531836)

      It is somewhat an information as well, as long as your idea of styling does not include gray on grey blinking text with bulbbing cursors and video in the background.

      Phew, so I'm on the clear with my grey on gray blinking text, bubbling cursors and video in the foreground, above the important content. ;-)