我试图在ID包装器内垂直对齐元素。我给属性display:inline-flex;作为ID包装器的伸缩容器。
但是在表现上没有区别。我期望包装器ID中的所有内容都将内联显示。为什么不是呢?
#包装{
显示:inline-flex;
/*无差异显示:flex;* /
}
身体< >
< div id = "包装" >
<标题>标题头> < /
<导航> nav < / nav >
除了除了除了< > < / >
主要主要主要< > < / >
< / > <页脚页脚页脚>
< / div >
< /身体>
显示:inline-flex不会使伸缩项目内联显示。它使伸缩容器内联显示。这是display: inline-flex和display: flex之间的唯一区别。类似的比较可以在display: inline-block和display: block之间进行,以及几乎任何其他具有内联对应项的显示类型
对伸缩物品的效果绝对没有区别;无论Flex容器是块级还是内联级,Flex布局都是相同的。特别是,伸缩项本身总是表现得像块级盒子(尽管它们确实具有内联块的一些属性)。不能内联显示伸缩项;否则你就没有伸缩布局。
It is not clear what exactly you mean by "vertically align" or why exactly you want to display the contents inline, but I suspect that flexbox is not the right tool for whatever you are trying to accomplish. Chances are what you're looking for is just plain old inline layout (display: inline and/or display: inline-block), for which flexbox is not a replacement; flexbox is not the universal layout solution that everyone claims it is (I'm stating this because the misconception is probably why you're considering flexbox in the first place).
块布局和内联布局之间的区别超出了这个问题的范围,但最突出的一个是自动宽度:块级盒子水平拉伸以填充其包含的块,而内联级盒子收缩以适应其内容。事实上,仅仅因为这个原因,你几乎永远不会使用display: inline-flex,除非你有一个很好的理由内联显示你的flex容器。
显示:inline-flex不会使伸缩项目内联显示。它使伸缩容器内联显示。这是display: inline-flex和display: flex之间的唯一区别。类似的比较可以在display: inline-block和display: block之间进行,以及几乎任何其他具有内联对应项的显示类型
对伸缩物品的效果绝对没有区别;无论Flex容器是块级还是内联级,Flex布局都是相同的。特别是,伸缩项本身总是表现得像块级盒子(尽管它们确实具有内联块的一些属性)。不能内联显示伸缩项;否则你就没有伸缩布局。
It is not clear what exactly you mean by "vertically align" or why exactly you want to display the contents inline, but I suspect that flexbox is not the right tool for whatever you are trying to accomplish. Chances are what you're looking for is just plain old inline layout (display: inline and/or display: inline-block), for which flexbox is not a replacement; flexbox is not the universal layout solution that everyone claims it is (I'm stating this because the misconception is probably why you're considering flexbox in the first place).
块布局和内联布局之间的区别超出了这个问题的范围,但最突出的一个是自动宽度:块级盒子水平拉伸以填充其包含的块,而内联级盒子收缩以适应其内容。事实上,仅仅因为这个原因,你几乎永远不会使用display: inline-flex,除非你有一个很好的理由内联显示你的flex容器。
好吧,我知道一开始可能会有点混乱,但display指的是父元素,所以当我们说:display: flex;时,它指的是元素而当我们说display:inline-flex;时,它也使元素本身成为内联的。
这就像创建一个div内联或块,运行下面的代码片段,你可以看到如何显示flex分解到下一行:
.inline-flex {
display: inline-flex;
}
.flex {
display: flex;
}
p {
color: red;
}
<body>
<p>Display Inline Flex</p>
<div class="inline-flex">
<header>header</header>
<nav>nav</nav>
<aside>aside</aside>
<main>main</main>
<footer>footer</footer>
</div>
<div class="inline-flex">
<header>header</header>
<nav>nav</nav>
<aside>aside</aside>
<main>main</main>
<footer>footer</footer>
</div>
<p>Display Flex</p>
<div class="flex">
<header>header</header>
<nav>nav</nav>
<aside>aside</aside>
<main>main</main>
<footer>footer</footer>
</div>
<div class="flex">
<header>header</header>
<nav>nav</nav>
<aside>aside</aside>
<main>main</main>
<footer>footer</footer>
</div>
</body>
也可以快速创建下面的图像,一目了然地显示差异:
您可以内联显示伸缩项,前提是您的假设是基于首先需要灵活的内联项。使用flex意味着一个灵活的块级元素。
最简单的方法是使用flex容器,并将其子容器设置为flex属性。在代码方面,它看起来像这样:
.parent{
display: inline-flex;
}
.children{
flex: 1;
}
Flex: 1表示比率,类似于元素宽度的百分比。
检查这两个链接,以查看简单的Flexbox实例:
https://njbenjamin.com/bundle-3.htm
https://njbenjamin.com/bundle-4.htm
如果你用第一个例子:
https://njbenjamin.com/flex/index_1.htm
您可以摆弄浏览器控制台,在flex和内联flex之间更改容器元素的显示。