admin管理员组文章数量:1394203
I'm trying to get an item in a flexbox layout to fill all available space inside the container if it can, or be centered if it can't due to its max-width
/max-height
constraints. In the snippet below, it works like I want along the main axis, but not along the cross axis. When using align-items: center
, the item is centered, but has minimum size. With align-items: stretch
it has maximum size, but is no longer centered. How do I make it work? I'd prefer to avoid additional html elements.
.container {
background: blue;
width: 200px;
height: 200px;
display: flex;
justify-content: center;
align-items: center;
/* align-items: stretch; */
}
.item {
flex: 1;
background: red;
min-width: 20px;
min-height: 20px;
max-width: 100px;
max-height: 100px;
}
<div class="container">
<div class="item"></div>
</div>
I'm trying to get an item in a flexbox layout to fill all available space inside the container if it can, or be centered if it can't due to its max-width
/max-height
constraints. In the snippet below, it works like I want along the main axis, but not along the cross axis. When using align-items: center
, the item is centered, but has minimum size. With align-items: stretch
it has maximum size, but is no longer centered. How do I make it work? I'd prefer to avoid additional html elements.
.container {
background: blue;
width: 200px;
height: 200px;
display: flex;
justify-content: center;
align-items: center;
/* align-items: stretch; */
}
.item {
flex: 1;
background: red;
min-width: 20px;
min-height: 20px;
max-width: 100px;
max-height: 100px;
}
<div class="container">
<div class="item"></div>
</div>
Share
Improve this question
asked Mar 12 at 16:25
yuri kilochekyuri kilochek
13.6k2 gold badges33 silver badges63 bronze badges
3
|
3 Answers
Reset to default 1I would define the size differently:
.container {
background: blue;
width: 200px;
height: 200px;
display: flex;
justify-content: center;
align-items: center;
/* resize the container to debug*/
resize: both;
overflow: hidden;
}
.item {
width: clamp(20px, 100%, 100px);
height: clamp(20px, 100%, 100px);
background: red;
border: 2px solid green;
box-sizing: border-box;
}
<div class="container">
<div class="item"></div>
</div>
Keep align-items: center;
and set height: 100%;
on the .item
:
.container {
background: blue;
width: 200px;
height: 200px;
display: flex;
justify-content: center;
align-items: center;
}
.item {
height: 100%;
flex: 1 1 100%;
background: red;
min-width: 20px;
min-height: 20px;
max-width: 100px;
max-height: 100px;
}
<div class="container">
<div class="item"></div>
</div>
Or use align-items: stretch;
and center the .item
with position: relative; top: 50%; translate: 0 -50%;
:
.container {
background: blue;
width: 200px;
height: 200px;
display: flex;
justify-content: center;
align-items: stretch;
}
.item {
position: relative;
top: 50%;
translate: 0 -50%;
flex: 1;
background: red;
min-width: 20px;
min-height: 20px;
max-width: 100px;
max-height: 100px;
}
<div class="container">
<div class="item"></div>
</div>
It's already centered the only issue is that it's not at full height. Setting max-height
isn't going to bring .item
height to max especially since it has no content. Setting the height explicitly will.
.item {
...
height: 100px;
}
.container {
background: blue;
width: 200px;
height: 200px;
display: flex;
justify-content: center;
align-items: center;
/* align-items: stretch; */
}
.item {
flex: 1;
background: red;
min-width: 20px;
min-height: 20px;
max-width: 100px;
max-height: 100px;
height: 100px;
/* That's all that's needed */
}
<div class="container">
<div class="item"></div>
</div>
本文标签: htmlHow to stretch and center an element inside flexbox along cross axisStack Overflow
版权声明:本文标题:html - How to stretch and center an element inside flexbox along cross axis? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744740056a2622554.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
height: 100px
to.item
. – zer00ne Commented Mar 12 at 16:34align-self
is a more granular way to sayalign-items
on the container. It exhibits the same issue. – yuri kilochek Commented Mar 12 at 18:40