Why is an internal alignment of a <button> button different from a <a> link, even using the same class?

Asked

Viewed 372 times

11

Well, I created a class called .btn, where I wanted to use it so much to button as for a. So I pushed a few things in this class, to reset the original style, both of the button as in the a.

For example, in the a I removed the text-decoration. In the button, forced the change of background and border. So far so good.

The problem is that I’m noticing that the element button, even using the -webkit-appearence: none, has a different behavior in relation to the alignment of the internal text, according to the test below:

.btn{
    border: 1px solid transparent;
    padding: 10px 20px;
    box-shadow: 0 1px 2px 0px #666;
    outline: none;
    border-radius: 4px;    
    background-color: #efefef;
    cursor: pointer;
    transition: box-shadow .1s ease-out, border-color .3s linear;
    font-weight: bold;
    box-sizing: border-box;
    vertical-align: top;
	  display: inline-block;
	
    min-height: 55px;
    font-size: 14px;
	  
	  text-decoration: none;
	  color: #222;
	  background-image: none;

    -webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;
    font-family: Arial;

}
<button class="btn">
  Eu sou um &lt;Button&gt;
</button>

<a class="btn">
    Eu sou um &lt;A&gt;
</a>

I’ll put the image too, to show how the rendering looked in my Google Chrome:

Button e A, alinhamentos diferentes para a mesma classe

Now I have my doubts:

  • What property makes the button align the texts in the middle, while the a nay?

  • Is there a way to reset this standard alignment of button? My intention was to leave the two centralized, but more importantly than that, I want to know if there is a property of the button made it align to the center.

Observing: The question is not only intended to know how to align the text to the center, but to know in depth about what caused the difference mentioned above, since I used the same style for all.

Observation 2: I could use inline-flex and match justify-content: center; align-itens: center, but that’s not the answer I want, because I’m already putting it in the question. I want to know specifically about the details presented above, and not just a "support for the alignment problem"

  • Already checked which styles are assigned by <button> and <a> by default in any browser.

  • 1

    @Jorgecosta I copied all user-agent styles and applied to the selector a {...} nothing happens, this is behavior of the element Button as if isolating the text to, different from the A that the internal text accompanies the vertical-align: top

  • but found that the button has line-height, in safari on macOS is line-height: 13px;

  • 1

    @Jorgecosta up copying user-agent line-height to <button> the result is the same.

  • To test remove min-height: 55px; and place line-height: 3; padding: 0px;

  • @Jorgecosta if you put min-height: 1000px on the button, it gets everything in between, in the same way. I don’t think it’s just the line-height.

  • Dear @Jorgecosta take the min-height does not explain how the text in <button> aligns vertically to the center. Here nobody is looking for solutions how to solve, but rather understand why this occurs, as the author of the question already explained in his own question.

  • Yeah I’m trying to figure it out too

  • 1

    Dear @Jorgecosta I understand, but your comments were all about strip this, does that, ends up making understand that this trying to apply solutions and not trying to understand the behavior of the box-model: https://www.w3.org/TR/CSS2/box.html, but I think we are already clear of everything :)

  • I was checking what the browser itself applies by default, if to find the solution I would limit myself to applying the solutions already found in frameworks such as bootstrap. But since you are clear on the question I would appreciate you putting a full answer to the question and so share with all.

Show 5 more comments

1 answer

1

Updated - 08/11/2018

After much research despite understanding the question in various ways I researched and got a more plausible answer with sources for your question.

The answer is from Cubed Eye Stack overflow user.

Firstly, the main issue here is that <button> at least in Firefox are built with an internal element between the tag <button> and your children. In Firefox it is called moz-button-content is not something that can be achieved with CSS and has been set to display the block without inheriting the height of the button, you can see this style statement in useragent style sheet.

The default browser is already encoded to center the contents of the button vertically.

With these two questions you can start to see how the button forces the content to be centered, consider:

<button>

+------------------------+ ^
| button extra space     | |
|                        | |
+------------------------+ |
|| ::moz-button-content || | button height
||   display: block;    || |
+------------------------+ |
|                        | |
| button extra space     | |
+------------------------+ v

There is an alternative solution to the problem if you really want to change the default behavior, but this does not completely solve the problem depending on your implementation.

If you insert a wrapper <span> with display: block the only child of the button and put all its content in it, you can use it to skip the moz-button-content element.

You will need to make this <span> element has height: inherit correctly filled the height of the button and then add your normal button style to the <span> instead, you will get basically the desired behavior.

* {
box-sizing: border-box;
margin: 0;
border: 0;
padding: 0;
font-family: san-serif;
background: none;
font-size: 1em;
line-height:1;
vertical-align: baseline;
}

button, a {
height: 3em;
}

button {
background: red;
}
button::-moz-focus-inner {
border: 0;
padding: 0;
outline: 0;
}

button > span {
display: block;
height: inherit;
}

a {
display:inline-block;
background: green;
}



button.styled > span , a.styled{
padding: 10px;
background: yellow;
}
<button>
<span>Button content</span>
</button>

<a>
<span>Link Content</span>
</a>

<br/>
<br/>

<button class="styled">
<span>Button content</span>
</button>

<a class="styled">
<span>Link Content</span>
</a>

You should use one on <span> time of a <div>, as div are not children valid for <button>.

  • 1

    No friend I think you misunderstood the question or are mistaken. The questioner wants to know why the text inside btn is vertically aligned by default. And it’s not because of the vertical-align, because even putting on the button for example vertical-align: bottom !important the text remains centered vertically in the middle of the button, so that’s not what makes the btn text align centered. Summarizing puts vertical-align button and tries to align content other than in the center of btn, it will not work...

  • @hugocsl, I edited the answer, I ask you to review again, I really thought it was just about alignment, I added new remarks.

  • It would be nice to put a snippet there but I think the answer is wrong. I’ll turn on my PC in a little while and I’ll test

  • I don’t think it’s from the operating system, it’s from the browser’s default user-agent. input, select, checkbox, all are stylized by the user-agent and not by the operating system, so much so that in safari for windows (even old), the styles were the same as the Mac, so I guess OS has nothing to do with it... Then do the <a> become the same as <button> It is quite different to make the button look like ai A... To align vertically there are 1000 formulas, but why does the button do it by default? because there’s no way to change it? If you can, put it there. And if you have a source help.

  • About leaving <button> equal to <a> look again at the answer, edited.

  • Guy just give a button all:unset, not even need to write all this css to "reset" the default styles of the button... but even zeroing everything the text remains in the center vertically, just put a height in btn that you will see

  • @hugocsl updated, I think with the answer answers everything, really was getting it wrong and I had to look for almost the whole night to find the answer I was stunned.

  • 1

    Take a file without any css, create a button, put a span in with display:block and height 100px. It will be aligned at the top vertically after with Transform: translateY vc aligns where you want for example. Putting one element inside the other you can do whatever you want.... What is not clear is that the text inside the btn is always in the middle vertical... Despite your willingness to help I don’t think we have an answer yet.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.