CSS selector for element within element with inlin

2019-03-11 03:58发布

Is there a CSS selector to target elements with inline styles? So can I target the first span but not the 2nd with CSS only?

If not, can this be done with jQuery?

http://jsfiddle.net/TYCNE/

<p style="text-align: center;">
    <span>target</span>
</p>

<p>
    <span>not target</span>
</p>
​

4条回答
Ridiculous、
2楼-- · 2019-03-11 04:15

use :

​p[style] span {
  color: red;   
}​
查看更多
在下西门庆
3楼-- · 2019-03-11 04:23

A bit late to the tea party but thought I would share the solution I found & use.

@simone's answer is perfect if you can match the style attribute exactly. However, if you need to target an inline style attribute that may have other inline styles associated with it you can use:

p[style*="text-align:center;"]

"*=" means "match the following value anywhere in the attribute value."

For further reference or more detailed information on other selectors see this blog post on css-tricks.com:

The Skinny On CSS Selectors

http://css-tricks.com/attribute-selectors/#rel-anywhere

查看更多
疯言疯语
4楼-- · 2019-03-11 04:28

If you would like to apply styles to a particular rule declaration you can also use style*. This will match all elements that have the inline style, regardless of the value applied.

div[style*="background-image"] {
  background-size: cover;
  background-repeat: no-repeat;
}
查看更多
仙女界的扛把子
5楼-- · 2019-03-11 04:30
p[style="text-align: center;"] {
  color: red;
}

However this is ugly.

查看更多
登录 后发表回答