Center div horizontally
On this page, I would like to horizontally center the main #container div relative to the page. Normally I would achieve this by adding a CSS rule,
#container { margin: 0 auto }
However, the layout of this page (which I did not write), uses absolute positioning for #container and most of its child elements, so this property has no effect.
Is there any way I can achieve this horizontal centering without rewriting the layout to use static positioning? I've no problem with using JavaScript/JQuery if that's the only way to ac开发者_如何学运维hieving my objective.
Same as @rquinn answer but this will adjust to any width. Check this demo
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();
});
});
You can use also use jQuery:
function setMargins() {
var width = $(window).width();
if(width > 1024){
var leftMargin = (width - 1024)/2;
$("#container").css("marginLeft", leftMargin);
}
}
Then I put this code after the $(document).ready
event:
$(document).ready(function() {
setMargins();
$(window).resize(function() {
setMargins();
});
});
the container width is 1024px, so you can try to give a left value 50% and margin-left: -512px.
Using Javascript, this will put #container at the center of your browser's window.
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';
If your main container is using absolute
position, you can use this CSS to center it horizontally;
#container {
position:absolute;
width:1000px; /* adjust accordingly */
left:50%;
margin-left:-500px; /* this should be half of the width */
}
It's simple. Try this
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;
}
This way worked best for me. Without changing margins, but the position. Required: object -> margin-left: 0; and position: relative;
check example here: jsfiddle Code:
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);
});
}
Use:
<script type="text/javascript">
$(document).ready(function () {
setPositionOnResize(window, "#PanelTeste");
});
</script>
It can be centered within any container.
精彩评论