I am trying to create a simple three-part page navigation:
- Several previous page numbers (if any)
- Current page number (this should be centered)
- Several upcoming page numbers (if any)
The important thing is that the current page number is always horizontally centered in the parent container. The remaining two parts should evenly distribute the remaining horizontal space.
This JSFiddle illustrates my two attempts to solve this problem.
Solution 1 : use text-align: center . This achieves the desired result, but only if both sides are equal in width. If not the current page number will not be in the center.
HTML
<div class="container"> <input type="button" value="47"> <input type="button" value="48"> <input type="button" value="49"> <input type="text" size="5" maxlength="5" value="50"> <input type="button" value="51"> <input type="button" value="52"> <input type="button" value="53"> </div>
CSS
.container, input { text-align: center; }
Solution 2 : use the specified widths manually to evenly distribute horizontal space. This effectively centers the current page number under any circumstances, but this requires strict coding.
HTML
<div class="container"> <div class="left"> <input type="button" value="47"> <input type="button" value="48"> <input type="button" value="49"> </div> <div class="right"> <input type="button" value="51"> <input type="button" value="52"> <input type="button" value="53"> </div> <div class="center"> <input type="text" size="5" maxlength="5" value="50"> </div> </div>
CSS
.left { width: 40%; float: left; text-align: right; } .right { width: 40%; float: right; text-align: left; } .center { width: 20%; margin-left: 40%; }
None of these solutions really do what I want. Is there a way for the current page number to be centered, allowing other elements to fit their natural size, rather than an arbitrary pixel or percentage width?
source share