什么是“弹性"?和“证明内容"实现“文本对齐"不是吗 [英] What do "flex" and "justify-content" achieve that "text-align" doesn't?

查看:70
本文介绍了什么是“弹性"?和“证明内容"实现“文本对齐"不是吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这两种将按钮移到其父按钮右侧的方法似乎与我完全相同.有理由选择一个吗?在某些情况下flex可以将内容对齐到text-align可能不足的右侧?

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>

我很好奇,因为我注意到Bootstrap在版本3和版本4之间从text-align: right更改为flex,用于对齐模式的页脚部分中的按钮.

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.

为进一步说明:

推荐答案

是的,两者之间存在很大差异. Flexbox与框有关,而块级元素whearas text-align与文本和内联级元素有关.

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>

请注意,在flex容器中如何在文本和按钮之间不再留有空格,因为文本也将成为块元素 1 和按钮,在第二个示例中并非如此两者都是内联元素.到现在为止,这还可以,因为我们可以用保证金来纠正它.

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>

现在,我们之间有了明显的区别,我们可以看到flex容器将所有文本视为一个块元素,并且按钮将不会像第二个容器中那样跟随该文本.在某些情况下,这可能是预期的结果,但并非在所有情况下都是如此.

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>

flexbox容器越来越差!因为链接也是被阻塞化的 1 ,现在我们有4个块元素.链接前的文本,链接,链接后的文本和按钮.我们可以清楚地看到这种行为根本不是故意的.

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.

基本上,flexbox在对齐我们可以视为块元素或容器或盒子等的元素时很有用,但在涉及文本容器时则不是. text-align在对齐上一个block/box/container元素内的文本时更有用.

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.

换句话说,应该在文本级别使用text-align来对齐文本,图像等,而应该在更高级别使用flexbox来对齐块元素并创建布局.

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.

在您的情况下,没有什么大的不同,因为我们可以将按钮视为方框或内联元素.唯一的区别是,使用text-align时,如果将它们视为行内元素,则您将要面对的按钮之间的空白.

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 宽松地说,弹性容器的弹性项目是表示其流入内容的框.

1 Loosely speaking, the flex items of a flex container are boxes representing its in-flow contents.

弹性容器的每个流入子成为弹性项目,并且每个连续的子文本序列都包装在一个匿名块容器弹性项目中.但是,如果整个子文本序列只包含空格(即可能受white-space属性影响的字符),则不会呈现

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

弹性项目的显示值已被阻止

https://www.w3.org/TR/css-flexbox-1/#flex-items

这篇关于什么是“弹性"?和“证明内容"实现“文本对齐"不是吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆