These two ways to move a button to the right of its parent seem completely equivalent to me. Is there a reason to choose one over the other? Are there circumstances in which flex
works to align content to the right where text-align
might not suffice?
.parent {
text-align: right;
}
<div class="parent"><button>Awesome button!</button></div>
.parent {
display: flex;
justify-content: flex-end;
}
<div class="parent"><button>Awesome button!</button></div>
I'm curious because I noticed that Bootstrap changed from text-align: right
to flex
between versions 3 and 4 for aligning buttons in a modal's footer section.
To illustrate further:
Yes there is a big difference. Flexbox is about boxes and block level element whearas text-align
is about text and inline level element.
When having one element we won't notice the difference but when it comes to multiple element we can see a clear difference.
Here is a basic example where we have text and button inside a container:
.parent-flex {
display: flex;
justify-content: flex-end;
margin-bottom:10px;
}
.parent-normal {
text-align:right;
}
<div class="parent-flex">some text here <button>Awesome button!</button></div>
<div class="parent-normal">some text here <button>Awesome button!</button></div>
Note how in the flex container we no more have white space between the text and the button because the text will become a block element1 and the button too which is not the case in the second example where both are inline element. Until now, it's ok because we can rectify this with margin.
Let's put more text and see the difference again:
.parent-flex {
display: flex;
justify-content: flex-end;
margin-bottom:10px;
}
.parent-normal {
text-align:right;
}
<div class="parent-flex">some text here some text here some text here some text here some text here some text here some text here some text here some text here some text here<button>Awesome button!</button></div>
<div class="parent-normal">some text here some text here some text here some text here some text here some text here some text here some text here some text here some text here<button>Awesome button!</button></div>
Now we have a clear difference and we can see that the flex container consider all the text as a block element and the button will not follow the text like in the second container. In some case it can be an intended result but not in all the cases.
Let's add a link inside our text:
.parent-flex {
display: flex;
justify-content: flex-end;
margin-bottom:10px;
}
.parent-normal {
text-align:right;
}
<div class="parent-flex">some text here some text here some text here some text here some text here some text here some text <a href="">link</a> here some text here some text here some text here<button>Awesome button!</button></div>
<div class="parent-normal">some text here some text here some text here some text here some text here some text here some text <a href="">link</a> here some text here some text here some text here<button>Awesome button!</button></div>
The flexbox container is getting worse! because the link is also blockified1 and now we have 4 block elements. The text before the link, the link, the text after and the button. We can clearly see that this behavior is not intended at all.
Basically flexbox is useful when it comes to align element that we can consider as block element or container or boxes, etc but not when it comes to text container. text-align
is more useful to align text inside the previous block/box/container element.
In other words, text-align
should be used at text level to align text, images, etc and flexbox should be considered at an upper level to align block element and create layouts.
In your case, there is no big difference since we can consider button as boxes or inline-element. the only difference will be the whitespace between the button that you will face if you consider them as inline element when using text-align
.
1 Loosely speaking, the flex items of a flex container are boxes representing its in-flow contents.
Each in-flow child of a flex container becomes a flex item, and each contiguous sequence of child text runs is wrapped in an anonymous block container flex item. However, if the entire sequence of child text runs contains only white space (i.e. characters that can be affected by the white-space property) it is instead not rendered
The display value of a flex item is blockified
https://www.w3.org/TR/css-flexbox-1/#flex-items
flex-box system provides more powerful features when it comes to aligning your content. If you have three buttons and you want them to be placed equally distant from each other, or equally distant from each other and container boundary you can give space-between
or space-evenly
value to justify-content
property to the container. You cannot do that with text-align or float.
Yes, the reason is vertical centering. The align-items: center
is the key here, which allows the buttons to be vertically centered in the modal footer. This is hard to do without flex-boxes - you would need to resort to "hacks" like using position:absolute
, or adding some precalculated amounts of padding
on both sides, etc. Flexes (and Grids) allow developers to define layouts more succinctly.
To answer your question - they didn't opt for justify-content:flex-end
instead of text-align:right
- instead, they opted for flex
instead of block
as the display box model (for vertical centering), and the justify-content
usage comes naturally from that decision.