0

I want the image centered within my button but it breaks out in Firefox while it works flawlessly in Google Chrome. Why does this occur and how can I prevent it from happening?

Here is the code:

button {
    all: unset;
    height: 50px;
    width: 50px;
    display: inline-grid;
    place-items: center;
    background-color: firebrick;
}

button img {
    height: 60%;
    margin: auto;
}
<button><img src="https://upload.wikimedia.org/wikipedia/commons/9/99/Black_square.jpg"></button>

Here is what it looks in Chrome:

Chrome

And in Firefox:

Firefox

leonheess
  • 16,068
  • 14
  • 77
  • 112

1 Answers1

1

display: inline-grid is not needed. Button is a block element and everything set in the body will be in the middle of the button

button {
  height: 50px;
  width: 50px;
  background-color: firebrick;
}

button img {
  height: 60%;
  margin: auto;
  display: block;
}
<button><img src="https://upload.wikimedia.org/wikipedia/commons/9/99/Black_square.jpg"></button>
Grzegorz T.
  • 3,903
  • 2
  • 11
  • 24
  • 2
    *Button is a block element and everything set in the body will be in the middle of the button* : can you explain what you mean by this? button isn't a block element – Temani Afif Aug 15 '19 at 08:54
  • @TemaniAfif true. The `Button` element is by default an `inline-block` element. Nevertheless the core of the answer is correct. Removing `inline-grid` will solve the problem. – j.Doe Aug 15 '19 at 08:59
  • It is however not 100% centered, is it? It looks shifted upwards – leonheess Aug 15 '19 at 09:00
  • @j.Doe removing everything will also work (margin:auto, text-align:center) .. but still don't understand the explanation – Temani Afif Aug 15 '19 at 09:00
  • I wrote it wrong, I meant that every element of text or in this case img placed between the opening and closing the tag will be inside this button. – Grzegorz T. Aug 15 '19 at 09:01
  • 2
    @MiXT4PE you need `display:block` to the image to avoid the whitespace at the bottom and it will be centred – Temani Afif Aug 15 '19 at 09:01