我在容器中有两个元素,设置为“显示 flex ”和“调整内容之间的空间”。这使得它们显示在容器中彼此相对的一侧。如果容器缩小,我希望左侧的元素堆叠在右侧元素的顶部并向右对齐。我现在遇到的行为是它们换行,但左侧的元素位于右侧元素之上并且向左对齐,而右侧的元素向右对齐。我如何使它们都正确对齐?
请记住,我无法使用媒体查询,因为我有多个具有不同数据的单元格。因此,这些元素需要以多种介质尺寸进行包装,而我事先不会意识到这一点。请看我的CodePen了解更多详情。
.u-relative {
position: relative;
}
.u-centerY {
display: flex;
align-items: center;
}
.u-fillRemaining {
flex: 1;
}
.u-spaceBetweenX {
justify-content: space-between;
}
.u-wrap {
flex-wrap: wrap;
}
.Rtable-cell {
padding: 0 0.625rem;
max-width: 12rem;
min-width: 6.25rem;
border: 1px solid black;
}
.table-sum {
// visibility: hidden;
text-transform: uppercase;
color: Grey;
font-size: 0.75rem;
}
.t-alignRight {
text-align: right;
}
@media only screen and (max-width: 400px) {
.u-spaceBetweenX {
justify-content: flex-end;
}
}
<div class="u-relative Rtable-cell">
<div class="u-centerY u-fillRemaining u-spaceBetweenX u-wrap">
<span class="table-sum">sum</span>
<div class="u-fillRemaining t-alignRight">
<p>$4,000,000.00</p>
</div>
</div>
</div>
最佳答案
justify-content: flex-end
使 Flex 容器内的元素向右对齐。
设置flex: 1 0 6rem
表示值区域可以增长,不能收缩,其基本大小为父级大小的一半。
一旦容器的大小缩小到两个元素可以容纳在同一行中的程度,第二个元素就会换行到第二行,这使得第一个元素粘在右侧(因为 justify-内容:flex-end
.
如果您希望在文本空间不足时达到效果,可以通过将第二个参数设置为 1
来缩小 value
容器。它不会缩小超出其内容:
flex: 1 1 6rem; /* now the minimum size can be less than 6rem if the text is shorter */
setInterval(() => {
$('.width-change').toggleClass('short');
}, 2000);
.kb-container {
padding: 0 0.625rem;
max-width: 12rem;
min-width: 6.25rem;
border: 1px solid black;
display: flex;
flex-wrap: wrap;
justify-content: flex-end;
background: white;
}
.kb-value {
flex: 1 0 6rem;
text-align: right;
}
/* not necessary. just for this demo */
.width-change {
width: 15rem;
height: 7rem;
transition: all 250ms;
background: lightgrey;
}
.width-change.short {
width: 9rem;
}
h3 {
margin-bottom: 0;
}
.sub {
margin: 3px 0;
}
body {
overflow: hidden;
}
html {
box-sizing: border-box;
}
html,
body {
margin: 0;
padding: 0;
}
*,
*:before,
*:after {
box-sizing: inherit;
}
.kb-example {
width: 45vw;
display: inline-block;
height: 100vh;
}
.kb-example.kb-colorized .kb-text {
background-color: #ccffcc;
}
.kb-example.kb-colorized .kb-value {
background-color: #ccffff;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="kb-example">
<h3>align items to the right using flex</h3>
<div class="sub">(container will resize to demo the behavior)</div>
<div class="width-change">
<!-- YOU ONLY NEED THIS -->
<div class="kb-container">
<span class="kb-text">sum</span>
<div class="kb-value">
$4,000,000.00
</div>
</div>
<!-- END -->
</div>
</div>
<!-- with colorized elements -->
<div class="kb-example kb-colorized">
<div class="sub">and now with x-ray:</div>
<div class="width-change">
<!-- YOU ONLY NEED THIS -->
<div class="kb-container">
<span class="kb-text">sum</span>
<div class="kb-value">
$4,000,000.00
</div>
</div>
<!-- END -->
</div>
</div>
这是一个 codepen (这仅包含此行为所需的代码,没有演示的额外代码)
尽管这是一个 flex-end
容器,但它的行为类似于 space- Between
Flex 容器,因为第二个元素会增长并且其文本向右对齐
关于html - 我可以制作设置为 justify-content : space-between align to the right when they wrap? 的元素吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50122870/