Is there a CSS selector to select this element by its inline style attribute value?
<div style='display:block'>...</div>
something like
div[cssAttribute=cssValue]
Is there a CSS selector to select this element by its inline style attribute value?
<div style='display:block'>...</div>
something like
div[cssAttribute=cssValue]
The inline style
attribute is no different to any other HTML attribute and can be matched with a substring attribute selector:
div[style*="display:block"]
It is for this very reason however that it's extremely fragile. As attribute selectors don't support regular expressions, you can only perform exact substring matches of the attribute value. For instance, if you have a space somewhere in the attribute value, like this:
<div style='display: block'>...</div>
It won't match until you change your selector to accommodate the space. And then it will stop matching values that don't contain the space, unless you include all the permutations, ad nauseam. But if you're working with a document in which the inline style declarations themselves are unlikely to change at all, you should be fine.
Note also that this is not at all selecting elements by their actual specified, computed or used values as reflected in the DOM. That is not possible with CSS selectors.
img[style*="float:right"] { margin-right: 6px; margin-left: 40px; } img[style*="float:left"] { margin-left: 6px; margin-right: 40px; }
–
Dicephalous align="left/right"
or use float: left/right;
in a stylesheet, than use style="float:left/right"
in the markup, for that to be often useful. –
Ovalle div[style*="display"][style*="block"]
. But be careful as to not accidentally select unwanted elements where it matches too, like this example would also match style="display:inline-block;"
--- Of course it's always better to avoid inline styles at all costs and use classes instead, but in a few specific situations it may still be unavoidable. –
Willwilla Including ";" works better for me.
div[style*="display:block;"]
style
must exactly match to the HTML property –
Papain ;
. –
Barn Always look how the attribute is written in HTML (you can check it in the Elements tab in the browser). You have to use the exact same value. In my case: style="left: 100%;"
. And not style="left:100%"
or anything like that.
I just checked in the project and the only thing I could find that worked is the exact class description: [style="display: block;"]
.
© 2022 - 2024 — McMap. All rights reserved.