[英]Vertically aligning a div inside a div that has a percentage height and width
I'm trying to vertically align a div inside a div (to make a play button for a video) and i've tried all the traditional methods of vertically aligning, and have even tried a jquery script to vertically align it, but as of yet i'm still to no avail. 我正在尝试将div中的div垂直对齐(以制作视频的播放按钮),并且尝试了所有垂直对齐的传统方法,甚至尝试了jquery脚本将其垂直对齐,但是截至但我仍然无济于事。
EDIT - The content / The circle play button needs to be fully responsive so i can't remove the 20% width and the 20% padding-bottom it has. 编辑-内容/圆圈播放按钮需要完全响应,因此我无法删除其具有的20%宽度和20%填充底部。
A live version of the website can be found here - http://www.mosesmartin.co.uk/digitalguys/gkn.php and the div in question is 'videoplay' div 可在此处找到该网站的实时版本-http: //www.mosesmartin.co.uk/digitalguys/gkn.php ,相关div是“ videoplay” div
HTML HTML
<div class="rectanglewrap">
<div class="rectangleimg" id="gknengine">
<div class="videoplay vcenter"><span class="glyphicon glyphicon-play" id="playbutton"></span></div>
</div>
</div>
CSS CSS
.rectangleimg{
position:relative;
height:0;
width:100%;
padding-bottom:50%;
}
.div-centerer {
position: absolute;
top: 0;
width:100%;
height:100%;
}
.vcenter {
display: inline-block;
vertical-align: middle;
}
.videoplay {
border-radius: 100%;
background-color: #12A537;
height:0;
width:20%;
padding-bottom:20%;
margin:0 auto;
}
Please help me, this is bugging the hell out of me! 请帮助我,这使我烦恼!
Thankyou 谢谢
Since you tagged the question jQuery, here's how to do it in jQuery: 由于您标记了jQuery问题,因此以下是在jQuery中的方法:
$('#playbutton').css('top', ($('#gknengine .videoplay').outerHeight() / 2) - ($('#playbutton').height() / 2));
You'll probably want to put it in a $(window).resize()
handler. 您可能希望将其放在$(window).resize()
处理程序中。
Try using relative and absolute positioning, you can use percentages to adapt to mobile like so: 尝试使用相对和绝对定位,您可以像这样使用百分比来适应移动设备:
.rectanglewrap {
width: 100%;
text-align: center;
border: 1px solid #ddd;
height: 200px;
position: relative;
}
.videoplay {
position: absolute;
top:50%;
margin-top: -10px;
left: 50%;
margin-left: -10%;
width: 20%;
background: #ddd;
}
The top & left margins should be half the width & height of the play element respectively (& they can be percentages). 顶部和左侧边距应分别为播放元素的宽度和高度的一半(并且它们可以是百分比)。
See Fiddle: http://jsfiddle.net/7fs52w2L/2/ 请参阅小提琴: http : //jsfiddle.net/7fs52w2L/2/
If you don't care about older browser (eg IE < 9) you can use transform to center your div only with CSS 如果您不关心较旧的浏览器 (例如IE <9),则可以使用transform仅将CSS居中
div.videoplay {
position: absolute;
top: 50%;
left: 50%;
transform: translateY(-50%) translateX(-50%);
// margin-top: 1px <--- delete
}
#playbutton {
position: absolute;
top: 50%;
left: 50%;
transform: translateY(-50%) translateX(-50%);
// top: 1px <--- delete
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.