html - 我可以让设置为 justify-content: space-between 的元素在换行时向右对齐吗?
问题描述
我在一个容器中有两个元素,它们设置为“显示弹性”和“对齐内容之间的空间”。这使它们在容器中显示在彼此的另一侧。如果容器缩小,我希望左侧的元素堆叠在右侧元素的顶部并与右侧对齐。我现在遇到的行为是它们会换行,但是左侧的项目在右侧元素上方并与左侧对齐,而右侧的元素与右侧对齐。如何使它们都对齐?
请记住,我不能使用媒体查询,因为我有多个单元格会有不同的数据。因此,这些物品需要以我事先不知道的多种介质尺寸包装。请参阅我的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-content: flex-end
.
如果您希望在文本空间不足时产生效果,您也可以value
通过将第二个参数设置为1
. 它不会缩小超出其内容:
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 容器,因为第二个项目增长并且它的文本向右对齐
推荐阅读
- angular - angular unversal, angular 6 to 9, 安装新版本的 angular Universal
- python - 使用 tensorflow 嵌入查找
- node.js - React + Webpack + Material UI 样式在生产中中断
- r - 闪亮的代码(图形)在本地工作正常,但在服务器上出现对象未找到错误
- sql - 加入即时创建
- c++ - 如何实现在图像上创建漩涡的 C++ 函数
- javascript - 使用 AJAX 在动态引导选项卡中加载动态内容
- neo4j - Neo4j:查找属性名称包含字符串的节点(在属性名称中,而不是属性值中)
- angular - 如何对 Angular 拦截器进行单元测试
- python - GetGUIThreadInfo() 与 pywin32