Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I center floated elements?

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:

alt text

like image 342
Mike Avatar asked Jan 22 '11 13:01

Mike


People also ask

How do you center a floated image in HTML?

An <img> element is an inline element (display value of inline-block ). It can be easily centered by adding the text-align: center; CSS property to the parent element that contains it. To center an image using text-align: center; you must place the <img> inside of a block-level element such as a div .

How do you make an element centered?

To just center the text inside an element, use text-align: center; This text is centered.

How do you center a list of elements?

Just give the list centered text (e.g. ul. nav { text-align: center; } ) and the list items inline-block (e.g. ul. nav li { display: inline-block; } ). If you want to do it with margin for whatever reason, look into width: fit-content; .


1 Answers

Removing floats, and using inline-block may fix your problems:

 .pagination a { -    display: block; +    display: inline-block;      width: 30px;      height: 30px; -    float: left;      margin-left: 3px;      background: url(/images/structure/pagination-button.png);  } 

(remove the lines starting with - and add the lines starting with +.)

.pagination {    text-align: center;  }  .pagination a {    + display: inline-block;    width: 30px;    height: 30px;    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 -->

inline-block works cross-browser, even on IE6 as long as the element is originally an inline element.

Quote from quirksmode:

An inline block is placed inline (ie. on the same line as adjacent content), but it behaves as a block.

this often can effectively replace floats:

The real use of this value is when you want to give an inline element a width. In some circumstances some browsers don't allow a width on a real inline element, but if you switch to display: inline-block you are allowed to set a width.” ( http://www.quirksmode.org/css/display.html#inlineblock ).

From the W3C spec:

[inline-block] causes an element to generate an inline-level block container. The inside of an inline-block is formatted as a block box, and the element itself is formatted as an atomic inline-level box.

like image 119
Arnaud Le Blanc Avatar answered Sep 19 '22 23:09

Arnaud Le Blanc