LESS: Can you group a CSS selector with a media query?

拥有回忆 提交于 2019-12-01 05:18:30

问题


I really enjoyed finding out you could create a media query variable that you can easily reuse and makes your code much more readable.

@tablet: ~"(min-width: 768px) and (max-width: 980px)";
@media @tablet { ... }

I want to know if it's possible to group a media query with a selector. It doesn't appear to work the way I've implemented it, but I thought I'd ask to see if it's even probable.

@tablet: ~"(min-width: 768px) and (max-width: 980px)";
body {
  aside { ... }

  &.homepage,
  @media @tablet {
    aside { ... }
  }
}

I understand that media queries are different from run-of-the-mill selectors because you have to define your selectors inside of the media query, but is there some voodoo LESS way to accomplish grouping like this?


回答1:


I'm not a 100% certain of the output you are going for, but this LESS only defines the color red once, and applies it to both:

@tablet: ~"(min-width: 768px) and (max-width: 980px)";
body {
  aside { color: blue }

  &.homepage {
    aside { color: red }
  }

  @media @tablet {
    .homepage;
  }
}

Yields this CSS:

body aside {
  color: #0000ff;
}
body.homepage aside {
  color: #ff0000;
}
@media (min-width: 768px) and (max-width: 980px) {
  body aside {
    color: #ff0000;
  }
}


来源:https://stackoverflow.com/questions/13843858/less-can-you-group-a-css-selector-with-a-media-query

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