如何在不更改其宽度的情况下将元素推到flexbox的结束
我将一些跨度放在Mat-Toolbar中,默认情况下具有显示flex。现在,我想要极右边的一些物品。我使用保证金左:这些元素的自动
。问题在于,这些元素是拉伸的,以填充整个Flexbox。我想要的是一些元素,然后是空白空间,然后是libgin-left:auto
具有其宽度为fit-content的元素。
这是我的代码:
html:
<div>
<span>Flex item</span>
<span class="right">Flex item</span>
<span class="right">Flex item</span>
</div>
css:
div {
display: flex;
border: 1px dotted black;
}
.right { margin-left: auto; }
span {
background-color: #E0E0E0;
border: solid 1px black;
padding: 10px
}
I am placing some spans inside mat-toolbar which by default has display flex. Now I want some of the items on the extreme right. I use margin-left: auto
for these elements. The problem is that these elements are stretched in order to fill the entire flexbox. What I want is some elements, followed by empty space, followed by the elements with margin-left: auto
having their width as fit-content.
This is my code:
HTML:
<div>
<span>Flex item</span>
<span class="right">Flex item</span>
<span class="right">Flex item</span>
</div>
CSS:
div {
display: flex;
border: 1px dotted black;
}
.right { margin-left: auto; }
span {
background-color: #E0E0E0;
border: solid 1px black;
padding: 10px
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据您所说的话,我知道您想要这样的东西...
使用
Flexbox
是实现它的正确方法,但是您必须添加一些属性以使其按照所需的方式使其样式。使用:
合理性:
之间的空格>将项目对齐到极端(左右);对齐项目:中心
垂直对齐中心,您在HTML上的信息。创建2个Divs
将两个divs
与极端对齐。这是一个例子:
Based on what you said, I understood you wanted something like this...
Using
flexbox
is the correct way to achieve it, but you must add some properties to make it style the way you want.Use:
justify-content: space-between
to align the items into the extremes (left and right);align-items: center
to align vertically to the center, your information on HTML.Create 2 divs
to align bothdivs
to the extremes.Here is the example: