开发者

Anchor (<a>) dimensions with only inline-block spans inside

开发者 https://www.devze.com 2023-02-23 08:12 出处:网络
Could someone explain me what\'s going on with this small piece of HTML ? http://jsbin.com/akome5 On most of current browsers (FF4, Chrome10, IE9, IE8, Opera 11), the layout of theelement looks like

Could someone explain me what's going on with this small piece of HTML ?

http://jsbin.com/akome5

On most of current browsers (FF4, Chrome10, IE9, IE8, Opera 11), the layout of the element looks like this :

Anchor (<a>) dimensions with only inline-block spans inside

Meh?! I don't understand开发者_StackOverflow why ?! Why aren't the height and width as big as the visible box (orange+red spaces) ?

Adding a "display:inline-block;" to the element doesn't seems to really fix it. How can I fix it ?

Thx!!


  • Setting a width and height on an A tag


Try adding the following styles.

a.button {
    display: block;
    float: left;
    overflow: auto;
}
a.button span {
    display: block;
    float: left;
}


I'd propose a different approach involving no spans

html:

<a class="button2" href="#">Text Text Text</a>

css:

/* Button 2 */
.button2 {
    background-color:red;
    border:solid 10px orange;
    border-top:0;
    border-bottom:0;
    display:inline-block;
    color:#fff;
    font-family: Arial,Helvetica,sans-serif;
    font-size:11px;
    font-weight:bold;
    line-height:30px;
    text-decoration:none;
    padding:0 3px;
}

Anchor (<a>) dimensions with only inline-block spans inside

old (top) new (bottom)

http://jsfiddle.net/pxfunc/vr7gJ/


For information I manage to do it without float:left, here is the whole CSS :

a.button{
    display: inline-block; /* <- added */
    text-decoration: none;
}

a.button span{
    display: inline-block;
    font-family: Arial,Helvetica,sans-serif;
    font-size: 11px;
    font-weight: bold;
    height: 30px;
    line-height: 30px; /* <- added */
    text-decoration: none;
}

a.button .left, a.button .right{
    background-color: orange;
    width: 10px;
}

a.button .text{
    background-color: red;
    color: white;
}

The line-height instruction was the key.

0

精彩评论

暂无评论...
验证码 换一张
取 消