将一个容器的一个元素在左上角的一个元素对齐,另一个使用Flexbox在右下角?

发布于 2025-02-05 19:13:37 字数 1368 浏览 1 评论 0原文

图像中的元素是使用绝对定位样式的,但是当我调整屏幕大小时,我必须稍微调整“待售”元素和'$ 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实现此布局,还是我必须坚持绝对定位?感谢您的任何输入。

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

A君 2025-02-12 19:13:37

添加到.container声明jusify-content:space-beten之间,使您的p都转到容器边缘,这已经完成了!

.container {
  display: flex;
  justify-content: space-between;
  background: red;
  width: 500px;
  height: 200px;
  color: white;
}

.item1 {
  align-self: flex-start;
}

.item2 {
  align-self: flex-end;
  justify-self: flex-end;
}
<div class = 'container'>
  <p class='item1'>Top Left</p>
  <p class='item2'>Bottom Right</p>
</div>

Add to .container the declaration justify-content: space-between, to make both your p go to the container edges, and that's done!

.container {
  display: flex;
  justify-content: space-between;
  background: red;
  width: 500px;
  height: 200px;
  color: white;
}

.item1 {
  align-self: flex-start;
}

.item2 {
  align-self: flex-end;
  justify-self: flex-end;
}
<div class = 'container'>
  <p class='item1'>Top Left</p>
  <p class='item2'>Bottom Right</p>
</div>

淡水深流 2025-02-12 19:13:37

在容器中添加正当内容,并仅使用类似的Item2类将有所帮助。

.container{
  display: flex;
  justify-content: space-between;
  background: red;
  width: 500px;
  height: 200px;
  color: white;
}

.item2{
  align-self: flex-end;  
}

Adding justify-content to container and just using the item2 class like this would help.

.container{
  display: flex;
  justify-content: space-between;
  background: red;
  width: 500px;
  height: 200px;
  color: white;
}

.item2{
  align-self: flex-end;  
}
浪推晚风 2025-02-12 19:13:37

可以是答案,但我不确定。当我跑步时,它起作用了。

.bg-flex {
  width: 100vw;
  height: 100vh;
  background-image: url('https://media.istockphoto.com/photos/the-city-of-london-skyline-at-night-united-kingdom-picture-id1312550959');
  display: flex;
  flex-direction: column;
}

.for-sale {
  height: 50%;
  display: flex;
  flex-direction: row;
  align-items: flex-start;
  justify-content: flex-start;
}

.k-400 {
  height: 50%;
  display: flex;
  flex-direction: row;
  align-items: flex-end;
  justify-content: flex-end;
}

.example {
  width: 300px;
  height: 100px;
  background-color: aqua;
}
<div class="bg-flex">
  <div class="for-sale">
    <div class="example">

    </div>

  </div>
  <div class="k-400">
    <div class="example">

    </div>
  </div>
</div>

Can be the answer but im not sure. When i ran it worked.

.bg-flex {
  width: 100vw;
  height: 100vh;
  background-image: url('https://media.istockphoto.com/photos/the-city-of-london-skyline-at-night-united-kingdom-picture-id1312550959');
  display: flex;
  flex-direction: column;
}

.for-sale {
  height: 50%;
  display: flex;
  flex-direction: row;
  align-items: flex-start;
  justify-content: flex-start;
}

.k-400 {
  height: 50%;
  display: flex;
  flex-direction: row;
  align-items: flex-end;
  justify-content: flex-end;
}

.example {
  width: 300px;
  height: 100px;
  background-color: aqua;
}
<div class="bg-flex">
  <div class="for-sale">
    <div class="example">

    </div>

  </div>
  <div class="k-400">
    <div class="example">

    </div>
  </div>
</div>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文