5

I'm in charge of supervising the whole IT side of a website (both infrastructure and development) and I'm having a difference of opinion with the lead frontend developer. His approach is to create individual classes that each bring one functionality, and combine as many classes as needed to style an element:

HTML
<div class="float_left padding_10 width_60_percent"></div>
<div class="float_left width_30_percent"></div>
<div class="float_left padding_10 width_10_percent"></div>
CSS
.float_left { float: left; }
.padding_10 { padding: 10px; }
.width_60_percent { width: 60%; }
.width_30_percent { width: 30%; }
.width_30_percent { width: 10%; }

Pros:

-understanding of CSS via the HTML: you can understand what the CSS is doing by just looking at the class names in the HTML.
-ease of creation: once you have your set of classes defined, it's just a matter of combining them in the HTML, you don't have to constantly write new CSS.
-ease of maintenance: same reason as above.
-portability: you can use the same approach for any website you work on (which makes sense for him as he is a contractor working on different projects).
-smaller CSS: as long as your layout reaches a certain critical size whereby the same classes are being re-used again and again (which would be the case with a proper layout, but obviously isn't reflected above due to the conciseness of the example).

Cons:

-bigger HTML.
-HTML harder to read if you're not interested in the CSS part (which my backend developers creating the views aren't).
-harder to integrate Javascript based on class selectors (we're using jQuery).

My approach isn't dictated by the ease of writing/maintaining CSS itself, but rather by concerns that a substantial part of our HTML code is going to be used solely for the purpose of integrating our CSS, which will impact performance: I believe it's better to have a bigger CSS which gets served and cached once, and a smaller HTML which saves you bandwidth on every page view, rather than the opposite. Accordingly, I would opt for the following approach:

HTML
<div id="column_left"></div>
<div id="column_middle"></div>
<div id="column_right"></div>
CSS
#column_left { float: left; padding: 10px; width: 60%; }
#column_middle { float: left; width: 30%; }
#column_right { float: left; padding: 10px; width: 10%; }

Pros/Cons
Opposite as above.

I have the feeling that having a lightweight HTML code is critical to achieving growth of your website:
-page load will be faster which will help with SEO and user experience.
-more pages can be served with the same bandwidth will which save you infrastructure cost.

In an attempt to get a de-facto answer, we scanned "big" websites to see what part of HTML was allocated to using ids and classes. Here is the code:

<?php
require_once 'simple_html_dom.php';
$pages = array('http://www.amazon.com', 'http://www.ebay.com', 'mywebsite.html', 'http://stackoverflow.com','facebook.html');
foreach ($pages as $page) {
    echo "\n$page\n";
    $html = new simple_html_dom();
    $string = file_get_contents($page);
    $total = mb_strlen($string);
    echo "HTML = " . $total . " characters\n";
    $html->load($string);
    foreach (array('id', 'class') as $attribute) {
        $count = 0;
        $elements = $html->find("*[$attribute]");
        foreach ($elements as $element) {
            // length of id or classes, + id="..." or class="..."
            $count = $count + mb_strlen($element->$attribute) + mb_strlen($attribute) + 3;
            //echo $element->$attribute . "\n";
        }
        echo "  -from $attribute attributes = $count -> " . round($count / $total * 100) . "%\n";
    }
}

and here are the results:

http://www.amazon.com
HTML = 101680 characters
  -from id attributes = 943 -> 1%
  -from class attributes = 6933 -> 7%

http://www.ebay.com
HTML = 71022 characters
  -from id attributes = 1631 -> 2%
  -from class attributes = 4689 -> 7%

ourwebsite.html
HTML = 35929 characters
  -from id attributes = 754 -> 2%
  -from class attributes = 2607 -> 7%

http://www.imdb.com/
HTML = 74178 characters
  -from id attributes = 1078 -> 1%
  -from class attributes = 5653 -> 8%

http://stackoverflow.com
HTML = 204169 characters
  -from id attributes = 3376 -> 2%
  -from class attributes = 39015 -> 19%

facebook.html
HTML = 419001 characters
  -from id attributes = 6871 -> 2%
  -from class attributes = 85016 -> 20%

So far we've been using "my" approach and I'm happy to see we're keeping the percentages of code related to the id andclass` attributes within the same proportions as other big websites (around 2% and 7% respectively). Nonetheless in order to validate that choice, I would like to ask the following questions that I hope experienced developers/webmasters will be able to answer:

Q1: Do you see other pros/cons in favour/against either of the 2 approaches?

Q2: From your experience, is it that critical to worry about having a lightweight HTML code, or does this become neglectable with the use of compression and other criteria (e.g. you will spend more money fighting with your frontend developers)?

By looking at the results, you will see that there seems to be a pattern in the amount of code related to the id and class attributes (although I know scanning more websites would provide a more accurate picture):
-all websites have around 1% to 2% of their HTML code related to id attributes.
-4 of the websites have around 7% of their HTML code related to classes.
-the others have around 20% of their HTML code related to classes.

Q3: Is there a technical explanation (e.g. are Facebook and Stackoverflow using the same frameworks to generate their CSS) for these patterns?

4

3 回答 3

3

我是一个企业网络软件项目的前端开发人员,大约有 50 人编写代码,其中大多数是后端。最初,我同意尽可能少的类以使代码更轻,在 css 中使用大量继承。所以我们一直在使用“尽可能少的类”的方法。

A1:使用少数类的方法,许多 css 定义将使用元素名称来添加样式,这样的事情.left-column section li a 使开发人员的生活更轻松,但实际上使 css 变慢,因为 css 是从右到左分析的。在我的示例中,浏览器首先检查所有链接,然后检查所有列表元素,依此类推。如果我只有a.lc-linkcss 使用起来会更快,要查找的元素更少。

我们遇到的另一个问题是缺乏模块化。我可以设计一个漂亮的元素,让我们称之为它.flight-dates,它工作得很好。然后出现了一个新的用例,有人想在表单中间使用,而不是作为主要页面模块。但是表单被构建为使用表单标签上的单个类,而其他所有内容都使用继承。所有继承都会污染 .flight-dates,所以我需要返回并为 css 添加更高的特异性,因此所有 .flight-dates 样式在它们是表单的子项时都有一个额外的定义。

这样的事情每周发生3到4次。更少的类 = 更少的灵活性 + 具有非常高特异性的丑陋样式表,这就是我从中得到的。

A2:随着页面上发生的 javascript数量,使用的类数量现在几乎无关紧要。它对我所知道的 DOM 操作速度没有任何影响。所以我们已经失去了从少数类方法中获得的任何优势。

参考您的测试,请记住,下载 css 和解析 css 不是一回事

此外,前面提到的 CSS 修正实际上让后端和前端开发人员的速度变慢了。因为我们试图使用尽可能少的类来使后端的工作更容易,所以如果你把它们放在一个新的上下文中,后端就会留下像婴儿一样哭泣的组件,而前端必须更改他们 6 个月前编写的 css。我个人已经浪费了几个星期。

我会说它使工作变慢,并且没有值得一提的性能优势。

A3:像 Sass 和 Less 之类的东西往往会导致 css 中的常见模式。此外,许多好的网格和相关的 css 框架都使用通用的类名。想想 jQuery ui 或 960 网格。

如果您想从我们的错误中吸取教训,请听取您的前端人员的意见。 你的后端人员会更开心,因为组件总是按照他们期望的方式运行。你的前端人员会更开心,因为他们将能够编写健壮的、即发即弃的 css,他们不必每次出现新的用例时都重新散列。你的 html 会大一点,但老实说差异可以忽略不计,我不相信它会导致任何重大瓶颈。你的 CSS 会运行得更快。工作最终将花费更少的时间来完成。

于 2012-05-02T10:41:38.390 回答
3

您的前端开发人员正在做的事情称为面向对象的 css (OOCSS),有些人认为这有助于提高您网站的速度。

我不想在这里重新讨论 OOCSS 的论点,所以请阅读这个常见问题解答:

https://github.com/stubbornella/oocss/wiki/faq

编辑:在编码标准方面,我宁愿在 facebook/stack 方面犯错,而不是 amazon/ebay。注意更进步的网站(fb/stack)似乎有更高的班级百分比?

于 2012-05-02T10:11:05.123 回答
0

我最近从 Google 看到一篇文章显然鼓励了 oocss 方法,但它几乎肯定会创建更大的 html 和 css,这在更大的网站上确实很重要。

我从未见过使用 oocss 的开发人员,这让我建议,如果该网站正在由其他开发人员开发,或者将来可能会这样做,那么采用更多开发人员遵循的方法可能会更好

于 2012-05-02T10:15:46.170 回答