【问题标题】:Div vertical align centre with relative position [duplicate]div垂直对齐中心与相对位置[重复]
【发布时间】:2018-11-20 13:56:48
【问题描述】:

stackoverflow 上有很多关于垂直对齐的内容,但在我的情况下它不起作用。请参阅下面的代码和 CSS,如何使 img 居中...

 
.file-upload-content {
    width: 100%;
    height: 100%;
    text-align: center;
    position: relative;
}
.file-upload-image {
  position: relative;
  max-height: 100%;
  max-width: 100%;
  margin: auto;
}
.btn-verwijderen{
  position: absolute;
  top: 0px;
  right: 0px;
}
.nr-foto{
  position: absolute;
  bottom: 0px;
  right: 0px;  
}
<div class="foto_vak_25_50 border_dashed foto_upload">
    <div class="file-upload-content" id="content_foto2" >
       <img class="file-upload-image" src="#" id='image_foto2' />
       <img src="/img/prullenbak.png" class="btn-verwijderen">
       <img src="/img/nr-2.png" class="nr-foto">
    </div> 
</div>

【问题讨论】:

  • 您要居中哪个图像?里面有3张图片
  • 您需要哪种类型的输出??
  • image_foto_2 需要居中,nr-foto 在右下角,另一个在右上角(div)

标签: css


【解决方案1】:

添加了这些样式

body , html,.foto_vak_25_50 {
   height: 100%;
}
.file-upload-image {
  position: absolute;
  max-height: 100%;
  max-width: 100%;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

body , html,.foto_vak_25_50 {
   height: 100%;
}
 
.file-upload-content {
    width: 100%;
    height: 100%;
    text-align: center;
    position: relative;
}
.file-upload-image {
  position: absolute;
  max-height: 100%;
  max-width: 100%;
  top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.btn-verwijderen{
  position: absolute;
  top: 0px;
  right: 0px;
}
.nr-foto{
  position: absolute;
  bottom: 0px;
  right: 0px;  
}
<div class="foto_vak_25_50 border_dashed foto_upload">
    <div class="file-upload-content" id="content_foto2" >
       <img class="file-upload-image" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTjzx-JHoAz16ZUQ9fu7FUGvBvsIRn-30nSCHS64zlwZXaeqz3aZw" id='image_foto2' />
       <img src="/img/prullenbak.png" class="btn-verwijderen">
       <img src="/img/nr-2.png" class="nr-foto">
    </div> 
</div>

【讨论】:

  • 谢谢!只有水平对齐中心不再起作用。为此要添加什么?
  • 我不明白哪里和什么不工作!你能详细说明一下吗
  • 当图像是横向时它会正确对齐(因为 max-with 100%),但是当纵向时它会在中间对齐而不是右对齐
  • 你能告诉我哪里不工作吗!因为它对我来说工作正常。可能是其他一些样式干扰它
  • 由于某种原因,我无法再通过图片/屏幕截图了。 image_foto2 必须居中对齐,水平和垂直
猜你喜欢
  • 1970-01-01
  • 2018-01-15
  • 1970-01-01
  • 1970-01-01
  • 2018-10-24
  • 2017-03-03
  • 2018-10-29
  • 1970-01-01
  • 2012-06-13
相关资源
最近更新 更多