Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to center icon and text inside of a button?

Tags:

html

css

How can I center icon and text inside of the button without specifiing the sizes to align items?

.ico {
  background: url(https://www.gravatar.com/avatar/cbfaff96665b7567defe1b34a883db8b?s=32&d=identicon&r=PG);
  width: 32px;
  height: 32px;
  display: inline-block;
}
<button>
  <span class=ico></span>
  And some text
</button>
like image 402
Qwertiy Avatar asked Feb 18 '16 20:02

Qwertiy


2 Answers

You can add vertical-align: middleto the class ico. Fiddle at: https://jsfiddle.net/mnet4hn4/

like image 90
emattiazzi Avatar answered Oct 05 '22 04:10

emattiazzi


https://jsfiddle.net/nog2kuta/

.ico {
  background: url(//www.gravatar.com/avatar/cbfaff96665b7567defe1b34a883db8b?s=32&d=identicon&r=PG);
  width: 32px;
  height: 32px;
  display: inline-block;
}

button {
  padding: 3px;
}

button span {
  display: inline-block;
  vertical-align: middle;
}
<button>
  <span class=ico></span>
  <span>And some text</span>
</button>

Works fine in Chrome, FF, IE (tested in 11), Opera 12

like image 21
Qwertiy Avatar answered Oct 05 '22 03:10

Qwertiy