2

My HTML element has an attribute and the goal is to give a certain style to this element when found inside another class.

for ex.:

<style>
em {color:#ff0000;}
.emClass {color:#ff0000;}
.pClass em {color:#ff0000;}
.pClass .emClass  {color:#ff0000;}
</style>

<p>
    Please <em>red</em> me.
</p>

<p>
    Please <em class="emClass">red</em> me.
</p>

<p class="pClass">
    Please <em>red</em> me.
</p>

<p class="pClass">
    Please <span class="emClass">red</span> me.
</p>

<p class="pClass">
    Please <em class="emClass">orange</em> me.
</p>

the goal is to have the text in Orange only in case:

  • it is emphasized text
  • AND has the "emClass" attribute
  • AND it is inside another element (paragraph or div) that has the "pClass" attribute.

(live example: https://jsfiddle.net/Yatko/Ffkcq/ )

Thank you for your help!

2
  • 1
    Is this what you are looking for? jsfiddle.net/Ffkcq/1 Commented May 17, 2013 at 21:30
  • 2
    None of your elements have an emClass attribute, some of them do have an emClass class-name, however. Commented May 17, 2013 at 21:31

4 Answers 4

4

You just need to update your class to

.pClass em.emClass {
    color: orange;
}

Check this fiddle

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

Comments

2

try this:

.pClass em.emClass {color: #ffff00;}

Comments

2

JSFiddle Demo

You can prefix the class name with the element type. You should target the specific element like this:

.pClass em.emClass  {color:orange;}
.pClass span.emClass  {color:red;}

Comments

1

You can do this with CSS DEMO https://jsfiddle.net/kevinPHPkevin/Ffkcq/4/

em {

    color:#ff0000;

}

.emClass {

    color:#ff0000;

}

.pClass em {

    color:#ff0000;

}

.pClass .emClass {

    color:#ff0000;

}

.pClass em.emClass {

    color: #FAC802;

}

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.