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