Yii2 version of <a href='go' id='blah'><img src='myimage' ></a>

Ok, I’ve spent about 4 hours trying to figure out how to do something that can be done in simple html in about 3 seconds – use an IMAGE as an href link. This is so common in the web that I can’t believe it’s not simply an option in the HTML::a thing. I don’t see any obvious reference to it in the tutorials and this is such a basic thing that it ought to be a priority in a tutorial. What is the deal?? How do use HTML::a to accomplish " <a href=‘go’ id=‘blah’><img src=‘myimage’ ></a>". Yeah, I see all kinds of tutorials on “options” but I don’t see EXAMPLES on how to accomplish this. I looked at Zurmo, but it uses an id attribute and it’s Yii version 1 anyway.

I did try the "id" attribute and it halfway works:

    &lt;?php &#036;options = ['class' =&gt; 'site-employer-demo', 'id' =&gt; 'emp-vid']; ?&gt;

      &lt;?= Html::a('Employer Demo', ['site/employer_demo'], &#036;options); ?&gt;

The problem with this is that it only displays the top ‘sliver’ of the image. The image is 125 x 128. I’ve tried hard-coding that in the css several different ways, but nothing works.

And I’d really rather use the IMG tag for href anyway. And by the way, the very next line of my code IS a ‘non-Yii’, standard href tag as described above, and it displays just fine. But I’d rather follow the Yii2 paradigm on this if it’s not too much of a pain. Thanks

You can just use an image in place of the link text.

<?= Html::a(Html::img($img_url, $img_options), $link_url, $link_options); ?>

See the Guide > Html helper


And also see API for yii\helpers\BaseHtml::a() and yii\helpers\BaseHtml::img().



Yessir, thank you. I found that on SO about an hour before I saw your response. It just seemed to me to be such a basic thing that I didn’t understand why it was stuck off somewhere other than the “main” tutorial. I need to adjust my thinking on this tutorial. Thanks again.