Why are dashes preferred for CSS selectors / HTML attributes?

I use dashes because I don't have to hit the shift key.

Code completion

Whether dash is interpreted as punctuation or as an opaque identifier depends on the editor of choice, I guess. However, as a personal preference, I favor being able to tab between each word in a CSS file and would find it annoying if they were separated with underscore and there were no stops.

Also, using hyphens allows you to take advantage of the |= attribute selector, which selects any element containing the text, optionally followed by a dash:

span[class|="em"] { font-style: italic; }

This would make the following HTML elements have italic font-style:

<span class="em">I'm italic</span>
<span class="em-strong">I'm italic too</span>

Ambiguity with arithmetic operator

I'd say that access to HTML elements via dot notation in JavaScript is a bug rather than a feature. It's a terrible construct from the early days of terrible JavaScript implementations and isn't really a great practice. For most of the stuff you do with JavaScript these days, you'd want to use CSS Selectors for fetching elements from the DOM anyway, which makes the whole dot notation rather useless. Which one would you prefer?

var firstName = $('#first-name');
var firstName = document.querySelector('#first-name');
var firstName = document.forms[0].first_name;

I find the two first options much more preferable, especially since '#first-name' can be replaced with a JavaScript variable and built dynamically. I also find them more pleasant on the eyes.

The fact that Sass enables arithmetic in its extensions to CSS doesn't really apply to CSS itself, but I do understand (and embrace) the fact that Sass follows the language style of CSS (except for the $ prefix of variables, which of course should have been @). If Sass documents are to look and feel like CSS documents, they need to follow the same style as CSS, which uses dash as a delimiter. In CSS3, arithmetic is limited to the calc function, which goes to show that in CSS itself, this isn't an issue.

Inconsistency with variable naming across languages

All languages, being markup languages, programming languages, styling languages or scripting languages, have their own style. You will find this within sub-languages of language groups like XML, where e.g. XSLT uses lower-case with hyphen delimiters and XML Schema uses camel-casing.

In general, you will find that adopting the style that feels and looks most "native" to the language you're writing in is better than trying to shoe-horn your own style into every different language. Since you can't avoid having to use native libraries and language constructs, your style will be "polluted" by the native style whether you like it or not, so it's pretty much futile to even try.

My advice is to not find a favorite style across languages, but instead make yourself at home within each language and learn to love all of its quirks. One of CSS' quirks is that keywords and identifiers are written in lowercase and separated by hyphens. Personally, I find this very visually appealing and think it fits in with the all-lowercase (although no-hyphen) HTML.

Why are dashes preferred for CSS selectors / HTML attributes?的更多相关文章

  1. BeautifulSoup高级应用 之 CSS selectors /CSS 选择器

    BeautifulSoup支持最常用的CSS selectors,这是将字符串转化为Tag对象或者BeautifulSoup自身的.select()方法. 本篇所使用的html为: html_doc ...

  2. 转:SELENIUM TIPS: CSS SELECTORS

    This page will show you some CSS rules and pseudo-classes that will help you move your XPATH locator ...

  3. (3)选择元素——(4)css选择器(CSS selectors)

    The jQuery library supports nearly all of the selectors included in CSS specifications 1 through 3, ...

  4. CSS selectors for Selenium with example,selenium IDE

    CSS selectors for Selenium with example http://seleniumeasy.com/selenium-tutorials/css-selectors-tut ...

  5. [Selenium] 在Chrome的开发者工具中验证检查XPath/CSS selectors

    Evaluate and validate XPath/CSS selectors in Chrome Developer Tools Method 1 : From Elements panel U ...

  6. css selectors tips

    from https://saucelabs.com/resources/articles/selenium-tips-css-selectorsSauce Labs uses cookies to ...

  7. CSS Selectors

    CSS selectors are used to "find" (or select) HTML elements based on their element name, id ...

  8. flight.Archives001 / CSS Selectors选择器

    Title/CSS选择器 序 : 这是flight.Archives 梦开始的地方, 作者我熬夜肝出来了这篇文章... 保证这是最简洁高效的 CSS Selectors 教程 Note : 暂时没有能 ...

  9. Review CSS Selectors

    Throughout this lesson, you learned how to select HTML elements with CSS and apply styles to them. L ...

随机推荐

  1. 109、Secret的使用场景 (Swarm16)

    参考https://www.cnblogs.com/CloudMan6/p/8082429.html   我们可以用secret管理任何敏感数据.这些敏感数据是容器在运行时需要的.同时我们又不想把这些 ...

  2. CF407D Largest Submatrix 3

    cf luogu 被自己菜到自闭了/kk 既然是子矩阵,那么惯用套路为枚举矩阵上下边界,然后\(O(n)\)扫描求解.这题里要从左往右枚举右端点,然后看左端点最多能放到哪,那就对于每个数求出在上下边界 ...

  3. log4j2配置文件xml详细了解

    log4j2配置文件xml详细了解 详细参考:https://www.cnblogs.com/new-life/p/9246143.html log4j 2.x版本不再支持像1.x中的.propert ...

  4. 关于获取jquery对象的长度

    /* 17:10 2019/8/6 @author zhangxingshuo jQuery:"write less, do more" homepage: https://jqu ...

  5. Java注解的继承

    注解继承的说明 1.首先要想Annotation能被继承,需要在注解定义的时候加上@Inherited,并且如果要被反射应用的话,就需要还有个事@Retention(RetentionPolicy.R ...

  6. Wayos网吧路由英雄联盟频繁掉线解决办法

    英雄联盟某些机器瞬间ping值飙升,然后一直掉线重连!研究好久,解决了,经验与大家分享 第一步,在路由器地址后加qos_ext.htm进入qos参数设置页面(如果出现不了设置界面请更新固件).比如:h ...

  7. Mybatis分页-利用Mybatis Generator插件生成基于数据库方言的分页语句,统计记录总数 (转)

    众所周知,Mybatis本身没有提供基于数据库方言的分页功能,而是基于JDBC的游标分页,很容易出现性能问题.网上有很多分页的解决方案,不外乎是基于Mybatis本机的插件机制,通过拦截Sql做分页. ...

  8. deep_learning_Function_numpy_newaxis参数

    np.newaxis的作用就是在这一位置增加一个一维,这一位置指的是np.newaxis所在的位置,比较抽象,需要配合例子理解. x1 = np.array([1, 2, 3, 4, 5]) # th ...

  9. html 中 图片和文字一行 垂直居中对齐

    效果:      代码:<div><img src='img/point_icon.png' width='35px' height='35px' style='float: lef ...

  10. PAT Basic 1093 字符串A+B (20 分)

    给定两个字符串 A 和 B,本题要求你输出 A+B,即两个字符串的并集.要求先输出 A,再输出 B,但重复的字符必须被剔除. 输入格式: 输入在两行中分别给出 A 和 B,均为长度不超过 1的.由可见 ...