How to style HTML element with PHP

790

I hope you can help me with this code below. So I don't know how to style element .img-box with php code.

this is original code from file:

<?php hc_echo($img,'<div >
<div >
<img src="','" alt="" /></div></div>' ); 
?>

and I have added this:

class="page_speed_534373672"custom_imgbox_style"]);"

so it looks like this:

<?php hc_echo($img,'<div >
<div custom_imgbox_style"]);">
<img src="','" alt="" /></div></div>' ); 
?>

but it is not working.

I am still a beginner and I no longer know what to do next.

I need to have ability to add style for .img-box element.

Just i dont know how to embed style for .img-box.

Also there is other solution like this:

<?php
echo '<style type="text/css">
        .img-box {

        }
        </style>'; ?>

but again how to add php variable inside ?

Thank you

75

Answer

Solution:

Basically, whenever you need to write anything which is php related in html code just put it in the php tags, like this:

<?php 

$user = 'Jeff';

?>

<h1><?php echo 'Hey ' . $user . ', how are you?'; ?></h1>

So this will output following text:

Hey Jeff, how are you?

It's hard to tell why your code is not working, without seeing the code of "hc_echo" function and knowledege of what this function expects as parameters.

Seeing that you are trying to use "esc_attr", which is wordpress related function, I assume you have installed a plugin which uses "hc" prefix for it's functions (because it is a good practice when you are creating wordpress plugins - use your own prefix for that plugin functions) and now you are trying to modify it's code.

What you can try (though I do not recommend doing that - explanation below) is adding style this way:

class="page_speed_1257402319"custom_imgbox_style"]); ?>"

This is basically what I mentioned before. Since "esc_attr" is a wordpress php function, and you want to put it into html attribute, you have to use php tags. Otherwise it will just be a plain text, not an execution of the function.

I DO NOT recommend doing that though, because this is basically adding something to the parameter of the function which is unknown, we don't know what the function does and what the function expects as the parameter. Doesn't sound very safe, does it?

159

Answer

Solution:

You can do:

<?php
echo '<style type="text/css">
        .img-box {
           width: '. $var . ';
        }
        </style>'; ?>

People are also looking for solutions to the problem: curl - Coinbase Pro post place order php

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.