You can use has to narrow down the set of all p elements to those that contain img elements, and then use css to change the property:
$("p").has("img").css({textAlign: "center"});
Alternatively, you can use the :has selector:
$("p:has(img)").css({textAlign: "center"});
However, the .has method is faster than the selector.
source share