0

There's a style for a button, that can be applied to a-elements and button-elements:

.button {
  appearance: none;
  display: flex;
  justify-content: center;
  align-items: center;
  width: auto;
  min-width: 150px;
  min-height: 50px;
  margin: 0 auto 20px auto;
  padding: 0;
  border: 1px solid black;
  background: white;
  font-size: 1.4rem;
}
<a class="button">a-element</a>
<button class="button" type="button">a-element</button>

While this works fine, there's a difference in rendering:

  • the a-element has 100 % width of the parent container
  • the button-element doesn't stretch and is centered

I'm not sure which browser default style property for buttons causes this.

How can I make the a-element to behave like the button?

So, the element is centered but not stretched.

lampshade
  • 2,470
  • 3
  • 36
  • 74

2 Answers2

1

Use inline-flex

.button {
  appearance: none;
  display: flex;
  justify-content: center;
  align-items: center;
  width: auto;
  min-width: 150px;
  min-height: 50px;
  margin: 0 auto 20px auto;
  padding: 0;
  border: 1px solid black;
  background: white;
  font-size: 1.4rem;
}

a.button {
  display: inline-flex;
  background: lightblue;
}

body {
text-align:Center;
}
<a class="button" href="#">a-element</a>
<button class="button" type="button">a-element</button>
Paulie_D
  • 107,962
  • 13
  • 142
  • 161
  • Yeah, but unfortunately `inline-flex` will stop `margin: auto` from working and the element is not centered anymore. – lampshade Jul 07 '20 at 10:13
  • Then you can center it but adding `text-align:center` to the parent. – Paulie_D Jul 07 '20 at 10:16
  • Yeah, but if I don't need to center everything in the parent, then I need to add another wrapper for the button. I thought there might be a way to circumvent this. – lampshade Jul 07 '20 at 10:25
-1

It's just because of display : flex, can be solved with inline flex

EDITED

.button {
  appearance: none;
  display: inline-flex;
  justify-content: center;
  align-items: center;
  min-width: 150px;
  min-height: 50px;
  width: auto;
  margin: 0 auto 20px auto;
  padding: 0;
  border: 1px solid black;
  background: white;
  font-size: 1.4rem;
}

.btn-container{
  text-align:center;
}
<div class="btn-container">
  <a class="button">a-element</a>
  <button class="button" type="button">a-element</button>
</div>
Niloy
  • 576
  • 4
  • 13
  • Yeah, but unfortunately `inline-flex` will stop `margin: auto` from working and the element is not centered anymore. – lampshade Jul 07 '20 at 10:15
  • if you want to center the button you can put them in a div and add text-align center. SEE MY UPDATED ANSWER – Niloy Jul 07 '20 at 10:21
  • Yeah, but if I don't need to center everything in the parent, then I need to add another wrapper for the button. I thought there might be a way to circumvent this. – lampshade Jul 07 '20 at 10:25
  • in this case, you need a wrapper, then you can use position: absolute with relative wrapper or simply text align center – Niloy Jul 07 '20 at 10:31