简体   繁体   English

css 方形限制大小和 position 到父 div

[英]css square limit size and position to parent div

I am trying to get a square to have a limited size and stay within the bounds of the parent div and scale cleanly.我试图让一个正方形的大小有限,并保持在父 div 的范围内并干净地缩放。 I can get one or two of these, but not all.我可以得到其中的一两个,但不是全部。

Setup is I have a main div, two column divs on the left, and a div that takes up the remaining space on the right.设置是我有一个主 div,左边有两列 div,右边有一个 div 占用剩余空间。 I would like the div on the right to contain the square and the bounds of the square stay within the parent div.我希望右侧的 div 包含正方形,并且正方形的边界保持在父 div 内。 I can get it to stay put if I don't use the padding-bottom to keep it square, but then the pic of the item looks horrible.如果我不使用 padding-bottom 让它保持正方形,我可以让它保持原样,但是这个项目的图片看起来很糟糕。

Take a look at my js bin .看看我的js bin The pathway to hit the square is: Weapons -> item b1 -> click on it to make it stay击中方块的途径是:武器->物品b1->点击使其停留

The padding-bottom pushes it way down and outside of the parent div and blows the bottom out of the whole thing. padding-bottom 将其向下推到父 div 之外,并将整个底部吹出。

css css

.itempanel{
  display: block;
  align-items: center;
  position: relative;
}
.itemcontainer{
  position: relative;
  top: 30%;
  height: 60%;
  width: auto;
  margin-left: auto;
  margin-right: auto;

}
.itemdisplay{
  display:none;
  flex-direction: column;
  width: 60%;
  padding-bottom: 60%;
  margin-left: auto;
  margin-right: auto;
  position: relative;
  align-items: center;
  overflow: hidden;
}
/* .itemdisplay::after{
  content: " ";
  display: block;
  padding-bottom: 100%;
} */

html html

<div class="itempanel">
    <div class = "itemcontainer">
      <div id="itemdisplay" class="itemdisplay">
        <img id="itemimg" class="itemimg" src=""></img>
        <div id="itemdesc" class="itemdesc">
          <div id="itemtitle" class="itemtitle"></div>
          <div id="itembody" class="itembody"></div>
          <a id="itemclick" class="itemclick" href="">Click To Place Order</a>
        </div>
      </div>
    </div>
  </div>

Thanks for any help!谢谢你的帮助!

*little bit of background - girlfriend wants a skyrim wedding so building out a website for rsvp and stuff. *一点背景 - 女朋友想要一场天际婚礼,所以为 rsvp 和其他东西建立了一个网站。 Belethor's shop is going to be setup to help those not so nerdy shop for cheap cosplay outfits. Belethor 的商店将帮助那些不那么书呆子购买廉价角色扮演服装的人。 Everything else works just like I want it, but the itempanel/itemcontainer/itemdisplay is not in the display area how I want it.其他一切都像我想要的那样工作,但是 itempanel/itemcontainer/itemdisplay 不在我想要的显示区域中。

I see that you have created a class .bottommenu but I don't see it being applied anywhere on your HTML or JavaScript.我看到您已经创建了一个 class .bottommenu ,但我没有看到它被应用到您的 HTML 或 JavaScript 的任何地方。 If you want to display two rows using Flex-box and you are not quite sure of the height of your images or content, you could set that parent container to have a height: auto and on your child container you could use the align-self: flex-end to handle the positioning of items, and on your parent use the justify-content or align-content to your liking.如果您想使用 Flex-box 显示两行并且您不太确定图像或内容的高度,您可以将该父容器设置为height: auto并在您的子容器上使用align-self: flex-end来处理项目的定位,并在您的父级上使用justify-contentalign-content根据您的喜好。 Something like this:像这样的东西:

div .parent {
   display:flex;
   height: auto;
   min-width: 300px;
 }

div .child {
   align-self: center;
   width: 100%;
   min-height: 50%;
   height: auto;
   margin: 0 auto;
}

Hope that helps.希望有帮助。

figured it out.弄清楚了。 The psuedo element has to be opposite what padding you are using.伪元素必须与您使用的填充相反。 For instance, using this worked like I expected it to.例如,使用它就像我预期的那样工作。 it's always the simple things...总是很简单的事情...

.itemdisplay::after{
  content: " ";
  display: block;
  padding-top: 100%;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM