Centering the absolute positioned navigation bar of NivoSlider? (or using the margin: 0 auto trick)?
I added Nivo Slider to my Wordpress theme.
In the demo provided with the file the nivo-ControlNav has position absolute, and it is moved to the right.
I would like to know if there is a way of centering the nivo-ControlNav (and it should even be centered in Internet Explorer)?
front-page.php开发者_如何学C:
<div id="feature">
<div class="container">
<div id="slider-wrapper">
<div id="slider">
<?php // Retrive custom post type with a custom taxonomy assigned to it
$posts = new WP_Query('post_type=page_content&page_sections=Slider (Front Page)&order=ASC') ?>
<?php while ( $posts->have_posts() ) : $posts->the_post(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</div>
<div class="shadow-slider">
<!-- Shadow at the bottom of the slider -->
</div>
</div>
</div><!-- .container -->
</div><!-- #featured -->
stlye.css:
#feature {
background: #333;
padding: 30px 0;
height: 400px;
}
#slider-wrapper {
float: left;
height: 500px;
}
#slider {
float: left;
border: 1px solid #DDD;
}
#slider {
position:relative;
background:url(images/loading.gif) no-repeat 50% 50%;
}
#slider img {
position:absolute;
top: 0px;
left: 0px;
display: none;
}
#slider a {
border: 0;
display: block;
}
.nivo-controlNav {
position: absolute;
left: 260px;
bottom: -42px;
width: 50%;
left: 0;
right: 0;
margin: 0 auto;
}
.nivo-controlNav a {
display: block;
width: 22px;
height: 22px;
background: url(images/bullets.png) no-repeat;
text-indent: -9999px;
border: 0;
margin-right: 3px;
float: left;
}
.nivo-controlNav a.active {
background-position: 0 -22px;
}
.nivo-directionNav a {
display: block;
width: 30px;
height: 30px;
background: url(images/arrows.png) no-repeat;
text-indent: -9999px;
border: 0;
}
a.nivo-nextNav {
background-position: -30px 0;
right: 15px;
}
a.nivo-prevNav {
left: 15px;
}
.nivo-caption {
text-shadow:none;
font-family: Helvetica, Arial, sans-serif;
}
Or should I just use the float: left; margin: 0 auto;
trick?
EDIT:
I decided to do this:
.nivo-controlNav {
margin: 0 auto;
overflow: hidden;
width: 200px;
}
.nivo-controlNav {
margin: 0 auto;
overflow: hidden;
width: 200px;
}
but the links inside the nivo-controlNav div
are not centered.
Any suggestions?
.nivo-controlNav {
text-align:center;
left:0;
right:0;
}
.nivo-controlNav a {
display:inline-block; /* so float:none */
}
It will work on all browsers understanding inline-block property.
I found the solution, and it's relevant for any number of images, the only inconvenient is that you'll have to use jQuery, but I guess that won't be a problem cause Nivo already works with jQuery ^^' ... anyways ...
Based on the earlier posted css codes :
.nivo-controlNav {
position: absolute;
left: 50%;
}
I could come up with this javascript code, I'll explain it later :
$(document).ready(function() {
$('#slider').nivoSlider();
var numberImages = $('#slider img').length;
var bulletWidth = 22;
var margin = - ( (numberImages * bulletWidth) / 2 );
$('.nivo-controlNav').css('margin-left', margin+'px');
});
What this basically means is that, instead of giving each slider's controlNav a random margin-left based on the number of images, we'll calculate that, and the way to do that is very simple :
- We MOVE the left edge of our controlNav to 50% (CSS)
Next we have to know the width of the WHOLE controlNav, which contains bullets of course :
`numberImages * bulletWidth`
Divide all this by 2, and then remove it from the margin-left, this simply means : move the controlNav by half of its width to the left.
Hope it works for you :)
精彩评论