简体   繁体   English

如何使用特定的css属性(CSS Only Solution)定位元素?

[英]How to target an element with specific css attribute (CSS Only Solution)?

I have an HTML snippet like below: 我有一个HTML代码段如下:

<div class="status">
    <div class="status-icon green">
      Online
    </div>
</div>

and related css: 相关的CSS:

.status {
  width: 100px;
}
.status-icon {
  display: none;
}

My question is: 我的问题是:

How can I write a css rule when .status{width=150px} then .status-icon{display: block;} ? .status{width=150px}然后.status-icon{display: block;}时,如何编写css规则?

Or is there a selector to target specific css rules like attribute selectors? 或者是否有一个选择器来定位特定的CSS规则,如属性选择器?

You cannot write a CSS rule where a property is set depending on whether the value of another property satisfies some condition. 根据另一个属性的值是否满足某些条件,您无法编写设置属性的CSS规则。 This seems to be what you are asking, even though you refer to CSS attributes. 即使您引用CSS属性,这似乎也是您所要求的。 (There are no attributes in CSS; there are attribute selectors, but they refer to HTML or XML attributes.) (CSS中没有属性;有属性选择器,但它们引用HTML或XML属性。)

CSS as currently defined is simply a style sheet language with no programming features (or, let us say, with very limited programming-like features). 目前定义的CSS只是一种没有编程功能的样式表语言(或者,我们说,具有非常有限的类似编程的功能)。

If you define status as a percentage (instead of fixed pixels) then you can do this with media queries 如果您将状态定义为百分比(而不是固定像素),则可以使用媒体查询执行此操作

like so: 像这样:

FIDDLE 小提琴

.status {
    width: 20%;
    height: 100px;
    background: blue;
}
.status-icon {
    display: none;
    color: white;
}
/* 20% of 750px = 150px */
@media (min-width: 750px) {  
    .status-icon
    {
        display: block;
    }
}

So now when the viewport width hits 750px+ the status element will be 150px wide and with the media media query we can set .status-icon to block 所以现在当视口宽度达到750px +时,状态元素将是150px宽,并且通过媒体媒体查询我们可以设置.status-icon来阻止

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM