Escape syntax for CSS selectors in LESS

拈花ヽ惹草 提交于 2019-12-23 05:51:29

问题


The tilde (~) and quote method to escape CSS properties appears to work fine, however what do I do if I want a CSS selector to contain a number (or anything else I want LESS to ignore), e.g.

~"#content ul.2col" {
    float: left;
    width: 45%;
}

This just gives me a syntax error. Any ideas?

Update:

It turns out, starting a CSS indentifier (element names, classes, IDs in selectors..) with a digit or a hyphen followed by a digit is not allowed. See this question:

Which characters are valid in CSS class names/selectors?


回答1:


Class selectors starting with numbers don't validate as standard CSS, as mentioned in your edit. However, I'm not sure how LESS treats invalid selectors, but you can either:

  • Change your class to a valid one that, say, starts with a letter; or
  • If you must keep the initial digit, use either one of these hacks to make it "valid":

    • Use an attribute selector:

      #content ul[class~="2col"] {
          float: left;
          width: 45%;
      }
      
    • Escape the digit with a backslash:

      #content ul.\2col {
          float: left;
          width: 45%;
      }
      


来源:https://stackoverflow.com/questions/8290104/escape-syntax-for-css-selectors-in-less

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!