Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set anchor tag text width using css?

I have html and css as below -

    .title {
        display: block; background-color: red;
    }

<a href="#">
    <span class="title">Text</span>
</a>

I could see that the SPAN spans to the 100% of the available width (because of display: block). Like below

|----------------------------------------------------|
| Text                                               |
|----------------------------------------------------|

In Firefox, I can click anywhere in the above box, and it takes me to the linked page. However, In IE (IE 7) I get the cursor as hand only when I hover over "Text" text only.

What hack I'll have to do to make it work (same as it does in FF) in IE as well?

I tried placing the anchor tag itself (not just the text) in span but it won't work.

Thanks.

like image 924
TigerTiger Avatar asked Jul 09 '09 12:07

TigerTiger


People also ask

How do I change the width of an anchor tag?

You need to make your anchor display: block or display: inline-block; and then it will accept the width and height values.

Can we give width to anchor tag?

An anchor is an inline element by default so you can't add dimensions to it unless you change its display property.

How do you give a tag a width?

To set the table width in HTML, use the style attribute. The style attribute specifies an inline style for an element. The attribute is used with the HTML <table> tag, with the CSS property width.


1 Answers

Style the anchor and remove the span.

(The problem is due to how some browsers handle elements that are display: block inside elements that are display: inline. You can work around it by styling both the anchor and the span, but the span appears redundant in this example)

like image 129
Quentin Avatar answered Sep 28 '22 18:09

Quentin