我如何在Div中获取物品以垂直堆叠

发布于 2025-02-09 07:55:57 字数 1460 浏览 0 评论 0原文

我试图制作一个模拟的在线商店,无法弄清楚如何用其图像堆叠项目描述。我在网格容器中有所有东西。我可以单独集中每个元素,但是当我尝试将图像堆叠在删除/下拉/下拉按钮上时,它们会在框中创建2个新列。有没有办法将图像放在描述的顶部,然后将两个元素放在下拉菜单和按钮的顶部?

#column_left{
display: grid;
width:100%;
height: 900px;
border: solid 2px black;
grid-template-rows: 33% 33% 33%;
}

#column_right {
    display: grid;
    width:100%;
    height: 900px;
    border: solid 2px black;
}

.item {
    border: solid 2px black;
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center; 
}

.sample_img {
    height: 50%;
    width: auto;
}
<div class="item">
    <img class="sample_img" src="https://st2.depositphotos.com/3080513/7014/i/600/depositphotos_70144489-stock-photo-t-shirt-template.jpg">

    <p>Sticks T-shirt</p>
    <label for="size">Choose a Size</label>
    <select name="size" class="size" >
        <option value="XS">XS</option>
        <option value="S">S</option>
        <option value="M">M</option>
        <option value="L">L</option>
        <option value="XL">XL</option>
        <option value="XXL">XXL</option>
    </select>
    <button class="addToCart" type="submit">Add to Cart</button>

</div>

Im trying to make a mock online store and cannot figure out how to stack an items description with its image. I have everything inside a grid container. I can center each element individually but when I try to stack the image over the desription/drop down/button they create 2 new columns in the box. Is there a way I can put the image on top of the description and then both of those elements on top of the dropdown menu and button?

#column_left{
display: grid;
width:100%;
height: 900px;
border: solid 2px black;
grid-template-rows: 33% 33% 33%;
}

#column_right {
    display: grid;
    width:100%;
    height: 900px;
    border: solid 2px black;
}

.item {
    border: solid 2px black;
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center; 
}

.sample_img {
    height: 50%;
    width: auto;
}
<div class="item">
    <img class="sample_img" src="https://st2.depositphotos.com/3080513/7014/i/600/depositphotos_70144489-stock-photo-t-shirt-template.jpg">

    <p>Sticks T-shirt</p>
    <label for="size">Choose a Size</label>
    <select name="size" class="size" >
        <option value="XS">XS</option>
        <option value="S">S</option>
        <option value="M">M</option>
        <option value="L">L</option>
        <option value="XL">XL</option>
        <option value="XXL">XXL</option>
    </select>
    <button class="addToCart" type="submit">Add to Cart</button>

</div>

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

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

发布评论

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

评论(3

聽兲甴掵 2025-02-16 07:55:57

在这种情况下,将其添加到.Item类

flex-direction: column;

中,图片和其余内容将在另一个下方

.item {
    border: solid 2px black;
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center; 
    flex-direction: column;
}

Add this to .item class

flex-direction: column;

in this case, the picture and the rest of the content will be one under the other

.item {
    border: solid 2px black;
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center; 
    flex-direction: column;
}
冰之心 2025-02-16 07:55:57

您应该能够设置弹性方向。
在这种情况下,您可能不需要使用网格。

flex-direction: row;
flex-direction: column;

是您大多可能选择的两个选项。改变弹性方向将改变证明内容和对齐项目的合理性,从本质上扭转它们。您也可以执行行反向和列反向,如果需要,将其向后进行。

You should be able to set a flex-direction.
You probably don't need to use grid in this situation.

flex-direction: row;
flex-direction: column;

are the two options you will mostly likely choose from. Changing the flex direction will change what justify content and align items do though, essentially reversing them. You can also do row-reverse and column-reverse to make it backwards if you want.

谁对谁错谁最难过 2025-02-16 07:55:57

需要使用flex-directionflex-flow column在这种情况下,在这种情况下,.Item

#column_left {
  display: grid;
  width: 100%;
  height: 900px;
  border: solid 2px black;
  grid-template-rows: 33% 33% 33%;
}

#column_right {
  display: grid;
  width: 100%;
  height: 900px;
  border: solid 2px black;
}

.item {
  border: solid 2px black;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  gap: 1em;
}

button {
  margin-bottom: 1em;
}

.sample_img {
  height: 50%;
  width: auto;
  max-width:100%
}

.item>div:first-child {
  text-align: center;
}
<div class="item">
  <div>
    <img class="sample_img" src="https://st2.depositphotos.com/3080513/7014/i/600/depositphotos_70144489-stock-photo-t-shirt-template.jpg">
    <p>Sticks T-shirt</p>
  </div>
  <label for="size">Choose a Size</label>
  <select name="size" class="size">
    <option value="XS">XS</option>
    <option value="S">S</option>
    <option value="M">M</option>
    <option value="L">L</option>
    <option value="XL">XL</option>
    <option value="XXL">XXL</option>
  </select>
  <button class="addToCart" type="submit">Add to Cart</button>
</div>

Need to use flex-direction or flex-flow column on the parent, in this case .item.

#column_left {
  display: grid;
  width: 100%;
  height: 900px;
  border: solid 2px black;
  grid-template-rows: 33% 33% 33%;
}

#column_right {
  display: grid;
  width: 100%;
  height: 900px;
  border: solid 2px black;
}

.item {
  border: solid 2px black;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  gap: 1em;
}

button {
  margin-bottom: 1em;
}

.sample_img {
  height: 50%;
  width: auto;
  max-width:100%
}

.item>div:first-child {
  text-align: center;
}
<div class="item">
  <div>
    <img class="sample_img" src="https://st2.depositphotos.com/3080513/7014/i/600/depositphotos_70144489-stock-photo-t-shirt-template.jpg">
    <p>Sticks T-shirt</p>
  </div>
  <label for="size">Choose a Size</label>
  <select name="size" class="size">
    <option value="XS">XS</option>
    <option value="S">S</option>
    <option value="M">M</option>
    <option value="L">L</option>
    <option value="XL">XL</option>
    <option value="XXL">XXL</option>
  </select>
  <button class="addToCart" type="submit">Add to Cart</button>
</div>

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