Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

eugenefvdm's avatar

Use CSS Style Image Width and Height with Nova

I'm trying to apply a CSS style class to an image preview field in Laravel Nova, but I'm not sure if it's possible. I've tried this:

Text::make('Image Preview 1', function() {
                return "<img src={$this->image_url_1}>";
            })->asHtml()->withMeta(
                [
                    'style' => 'height=20px;width=auto'
                ]
            ),

and I've tried this:

Text::make('Image Preview 1', function() {
                return "<img src={$this->image_url_1}>";
            })->asHtml()->withMeta(
                ['extraAttributes' => [
                    'style' => 'height=20px;width=auto']
                ]
            ),

Neither worked. I've scouted the documentation and google and just really struggling to find out if this is possible. Any clues?

0 likes
3 replies
bobbybouwmann's avatar
Level 88

You need to add the css to the img tag itself

Text::make('Image Preview 1', function() {
    return sprintf(
        '<img style="%s" src="%s">',
        'height=20px;width=auto',
        $this->image_url_1
    );
})->asHtml();
1 like
eugenefvdm's avatar

Sweet thanks @bobbybouwmann that worked perfectly. I also changed the CSS afterwards to use em instead of px but that was my design problem, and nothing to do with the original question :-)

Please or to participate in this conversation.