Apply class to every paragraph that holds image?

You could use jQuery if you don’t mind to rely on JavaScript for adding the class.

$(document).ready(function() {
    $('p:has(img)').addClass('image');
});

Update: the .has() method is probably faster, see this jsperf.com test.

$(document).ready(function() {
    $('p').has('img').addClass('image');
});

Leave a Comment