Hacker News 中文摘要

RSS订阅

CSS糟糕只因我们懒得学(2022) -- CSS sucks because we don't bother learning it (2022)

文章摘要

文章指出,人们常抱怨CSS难用,但根本原因在于没有真正花时间系统学习它。作者认为,与其一味指责CSS设计糟糕,不如静下心来深入学习,掌握后就不会再抱怨了。这与其他编程语言的学习过程类似,关键在于投入时间理解其原理。

文章总结

我们抱怨CSS难用,却从未真正学习它

作者:Ibrahim Diallo
发布日期:2022年3月24日

每当听到有人抱怨CSS多么糟糕时,我总想问:你真的系统学习过CSS吗?作为过来人,我注意到一个现象——人们总是固执地认为"CSS就是烂,是糟糕的设计者留下的烂摊子"。但事实上,这种评价可以套用在任何编程语言上。

前端开发的认知误区

后端开发需要多年积累:程序员构建复杂应用时,往往忽略了用户界面的呈现方式。就像Peter Norvig在《十年学会编程》中强调的,掌握编程语言远不止于语法,更需要建立解决问题的思维模型。然而奇怪的是,我们总把CSS当作可以临时抱佛脚的"简单题"。

CSS的真相

  1. 学习曲线被低估:虽然CSS不是传统编程语言,但解决设计问题同样需要系统学习和经验积累
  2. 历史包袱的误解:人们常吐槽CSS2时代居中对齐的困难,却忽略了自己糟糕的HTML结构才是根源
  3. 滥用工具的恶果:通过谷歌拼凑代码、滥用!important和绝对定位,最终只会制造难以维护的烂摊子

专业建议

  • 系统学习:参加专业课程,理解CSS的设计哲学
  • 克制使用危险属性:比如position:absolute应该成为最后手段而非默认选择
  • 承认不足:CSS确实存在缺陷(比如伪元素的content属性),但理解原理后就能合理规避

文章最后强调:与其抱怨,不如花时间系统学习。CSS比传统编程语言更易上手,但前提是你愿意真正了解它。

(移除了原文中的导航菜单、推荐文章、评论区等非核心内容,保留主要论证逻辑和关键案例)

评论总结

以下是评论内容的总结:

批评CSS的观点:

  1. 复杂且不断变化:CSS标准不断更新,难以跟上最新变化和浏览器兼容性。

    • "the 'it' you have to learn has constantly been a moving target. That's its biggest problem." (busterarm)
    • "CSS is complicated. Second, it changes constantly. Third, there are no widely-used conventions and best practices." (phoronixrly)
  2. 设计不合理:语法和规则反直觉,存在大量“hacks”。

    • "Why do you think the padding as a percentage is relative to the width of the parent? ... it was purposefully put there in the specs so that we could have boxes with fixed aspect ratios." (throwawayffffas)
    • "CSS also sucks because 10px isn't 10 pixels, 10cm isn't 10 centimeters, and 50% isn't 50% of the screen." (TZubiri)
  3. 学习曲线陡峭:需要记忆大量规则和例外,缺乏直观性。

    • "I can never remember any of it when I need it. Contrast with html, which I first learned in 95 and have never forgot the basics of." (dec0dedab0de)
    • "Learning it feels like work." (SilentM68)

为CSS辩护的观点:

  1. 功能强大:尽管复杂,但能实现高度灵活的布局和设计。

    • "The good thing is that all the hacks together make a working thing. And if you know them you can do a lot of stuff." (throwawayffffas)
    • "there’s a deeply elegant language hidden in there with unbelievable support for generic programming." (css_apologist)
  2. 学习方法很重要:通过系统学习和实践可以掌握。

    • "If you want to learn CSS, and I mean, REALLY learn it, buy 'CSS: The Definitive Guide', read it cover to cover..." (miiiiiike)
    • "The only way to really get comfortable with all of that is to do actual projects, not just tutorials." (kevinsync)

其他观点:

  1. 工具替代:AI和现代工具(如Tailwind、MUI)减少了直接编写CSS的需求。

    • "AI does CSS well!" (kbrisso)
    • "Look at how easy MUI's Grid is... Atomics vs Usability. Everyone needs CSS. No one needs to write CSS." (ukuina)
  2. 与编程语言不同:CSS更像是一组规则而非编程语言。

    • "CSS to me seems like a pile of rules, rather than something with control flow." (drcongo)
    • "It's like comparing building a sandcastle with building a house." (lbrito)

总结:CSS因其复杂性、不一致性和学习难度受到批评,但也因其强大功能和灵活性被部分开发者认可。现代工具和AI正在改变人们使用CSS的方式。