简体   繁体   English

如何将图像垂直居中于相对定位的容器内?

[英]how to vertically center an image inside of a relatively-positioned container?

I would like to vertically center the image in the below HTML, so that it is vertically-center to the div it is next to ("rtUpdateContentDiv"). 我想在下面的HTML中垂直居中图像,使它垂直居中于它旁边的div(“rtUpdateContentDiv”)。 Both the img and div are inside a parent container, "rtUpdateContainerDivActive". img和div都在父容器“rtUpdateContainerDivActive”中。

<div class="rtUpdateContainerDivActive">
     <img class="statusImg" src="images/icons/Knob-Valid-Green.png">
     <div class="rtUpdateContentDiv">
          <span class="rtBlueHighlight">date and time:</span>
          ipsum lorem text here
    </div>
</div>

here is the css of the image so far: 这是目前为止的图像的CSS:

.statusImg
{
     position: absolute;
     margin-top: auto;
     margin-bottom: auto;
     vertical-align: middle;
     padding-left: 3px;
     padding-top: 3px;
}

and of the container: 和容器:

.rtUpdateContainerDivActive
 {
     background-color: #90ee90;
     margin-top: 1%;
     position: relative;
     min-height: 38px;
 }

The parent needs to remain relative. 父母需要保持亲戚关系。

How can I vertically-center the image (38px x 38px)? 如何垂直居中图像(38px x 38px)?

Thank you for any help! 感谢您的任何帮助!

Solution1: 解决方法1:

.parent{
    display: inline-table;
    height:100%;
}

.middleChild{
    display: table-cell;
    vertical-align: middle;
}

Solution2: 溶液2:

.statusImg
{
     position: absolute;
     height:38px;
     top:50%;
     margin-top:-19px;
}

hope it will help... 希望它会有所帮助......

您可能希望阅读此http://css-tricks.com/centering-in-the-unknown/ ,最后一种方法可能是您需要的方法。

使用top: 50%用组合的margin-top一半的图像高度的: http://jsbin.com/adiseq/1/edit

暂无
暂无

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

相关问题 为什么 Google Chrome 会忽略 flex-box 内相对定位元素中的“top” css 属性? - Why does Google Chrome ignore “top” css property in a relatively-positioned element inside a flex-box? 如何使相对定位的按钮居中 - How to center a button that is positioned relatively 如何将这个绝对定位的<a>`标签在相对定位的div内</a>水平居中对齐<a>?</a> - How to horizontally center align this absolutely positioned `<a>` tag inside the relatively positioned div? 如何在相对定位的flexbox容器中获取绝对定位的项目以进行增长? - How to get an absolutely positioned item inside a relatively positioned flexbox container to grow? 如何将相对定位的元素居中与jquery? - How to center a relatively positioned element with jquery? 当使用jQuery .hover()移动相对定位的div时,如何解决IE6 / IE7边距消失的问题? - How to work around IE6/IE7 margins disappearing when relatively-positioned floated div is moved with jquery .hover()? 如何将元素固定在相对放置的容器中? - How to position element as fixed in relatively positioned container? 如何在绝对定位的父 div 内垂直居中 div - How to center div vertically inside of absolutely positioned parent div 相对定位的内容没有垂直堆叠? - Relatively positioned content not stacking vertically? 相对位置的父级与绝对位置的子级会折叠后面的元素 - Relatively-positioned parent with absolutely-positioned children collapses the later elements
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM