简体   繁体   English

使用纯CSS在未知尺寸的div内的9个可能位置中的任意一个位置上的未知尺寸的img | span | div位置

[英]position img|span|div of unknown dimensions in any of 9 possible positions within a div of unknown dimensions using pure CSS

I've asked a similar question and tried to modify it's answer for this but I'm not sure what to do. 我问过一个类似的问题,并试图修改它的答案,但是我不确定该怎么做。

I have a div of unknown dimensions and I need to place an img , span , or div in one of 9 positions within it. 我有一个尺寸未知的div ,我需要在其中的9个位置之一中放置imgspandiv

  • top left 左上方
  • top middle 中上层
  • top right 右上
  • center left 居中左
  • center middle 中间居中
  • center right 右中
  • bottom left 左下方
  • bottom middle 底部中间
  • bottom right 右下

The question from above works for images but not with anything of unknown dimensions. 上面的问题适用于图像,但不适用于未知尺寸的图像。

I've tried various combinations but cannot figure it out. 我尝试了各种组合,但无法弄清楚。 http://jsfiddle.net/g7hydky2/3/ is what I'm working with. 我正在使用http://jsfiddle.net/g7hydky2/3/

The only thing I can think of is using display: table and display: table-cell on the outer and inner elements because then I can fully use vertical-align and text-align . 我唯一能想到的就是在外部和内部元素上使用display: tabledisplay: table-cell ,因为这样我就可以充分使用vertical-aligntext-align However, this seems like a bad hack and I feel like there has to be a better way. 但是,这似乎是一个坏习惯,我觉得必须有更好的方法。

Also, I need this to work on IE 8... 另外,我需要在IE 8上工作...

Solved with flexbox , flexbox解决了,

Acknowledging this article - Getting Dicey with Flexbox 确认本文- 使用Flexbox精打细算

.pip {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: black;
}
.one {
  justify-content: flex-start;
  align-items: flex-start;
}
.two {
  justify-content: center;
  align-items: flex-start;
}
.three {
  justify-content: flex-end;
  align-items: flex-start;
}
.four {
  justify-content: flex-start;
  align-items: center;
}
.five {
  justify-content: center;
  align-items: center;
}
.six {
  justify-content: flex-end;
  align-items: center;
}
.seven {
  justify-content: flex-start;
  align-items: flex-end;
}
.eight {
  justify-content: center;
  align-items: flex-end;
}
.nine {
  justify-content: flex-end;
  align-items: flex-end;
}

 body { text-align: center; } .dice { width: 50px; height: 50px; padding: 5px; border: 1px solid grey; border-radius: 15px; display: inline-flex; vertical-align: top; margin: 10px; } .pip { width: 10px; height: 10px; border-radius: 50%; background: black; } .one { justify-content: flex-start; align-items: flex-start; } .two { justify-content: center; align-items: flex-start; } .three { justify-content: flex-end; align-items: flex-start; } .four { justify-content: flex-start; align-items: center; } .five { justify-content: center; align-items: center; } .six { justify-content: flex-end; align-items: center; } .seven { justify-content: flex-start; align-items: flex-end; } .eight { justify-content: center; align-items: flex-end; } .nine { justify-content: flex-end; align-items: flex-end; } 
 <div class="dice one"> <div class="pip"></div> </div> <div class="dice two"> <div class="pip"></div> </div> <div class="dice three"> <div class="pip"></div> </div> <div class="dice four"> <div class="pip"></div> </div> <div class="dice five"> <div class="pip"></div> </div> <div class="dice six"> <div class="pip"></div> </div> <div class="dice seven"> <div class="pip"></div> </div> <div class="dice eight"> <div class="pip"></div> </div> <div class="dice nine"> <div class="pip"></div> </div> 


For absolute certainty {pun intended} and older browswer support use positioning: Codepen Demo 为了获得绝对确定性(双关语意)和较早的浏览支持,请使用以下位置: Codepen演示

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

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