1

How can I style or add class to a css element depending on content type. My example: I wish to add class or style a p element when it contain an iframe like this.

<p><iframe frameborder="0" height="400" marginheight="0" marginwidth="0" scrolling="no" src="https://www.hashdoc.com/documents/185392/embed" style="border:1px solid #EAE9EA; border-width:1px; margin-bottom:5px; max-width: 100%;" title="Les résultats aux examens" width="550"></iframe></p>

So it could be something like that:

if p contain iframe add class 'myclass' to p

Can I do that with sass or do I need js?

EDIT: my final jquery code

$(".page-lycee p:has(iframe)").addClass('external-content');
1
  • if you want to apply only first child (if it is iframe) then basically by css you can do it i.e. p > iframe { your css } and if you want to apply every iframe in p tag then directly use p iframe { css } Commented Jun 27, 2017 at 10:09

3 Answers 3

1

As far as i know it's currently not possible with CSS. You can fix this with jQuery though.

Example code:

$('p:has(iframe)').addClass('myclass');
Sign up to request clarification or add additional context in comments.

Comments

1

you can use the jQuery selector :has()

$("p:has(iframe)").css("border", "solid red");

example: https://www.w3schools.com/jquery/sel_has.asp

Comments

1

You can use .has() to check if p has iframe like follow:

$("p").has("iframe").addClass('myClass');
.myClass{
  background:red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>
  <iframe frameborder="0" height="400" marginheight="0" marginwidth="0" scrolling="no" src="https://www.hashdoc.com/documents/185392/embed" style="border:1px solid #EAE9EA; border-width:1px; margin-bottom:5px; max-width: 100%;" title="Les résultats aux examens"
    width="550">
  </iframe>
</p>

<p> This will not have the class "myClass"</p>

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.