0

I want to use a toggle button and found a perfect solution. I adapted the code to use a Font Awesome icon:

<a id="button1" title="button1"><i class="fa fa-bluetooth fa-5x"></i></a>
<a id="button2" title="button2"><span id="mytext">large text</span></a>

The result is that neither the icon nor plain text expand the button:

enter image description here

What is the way to automatically the size of a button to the size of the underlying text?

Community
  • 1
  • 1
WoJ
  • 27,165
  • 48
  • 180
  • 345

1 Answers1

2

Add a display property to the anchor.

$(document).ready(function() {
  $('a#button1').click(function() {
    $(this).toggleClass("down");
  });
});
a {
  background: #ccc;
  cursor: pointer;
  border-top: solid 2px #eaeaea;
  border-left: solid 2px #eaeaea;
  border-bottom: solid 2px #777;
  border-right: solid 2px #777;
  padding: 5px 5px;
  display: inline-block; /* add display property */
}
a.down {
  background: #bbb;
  border-top: solid 2px #777;
  border-left: solid 2px #777;
  border-bottom: solid 2px #eaeaea;
  border-right: solid 2px #eaeaea;
}
#mytext {
  font-size: 3em; /* better you use em's rather than pt's */
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css" type="text/css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<a id="button1" title="button1"><i class="fa fa-bluetooth fa-5x"></i></a>
<a id="button2" title="button2"><span id="mytext">large text</span></a>
Aaron
  • 10,187
  • 3
  • 23
  • 39