问题
I'm trying to center my main container, but whatever I do it still floats left... The reason I am placing all of my content in a own Div, is that i use a Jquery cycle background, and I want to separate my content from my background. But as mentioned I really cant make my Div container center... I hope someone a bit more skilled than me can figure this out!
This is my html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Forside</title>
<link rel="stylesheet" type="text/css" media="screen" href="style.css" />
<script type="text/javascript" src="http://code.jquery.com/jquery- 1.6.3.min.js"></script> <script type="text/javascript" src="jquery.cycle.all.js"></script>
<script>
$(document).ready(function() {
$('#slideshow').cycle({
fx: 'fade', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
pager: '#smallnav',
pause: 1,
speed: 1800,
timeout: 3500
});
});
</script>
</head>
<body>
<div class="bx-overlay">
</div>
<div id="slideshow">
<img src="images/3.jpg" alt="san diego"/>
<img src="images/2.jpg" alt="bridge"/>
<img src="images/1.jpg" alt="mountains"/>
<img src="images/4.jpg" alt="waves"/>
<img src="images/5.jpg" alt="americanflag"/>
<img src="images/6.jpg" alt="football"/>
</div>
<div id="container">
<div id="header">
<img src="images/header.png">
</div>
</div>
</body>
</html>
and this is my css:
#slideshow, img.bgM {
min-height: 100%;
min-width: 1024px;
width: 100%;
height: auto;
position: fixed;
top: 0;
left: 0;
z-index:-9999;
}
.bx-overlay {
width: 100%;
height: 100%;
position: fixed;
top: 0px;
left: 0px;
background: transparent url(images/pattern.png) repeat top left;
z-index:-1;}
#container{
position: absolute;
text-align:center;
margin-left: auto;
margin-right: auto;
}
#header {
display: block;
margin-left: auto;
margin-right: auto;
position: absolute;
text-align:center;
margin:0 auto;
}
回答1:
With this as your HTML
<div id="container">
<div id="header">
<img src="images/header.png">
</div>
The container will be 100% fo the width of it's parent
To center the header
#header {
display: block; <-- not required as it's a div
margin-left: auto; <-- not required as it's a overridden by last margin property
margin-right: auto; <-- not required as it's a overridden by last margin property
position: absolute; <-- remove this
text-align:center; <-- only required if you are centering text
margin:0 auto; <-- this will center provided you add a width OR max-width
width: add a value;
}
来源:https://stackoverflow.com/questions/19032103/cant-center-a-div-container