Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to style an anchor tag to look like a button with the same height as the button?

Tags:

I am working on changing the buttons on my site to be styled by a jquery ui theme. Mostly everything is going good with it.

But there are a few anchor tags that I wanted styled as buttons. I added the classes and it styles it how I want it except that the height is not the same. Is there any way to make the styled anchor tag have the same height as the styled button tag?

Here is some of my css:

.mine-button {      outline: 0;      margin: 0 4px 0 0;     padding: 0 1em;     height: 2em;     text-decoration: none !important;      cursor: pointer;      position: relative;     text-align: center;      -moz-border-radius: 15px;     -webkit-border-radius: 15px } 

An example using it on a button:

<button class="mine-button ui-state-default"  onclick="stuff here">     <img src="/i_common/basket_add_24.gif" border="0" align="absmiddle"/> Add </button> 

An example using it on an achor:

<a class="mine-button ui-state-default" href="bla">     <img src="/i_common/CD_down_24.gif" border="0" align="absmiddle"/> Free </a> 
like image 662
Echo says Reinstate Monica Avatar asked Jul 13 '09 20:07

Echo says Reinstate Monica


People also ask

How do I change the height 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 I use anchor as button?

TLDR; you can open an anchor link in a new Tab/Window but not a button. Here is a rule of thumb: For navigation just use anchor it's alright to style it as a button and let it use it's href attribute well. For quick actions (play,pause,stop,+1 like) just use button it doesn't have href for a reason!

How do you give an anchor tag a style?

CSS Code. When styling the text of the link itself, we simply reference the anchor tag class name only, and we change change things such as the text's color and other attributes. When referencing the special attributes of the anchor tag, such as link, visited, hover, and active.


2 Answers

This should do it:

display: inline-block; 

The reason your height isn't working is because the anchor tag marks an inline element. The height property doesn't apply to inline elements, and the vertical margin, border and padding act differently.

Firefox 2 doesn't support inline-block, if you still need to support it, but you can usually get it to work by adding a rule display:-moz-inline-box before the above line.

Alternatively, you could try using the line-height property.

like image 55
mercator Avatar answered Sep 30 '22 12:09

mercator


border and align are deprecated attributes, or at least these are about presentation, not content and as such should be done in CSS, not in the HTML code:

.mine-button {   border: 0;   vertical-align: bottom/middle/top/whatever; } 

Also, alt is a mandatory attribute of the img element. Can be empty (alt="" for fancy though meaningless images) or meaningful (if image conveys information not already present in text)

like image 21
FelipeAls Avatar answered Sep 30 '22 12:09

FelipeAls