使用PHP DOM更改CSS

I'm making a Wordpress website, and I have a question. Can I change CSS of certain elements with PHP DOM? I don't want to change structure of HTML, just few styles if certain conditions are met.

For example, if no images are present in my post I would like to add change color of the links in that post.

I'm thinking doing that with jQuery since it sounds a lot simpler, but I'm just wondering is this a valid way to do, or should I use PHP DOM?

You can try with something like this:

if(!$('img').length){
    $('a').css('color', 'red');
}

here a jsfiddle with the example

For this type of operation, it is generally advised to do it with jQuery. This will save you processing time and power on your server for something easily done by the browser.

If, however, it absolutely must be done server-side, then you can achieve this with DOMAttr:

$attr = $element->setAttributeNode(new DOMAttr('style', 'border:1px solid black;'));

You can easily create an inline CSS block using PHP without PHP-DOM:

<?php if ($linkCount == 0) { ?>
    <style>
        a:link {
            color: red;
        }
    </style>
<?php } ?>