html - 媒体查询选择器元素的多个类有什么问题?或者这里发生了什么?

标签 html css

我一直在尝试让 horus 找出我制作的网页的问题,我已经将其缩小到与多个类有关的问题,并且我已经制作了一个 fiddle 来证明。

HTML:

<p class="bg-yellow-on-mobile">
  Should have a yellow background on mobile
</p>
<p class="bg-yellow-on-mobile talign-right-on-mobile">
  Should have a yellow background and text aligned right on mobile
</p>

CSS:

@media (max-width:767px){
  [class="bg-yellow-on-mobile"] { background: yellow; }
  [class="talign-right-on-mobile"] { text-align: right; }
}

fiddle :https://jsfiddle.net/4w9L16g3/

您会看到移动样式未应用于第二个 <p>标记,具有两个类的标记。是因为它有多个类的原因吗?

最佳答案

你需要使用 * 字符来选择类名,它正是属性中的内容或它的一部分

https://www.w3.org/TR/2001/WD-css3-selectors-20010126/#attribute-selectors

6.3.2 Substring matching attribute selectors

Three different attribute selectors are also available for pattern representation into the value of an attribute :

[att^="val"]

Represents the att attribute, its value beginning exactly with the prefix "val"

[att$=ident]

Represents the att attribute, its value ending exactly with the suffix "ident"

[att*="val"]

Represents the att attribute, its value containing at least once the substring "val"

@media (max-width:767px){
  [class*="bg-yellow-on-mobile"] { background: yellow; }
  [class*="talign-right-on-mobile"] { text-align: right; }
}
<p class="bg-yellow-on-mobile">
  Should have a yellow background on mobile
</p>
<p class="bg-yellow-on-mobile talign-right-on-mobile">
  Should have a yellow background and text aligned right on mobile
</p>

关于html - 媒体查询选择器元素的多个类有什么问题?或者这里发生了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35856771/

相关文章:

javascript - Jquery UI datepicker 添加 "Unknown"选项值到输入

html - 导航栏事件元素在悬停时不应该改变颜色?

html - 静态网站 AWS S3 上的多个页面

javascript - 输入 slideUp/slideDown 问题

html - 点击图片链接

css - 宽度为 : 100% inside position: absolute on IE7 的表格

javascript - 为什么 CSS 转换在通过 trigger.click 触发时不触发

CSS 框架,我的内容不遵守框架限制

html - 选择第一个子标签

java - 如何将 <Select> 和 &lt;input&gt; 数据从 View 传递到 Controller (Spring MVC)