Q
水平中心div
7
A
回答
6
与@rquinn答案相同,但可以调整为任意宽度。检查这个演示
http://jsfiddle.net/Starx/V7xrF/1/
HTML:
<div id="container"></div>
CSS:
* { margin:0;padding:0; }
#container {
width:50px;
position:absolute;
height:400px;
display:block;
background: #ccc;
}
的Javascript
function setMargins() {
width = $(window).width();
containerWidth = $("#container").width();
leftMargin = (width-containerWidth)/2;
$("#container").css("marginLeft", leftMargin);
}
$(document).ready(function() {
setMargins();
$(window).resize(function() {
setMargins();
});
});
1
容器宽度为1024px,因此您可以尝试给左侧值50%和左侧余量:-512px。
0
使用Javascript,这会将#container放在浏览器窗口的中心位置。
document.getElementById("container").style.top= (window.innerHeight - document.getElementById("container").offsetHeight)/2 + 'px';
document.getElementById("container").style.left= (window.innerWidth - document.getElementById("container").offsetWidth)/2 + 'px';
3
您可以使用也使用jQuery:
function setMargins() {
var width = $(window).width();
if(width > 1024){
var leftMargin = (width - 1024)/2;
$("#container").css("marginLeft", leftMargin);
}
}
然后我把这个代码$(document).ready
事件之后:
$(document).ready(function() {
setMargins();
$(window).resize(function() {
setMargins();
});
});
0
如果你的主容器使用absolute
的位置,你可以使用这个CSS使其居中居中;
#container {
position:absolute;
width:1000px; /* adjust accordingly */
left:50%;
margin-left:-500px; /* this should be half of the width */
}
0
很简单。 试试这个
body {
margin:50px 0px; padding:0px; /* Need to set body margin and padding to get consistency between browsers. */
text-align:center; /* Hack for Internet Explorer 5/Windows hack. */
}
#Content {
width:500px;
margin:0px auto; /* Right and left margin widths set to "auto". */
text-align:left; /* Counteract to Internet Explorer 5/Windows hack. */
padding:15px;
border:1px dashed #333;
background-color:#eee;
}
0
这种方式最适合我。没有改变利润率,但位置。 要求: object - > margin-left:0;和位置:相对;
检查这里例如:jsfiddle 代码:
function setObjPosition(container, object) {
var containerWidth = $(container).width();
var objectWidth = $(object).width();
var position = (containerWidth/2) - (objectWidth/2);
$(object).css('left', position);
}
function setPositionOnResize(container, object) {
setObjPosition(container, object);
$(container).resize(function() {
setObjPosition(container, object);
});
}
用途:
<script type="text/javascript">
$(document).ready(function() {
setPositionOnResize(window, "#PanelTeste");
});
</script>
它可以在任何容器中居中。
相关问题
- 1. 中心DIV水平
- 2. 中心DIV水平scolling
- 3. Div水平中心和垂直中间
- 4. 水平视差滚动到中心DIV
- 5. CSS/jQuery垂直和水平中心DIV
- 6. CSS垂直和水平中心Div
- 7. javascript水平中心
- 8. DIV水平
- 9. 水平的css中心
- 10. 中心利用水平
- 11. 中心水平RecyclerView在RelativeLayout
- 12. HTML水平导航中心
- 13. div中间的水平线
- 14. 水平居中浮动div
- 15. 居中图像div水平
- 16. 水平并排div div
- 17. 水平移动div?
- 18. 水平定位Div
- 19. 水平对齐div
- 20. 中心李水平与内溢出的div:汽车
- 21. 如何将DIV水平滚动到容器的中心?
- 22. 在一个div的中心水平对齐多个图像
- 23. 中心DIV内容流体垂直和水平
- 24. 中心div内容垂直和水平不工作
- 25. 中心使用默认的Twitter引导CSS水平对齐div
- 26. 如何对齐DIV垂直和水平中心
- 27. 怎样才能让我的内心的div中心垂直和水平
- 28. 定心问题(水平)
- 29. RelativeLayout中的TextView水平中心
- 30. 位置中心水平和垂直
正在使用脚本行吗? – Starx 2011-01-12 07:54:52
@Starx很好的问题,我已经更新了我的帖子,答案是肯定的 – 2011-01-12 08:05:33
@rquinn为你解答 – Starx 2011-01-12 09:22:21