Flexbox:两列中的三个元素

发布于 2025-02-06 00:51:10 字数 1144 浏览 2 评论 0原文

我在一个LI项目中有三个元素:

<li>
    <div class="on-offer">
        <img src="#" />
        <p>Title</p>
        <a href="#">Link text</a>
    </div>
</li>

我想使用Flexbox在左手图像中创建两个列,然后在右侧的段落和图像中创建两个列,但彼此之间的顶部。

到目前为止,我在SCSS中有:

.on-offer {
        text-align:center;
        display:flex;
        flex-wrap:wrap;
        justify-content:space-between;
        p {
            font-family:$serif;
            font-style:italic;
            font-weight:400;
            color:#cc0000;
            flex:1;
        }
        img {
            width:50px;
            height:auto;
            display:block;
        }
        a {
            display:block;
            text-decoration:none;
            color:$black;
            font-family:$sans-serif;
            flex:1;
        }
    }

有点有效。

创建了两个列,但是我无法获得段落和锚点彼此顶部。电子商务和发现单词是并排的,不像上图中的单词。

任何建议都将被感激地接受。

I have three elements in a li item:

<li>
    <div class="on-offer">
        <img src="#" />
        <p>Title</p>
        <a href="#">Link text</a>
    </div>
</li>

and I would like to use Flexbox to create two columns, in the left hand the image and then the paragraph and image in the right hand side but on top of each other.

enter image description here

So far I have in my SCSS:

.on-offer {
        text-align:center;
        display:flex;
        flex-wrap:wrap;
        justify-content:space-between;
        p {
            font-family:$serif;
            font-style:italic;
            font-weight:400;
            color:#cc0000;
            flex:1;
        }
        img {
            width:50px;
            height:auto;
            display:block;
        }
        a {
            display:block;
            text-decoration:none;
            color:$black;
            font-family:$sans-serif;
            flex:1;
        }
    }

It kinda works.

Two columns are created, but I can't get the paragraph and anchor to sit top of each other. The words Ecommerce and discover are side by side and not like in the image above.

Any suggestions would be gratefully accepted.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

蓝色星空 2025-02-13 00:51:10

使用FlexBox不可能,因为您无法重叠2个轴,您要在这里尝试进行。
要么使用floats网格布局类似:

div {
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: 1fr 1fr;
}

.img {
  widht: 50px;
  height: 100px;
  background: red;
  grid-area: 1 / 1 / 3 / 2;
}

p {
  grid-area: 1 / 2 / 2 / 3;
  background: blue;
  margin: 0;
}

a {
  display: block;
  grid-area: 2 / 2 / 3 / 3;
  background: green;
}
<div>
  <div class="img"></div>
  <p>Text</p>
  <a href="#">Link</a>
</div>

Its not possible with flexbox because you cant overlap 2 axes which you are trying to do here.
Either use floats or grid layout like:

div {
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: 1fr 1fr;
}

.img {
  widht: 50px;
  height: 100px;
  background: red;
  grid-area: 1 / 1 / 3 / 2;
}

p {
  grid-area: 1 / 2 / 2 / 3;
  background: blue;
  margin: 0;
}

a {
  display: block;
  grid-area: 2 / 2 / 3 / 3;
  background: green;
}
<div>
  <div class="img"></div>
  <p>Text</p>
  <a href="#">Link</a>
</div>

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