I'm wondering how to format my code. which one is 开发者_Go百科better?
<a href="http://stackoverflow.com">
<img src="http://localhost/g3.jpg" alt="" height="90" width="150">
Q&A for professional and enthusiast programmers
</a>
OR
<a href="http://stackoverflow.com">
<img src="http://localhost/g3.jpg" alt="" height="90" width="150">
</a>
<a href="http://stackoverflow.com">Q&A for professional and enthusiast programmers</a>
I would definitely go with the first one unless you have a specific reason for having two links. Don't think about it in terms of proper coding style, but rather what makes sense usability-wise.
Note that you also have the option to only use the text in the link, and set the image as the link's background-image
property in CSS. In many cases, that's exactly what I would do unless the image itself is essential to understanding the link.
In cases like this, the best thing is to think, "Which HTML structure most accurately represents how I want a user to perceive this element?"
It all depends on what you want to get :
- If you only want one link (which, I suppose, is the case, here ?), then go with the first solution
- If you want two distinct links (not sure why you'd want that here, as both point to the same location), then go with the second solution.
Here, you want the <img>
and the text to link to the same page -- so, I'd say it's more logical to only use one <a>
The first one makes more sense to me (since you want both image and text to be part of the link, i see no reason to make two links).
精彩评论