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. 如何去除List集合中的重复元素? a,b,c,a,c,b,d,,,,,,

    package com.fs.test; import java.util.ArrayList; import java.util.List; public class Listdemo { publ ...

  2. 用原生JS写省市二级联动

    HTML代码 <select id="s1"> <option value="0">~请选择省份~</option> < ...

  3. vue路由(基于VScode开发)

    index.js如果在router目录下,代表这个js文件只是路由使用 main.js中为全局,需要引入使用到的组件,一般vue中不用写东西vue中el挂载哪个就哪个组件为根目录,传值数据绑定的时候在 ...

  4. python调用接口的方式

    python中调用API的几种方式: - urllib2 - httplib2 - pycurl - requests urllib2 import urllib2, urllib github_ur ...

  5. IDEA修改Maven全局配置

    在使用过程中发现,IDEA每次新建一个Project ,这个maven配置都会初始化默认的. 这里需要设置下全局配置: File -> Other Settings -> Settings ...

  6. 2019-2020-1 20199319《Linux内核原理与分析》第七周作业

    进程的描述和进程的创建 进程的描述 1.操作系统内核实现操作系统的三大管理功能: 进程管理 内存管理 文件系统. 其中最核心的功能是进程管理. 2.对进程的描述:在操作系统原理中,通过进程控制块PCB ...

  7. 排序——插入排序(C语言)

    void insertSort(int* a,int T){ int tmp,p; ;i<T;i++){ tmp=a[i]; p=i-; &&tmp<a[p]){ a[p+ ...

  8. jmeter通过if控制器控制业务比例

    以发帖,看帖,回帖三个事物为例,这里就10个用户跑10次,进行测试下: 可以看到看帖,回帖,发帖比例是5:3:2,先来说说怎么做到的,就是通过if控制器,分别来看下几个控制器的内容, 看帖(if控制器 ...

  9. (转) SpringBoot:使用spring-boot-devtools进行热部署以及不生效的问题解决

    在idea上怎么才能使用Spring的热部署 这里仅用Maven做介绍 (1)在Maven中添加依赖 (2)在插件配置 但是,为什么配置了还是没有用呢 ?! 这是因为idea默认是没有自动编译的,我们 ...

  10. 同步windows时间到linux服务器

    输入date -R 查看系统时间 输入命令  ntpdate time.windows.com 同步windows时间到linux