[英]Centring a div horizontally inside another div with absolute position state
我正在尝试在另一个div内水平居中放置一个div。 我试图居中的div是使用jQuery的向下滚动按钮,并具有我制作的自定义图标字体和默认的宽度/高度。 我想将此div放在我的主div内,并保持原始大小,因为我想继续使用它作为按钮。 例如: 我想制作类似白色箭头的箭头,该箭头指向中心下方,但不会弄乱我的宽度。 这是我的代码:
的HTML
<div id="intro-tab"> <!-- First/Intro Tab -->
<div id="introtab-godownbtn">Q</div>
</div>
的CSS
#intro-tab {
width: auto;
height: 100%;
position: absolute;
left: 0px;
right: 0px;
top: 0px;
background-color: red;
box-shadow: 0px 1px 3px #000;
}
#introtab-godownbtn {
font-family: iconFont;
font-size: 50px;
position: absolute;
bottom: 25px;
width: 60px;
height: 30px;
left: 50%;
margin-left: 30px;
background-color: #FFF;
}
#introtab-godownbtn:hover {
cursor: pointer;
}
jQuery的
$('#introtab-godownbtn').click(function(){
$("html, body").animate({
scrollTop: (screen.height - 90)
}, 600);
return false;
});
我尝试了多种方法来使按钮introtab-godownbtn
但它不起作用,或者只是弄乱了我的按钮大小和单击位置。 我的问题有解决办法吗?
据我了解,您正在尝试将HTML元素水平居中。 通常,将使用margin: 0 auto;
在要应用的元素上设置固定宽度的方法。 这是一个这样的示例: http : //jsfiddle.net/5XTq2/
如果此答案无济于事,能否提供您要实现的布局的模型/屏幕截图? 我可以很高兴地更新答案以适应您的需求。
编辑:根据您的Spotify示例,如果您检查页面并选择向下箭头,它将具有以下样式。
.scroller-arrow {
width: 60px;
height: 60px;
background-image: url(../i/_global/arrow-big.png);
cursor: pointer;
display: block;
margin: 0 auto;
position: relative;
-webkit-tap-highlight-color: transparent;
}
要使内部绝对定位的div在水平和垂直方向上居中,请执行以下操作:
HTML:
<div id="intro-tab">
<div id="introtab-godownbtn">Q</div>
</div>
CSS:
body { margin: 0; }
#intro-tab {
width: 100%;
height: 100%;
position: absolute;
background-color: red;
box-shadow: 0px 1px 3px #000;
}
#introtab-godownbtn {
background-color: #FFF;
font-family: iconFont;
font-size: 20px;
width: 60px;
/* this does the centering */
height: 30px;
position: absolute;
margin: auto;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
#introtab-godownbtn:hover { cursor: pointer; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.