Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS selector for element within element with inline style?

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> ​ 
like image 684
Evanss Avatar asked Jan 03 '13 13:01

Evanss


People also ask

Do inline styles always use a selector?

Inline Style Syntax In our case, the value of the style attribute will be CSS property-value pairs: "property: value;" . You can have as many property value pairs as you want. Unlike normal CSS syntax, inline styling does not use selectors or curly braces.

How do you target a style attribute in CSS?

The CSS Attribute Selector is used to select an element with some specific attribute or attribute value. It is an excellent way to style the HTML elements by grouping them based on some specific attributes and the attribute selector will select those elements with similar attributes.

How can we select elements with a specified attribute in CSS?

The [attribute|="value"] selector is used to select elements with the specified attribute, whose value can be exactly the specified value, or the specified value followed by a hyphen (-). Note: The value has to be a whole word, either alone, like class="top", or followed by a hyphen( - ), like class="top-text".

Which selector would select only internal links within the current page?

The :link selector is used to select unvisited links.


2 Answers

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

like image 131
Dan Avatar answered Sep 24 '22 02:09

Dan


p[style="text-align: center;"] {   color: red; } 

However this is ugly.

like image 23
Simone Avatar answered Sep 24 '22 02:09

Simone