Textbox Background Image


I am new to Yii, and I dont know how to put background image for the textbox. So please any one can help me.


Pratik Shah

what textbox some code would help perhaps

If you’re using CHtml and want to apply it inline, you can do something like this:

CHtml::textField($name, $value, array(

    'style'=>'background-image: url("' . Yii::app()->baseUrl . '/images/image.png");',


You’re probably better off applying it in your style sheet though.

I was using

<?php echo $form->textField($model,‘username’); ?>

to display the textbox instead of textbox i want image, when i used

	&lt;?php CHtml::textField(&#036;model, 'username', array('style'=&gt;'background-image: url(&quot;' . Yii::app()-&gt;baseUrl . '/images/image.png&quot;);',));?&gt; 

I got an error saying that

Object of class LoginForm could not be converted to string

It sounds like the error is coming from somewhere else in your code. Look at the stack trace and check which line causes the error.

Also, you can continue to use $form->textField(), just use the array that I suggested as its third parameter.

there is a difference between:

CHtml::textField($name, $value...


CHtml::activeTextField($model, $attribute...

I am using the same

<?php echo CHtml::image(Yii::app()->request->baseUrl."/images/username.png");?>


		<?php CHtml::textField($model, 'username', array('style'=>'background-image: url("' . Yii::app()->baseUrl . '/images/UserName-Field.png");',));?>

as you mentioned I’m using it in array but still it is giving the error attaching the snapshot of stack trace.

like I said - there is a difference between Keith’s suggestions and how you use it. If you provide $model and attribute name you should use “active*” functions (in your case activeTextField), textField function requires field name as string and its value as string in first two arguments. This applies to CHtml helper.

Earlier you used CActiveForm widget which wraps those methods so you do not need "active" prefix:

class CActiveForm extends CWidget {


public function textField($model,$attribute,$htmlOptions=array())


    return CHtml::activeTextField($model,$attribute,$htmlOptions);




Thanx sir,

Now its not giving me error but image is not rendering…my Image is in the Image folder only and basepath is also correct… is there any problem with this quotes…?

<?php CHtml::activeTextField($model, 'username', array('style'=>'background-image: url("'.Yii::app()->request->baseUrl .'/images/UserName-Field.png");',));?>

You have to echo.

oooops…its showing now…can we remove Texbox’s border…?


I tried googling for you. :)