1

I have some markup below and what i want to do using css is target the 3 following points.

  • img
  • style*=right
  • class=media-element

I have tried the following code which fails

img[style*="right"][class="media-element"] {margin-left:10px;}

Using the following to select the style works

img[style*="right"]...

3 Answers 3

2

Try this:

img.media-element[style*="right"] {margin-left:10px;}

No need to use attribute equal selector if you know your exact class name. Use . ( dot ) notation instead.

Sign up to request clarification or add additional context in comments.

Comments

2

Your attribute selector [class="media-element"] looks for an exact match of the entire value of the class attribute. It will match the element only if the attribute as it appears in the markup is exactly class="media-element", with no other class names. Otherwise, it will fail.

If you're selecting by class name, you really should be using a class selector:

img[style*="right"].media-element {margin-left:10px;}

You should only use an attribute selector with the class attribute if you have a very good reason to do so.

Comments

1

If you have many classes inside class='', then [class="media-element"] is not going to match any element,

what you can do alternately is,

img[style*="right"][class*="media-element"] {margin-left:10px;}

see this fiddle: http://jsfiddle.net/m4t5qmw3/1/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.