Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Material Icons alignment in Bootstrap buttons

How can I get Google Material Icons properly aligned in my Bootstrap buttons?

For example, consider this code:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">

<!-- body -->
<button type="button" class="btn btn-danger btn-sm">
  <span class="material-icons">delete</span> Supprimer
</button>

Gives me this output:

enter image description here

That is not very nice output. What is the best solution to fix the alignment between the icon and the text?

like image 910
flaggalagga Avatar asked Nov 23 '16 16:11

flaggalagga


People also ask

How do I move a button to the right in bootstrap?

Bootstrap allows us to align elements by using the utility class float. As we want to align the button to the right side of the text box, we have to use the float-right class.

How do you align vertical icon and text in MUI?

If you want to make a text appear vertically aligned next to a Font Awesome icon, you can use the CSS vertical-align property set to “middle” and also, specify the line-height property. Change the size of the icon with the font-size property.

How do you make a rounded button in bootstrap?

Add . btn-rounded class to make the button rounded.


2 Answers

Using Bootstrap class :

<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">


<button type="button" class="btn btn-secondary d-flex justify-content-center align-content-between" (click)="refresh()" [disabled]="loading">
     <i class="material-icons mr-1">refresh</i> <span>Refresh</span>
 </button>
like image 75
Jeffrey Nicholson Carré Avatar answered Nov 08 '22 01:11

Jeffrey Nicholson Carré


I use this.

It works for every button-size and icon-size. No additional classes nor elements and no flexbox. line-height: 0; prevents that the icon affects the height of the button.

.material-icons {
  vertical-align: middle;
  line-height: 0 !important;
  position: relative;
  top: -1px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">

<!-- body -->
<button type="button" class="btn btn-danger btn-sm">
  <span class="material-icons">delete</span> Supprimer
</button>


<button type="button" class="btn btn-danger btn-lg">
  <span class="material-icons">delete</span> Supprimer
</button>


<button type="button" class="btn btn-danger btn-lg">
  <span class="material-icons" style="font-size: 36px;">delete</span> Supprimer
</button>


<button type="button" class="btn btn-danger btn-lg">
  <span class="material-icons" style="font-size: 12px;">delete</span> Supprimer
</button>
like image 34
haco Avatar answered Nov 08 '22 01:11

haco