I'm implementing pagination, and it needs to be centered. The problem is that the links need to be displayed as block, so they need to be floated. But then, text-align: center;
doesn't work on them. I could achieve it by giving the wrapper div padding of left, but every page will have a different number of pages, so that wouldn't work. Here's my code:
.pagination {
text-align: center;
}
.pagination a {
display: block;
width: 30px;
height: 30px;
float: left;
margin-left: 3px;
background: url(/images/structure/pagination-button.png);
}
.pagination a.last {
width: 90px;
background: url(/images/structure/pagination-button-last.png);
}
.pagination a.first {
width: 60px;
background: url(/images/structure/pagination-button-first.png);
}
<div class='pagination'>
<a class='first' href='#'>First</a>
<a href='#'>1</a>
<a href='#'>2</a>
<a href='#'>3</a>
<a class='last' href='#'>Last</a>
</div>
<!-- end: .pagination -->
To get the idea, what I want:
Since many years I use an old trick I learned in some blog, I'm sorry i don't remember the name to give him credits.
Anyway to center floating elements this should work:
You need a structure like this:
the trick is giving float left to make the containers change the width depending on the content. Than is a matter of position:relative and left 50% and -50% on the two containers.
The good thing is that this is cross browser and should work from IE7+.
Removing
float
s, and usinginline-block
may fix your problems:(remove the lines starting with
-
and add the lines starting with+
.)inline-block
works cross-browser, even on IE6 as long as the element is originally an inline element.Quote from quirksmode:
this often can effectively replace floats:
From the W3C spec:
IE7 doesn't know
inline-block
. You must add:I think the best way is using
margin
instead ofdisplay
.I.e.:
Check the result and the code:
http://cssdeck.com/labs/d9d6ydif
Add this to you styling
*If your container width is 50px put 25px, if it is 10em put 5em.