使柔性项目的内容宽度,而不是父容器的宽度 [英] Make flex items take content width, not width of parent container
问题描述
我有一个容器< div>
,其中显示:flex
。它有一个小孩< a>
。
我怎样才能让孩子出现内联?
$ b
$ b $具体来说,我怎样才能让孩子的宽度由其内容决定,而不是扩大到父母的宽度? b
我试过了: 我把孩子设置为 示例: http://codepen.io/donpinkus/pen/YGRxRY 不需要 一个flex容器的初始设置是 由于您的容器是 了解更多关于 cross axis 详细了解沿着主轴 p> I have a container How can I make the child appear "inline"? Specifically, how can I make the child's width determined by its content, and not expand to the width of the parent? What I tried: I set the child to Example:
http://codepen.io/donpinkus/pen/YGRxRY Use No need for An initial setting of a flex container is The By default, Since your container is You can override the default with Learn more about flex alignment along the cross axis here: Learn more about flex alignment along the main axis here: 这篇关于使柔性项目的内容宽度,而不是父容器的宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
display:inline-flex
,但它仍然占用了整个宽度。我也尝试了所有其他的显示属性,但没有任何效果。
.container {background:red; height:200px; flex-direction:column;填充:10px; display:flex;} a {display:inline-flex;填充:10px 40px;背景:粉红色;}
< div class =container > < a href =#>测试< / a>< / div>
align-items:flex-start
align-self:flex-start
在弹性项目上。
显示:inline-flex
。
align-items:stretch
。这意味着flex项目将扩展到沿横轴覆盖整个容器。
align-items
完全相同,除了 align-self
适用于 flex项目而 align-items
适用于 flex容器。 通过默认情况下, align-self
继承 align-items
。
flex-direction:column
,横轴是水平的, align-items:flex-start
align-self:flex-start
。
$ b
<div>
with display: flex
. It has a child <a>
. display: inline-flex
, but it still took up the full width. I also tried all other display properties, but nothing had an effect. .container {
background: red;
height: 200px;
flex-direction: column;
padding: 10px;
display: flex;
}
a {
display: inline-flex;
padding: 10px 40px;
background: pink;
}
<div class="container">
<a href="#">Test</a>
</div>
align-items: flex-start
on the container, or align-self: flex-start
on the flex items.display: inline-flex
.
align-items: stretch
. This means that flex items will expand to cover the full length of the container along the cross axis.align-self
property does the same thing as align-items
, except that align-self
applies to flex items while align-items
applies to the flex container.align-self
inherits the value of align-items
.flex-direction: column
, the cross axis is horizontal, and align-items: stretch
is expanding the child element's width as much as it can.align-items: flex-start
on the container (which is inherited by all flex items) or align-self: flex-start
on the item (which is confined to the single item).