将一个容器的一个元素在左上角的一个元素对齐,另一个使用Flexbox在右下角?
图像中的元素是使用绝对定位样式的,但是当我调整屏幕大小时,我必须稍微调整“待售”元素和'$ 400,000'元素的定位,因此我很好奇是否有一种使用FlexBox实现相同布局的方法?
是我的尝试( https://codepen.io/ob98/pen/pen/pen/pen/pen/eyvpjlj )
<div class = 'container'>
<p class='item1'>Top Left</p>
<p class='item2'>Bottom Right</p>
</div>
这 :
.container{
display: flex;
background: red;
width: 500px;
height: 200px;
color: white;
}
.item1{
align-self: flex-start;
}
.item2{
align-self: flex-end;
justify-self: flex-end; /* If align-self moved this to the bottom of the container vertically, I am thinking that this should move it to the end/right side of the container horizontally, but that is not working */
}
刚刚去了 https:// https://developer.mozilla.org/ en-us/doc/web/css/jusify-self ,并看到“在flexbox布局中,此属性被忽略(更多关于flexbox中的对齐)”,所以我想这解释了为什么这是不起作用的,但是我真的不明白为什么要忽略此属性...无论如何,是否有一种方法可以使用Flexbox实现此布局,还是我必须坚持绝对定位?感谢您的任何输入。
The element in the image is styled using absolute positioning but when I resize the screen I have to slightly adjust the positioning of both the 'For Sale' element and the '$400,000' element so I am curious as to if there is a way to achieve the same layout using flexbox?
Here is my attempt (https://codepen.io/ob98/pen/eYVPJLJ)
<div class = 'container'>
<p class='item1'>Top Left</p>
<p class='item2'>Bottom Right</p>
</div>
css:
.container{
display: flex;
background: red;
width: 500px;
height: 200px;
color: white;
}
.item1{
align-self: flex-start;
}
.item2{
align-self: flex-end;
justify-self: flex-end; /* If align-self moved this to the bottom of the container vertically, I am thinking that this should move it to the end/right side of the container horizontally, but that is not working */
}
Just went to https://developer.mozilla.org/en-US/docs/Web/CSS/justify-self and saw that "In flexbox layouts, this property is ignored (more about alignment in Flexbox)", so I guess that explains why that is not working but I don't really understand why this property is being ignored... Anyway, is there a way to achieve this layout using flexbox or do I have to stick to absolute positioning? Thanks for any input.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
添加到
.container
声明jusify-content:space-beten
之间,使您的p
都转到容器边缘,这已经完成了!Add to
.container
the declarationjustify-content: space-between
, to make both yourp
go to the container edges, and that's done!在容器中添加正当内容,并仅使用类似的Item2类将有所帮助。
Adding justify-content to container and just using the item2 class like this would help.
可以是答案,但我不确定。当我跑步时,它起作用了。
Can be the answer but im not sure. When i ran it worked.