0

I am making a pagination system for my search page and currently have something that looks like this:

.pagination-wrapper{
  display: flex;
  justify-content: center;
  padding-top: 50px;
}
.number{
  border: thin solid rgb(80 80 80);
  padding: 0.2rem 0.3rem 0.2rem 0.3rem;
  text-align: center;
  margin: 0 0.2rem;

  min-width: 1rem;
}
<div class="pagination-wrapper">
    <span class="number">1</span> 
    <span class="number">56</span> 
  <span class="number">111</span> 
  <span class="number">888</span> 
</div>

I put min-width on the number elements to make the border have the same size for single and double digit numbers which works as expected.

Problem

When I have a "small" 3-digit number like 111 it takes up less space than a larger one like 888. The difference is subtle in the fiddle, it is much larger with my current font. Image for reference with my font:

enter image description here

What I want

I want the boxes of each 3 digit number to be the same size. Not the same size as the single and 2 digit ones, it can be larger. So the sizes for each 1 and 2 digit numbers are as the min width, say 16px. Then all 3 digit numbers should have the same width of the box, say 24px. And each 4 digit number should have 32px etc.

What I tried

I have tried to apply the attribute monospace to the font family with no help, I guess it is because my font is simply not monospaced. I solved it with Javascript by when I click on a page it will check the page numbers and apply a different min-width to each element depending on the size of the number. This feels a bit overkill though.

If it's of any help I use bootstrap 3 as framework, but the problem applies without it too.

Is there a way to solve this with HTML/CSS without JS or without changing to monospaced font?

eligolf
  • 1,682
  • 1
  • 6
  • 22
  • 4
    I doubt there's a simple solution without changing to a [monospace](https://cms-assets.tutsplus.com/cdn-cgi/image/width=850/uploads/users/2056/posts/34717/image/MonospacedFonts-3.jpg) font as each letter is inherently different in [its width](https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQs8vW56mWEzdOI9vOVYUlZH9JDLo0KrtvGCEWOTaoPIknNPWvJZjURL6LnALsrdR01Gw0&usqp=CAU). If it's only for footer pagination and it's critical for you to keep consistent width, I'd suggest you consider a monospace font of similar visual quality like [Roboto Mono](https://fonts.google.com/specimen/Roboto+Mono/) – Bumhan Yu Jan 18 '23 at 05:51
  • @BumhanYu, That is what I was thinking too, just asked if there was some solution I had missed. Thank you :) – eligolf Jan 18 '23 at 07:34
  • The only way I see is to check the length with eg JavaScript and then set a class – c.m. Jan 18 '23 at 09:34
  • You might find [this CSS Tricks article](https://css-tricks.com/subset-numerals-so-theyre-as-awesome-as-the-rest-of-your-content/) interesting. It deals with aligning numeric characters in monospaced fashion. – Bumhan Yu Jan 18 '23 at 13:24
  • I’m classically trained on design and typography, and there is this concept of [proportional vs tabular lining](https://theworldsgreatestbook.com/wp-content/uploads/2011/11/figureStyles1.gif) of figures. It’s well supported idea in editorial design tools (like [Illustrator or InDesign](https://www.monotype.com/resources/expertise/how-use-figure-styles-illustrator) ) but I haven’t found a simple setting change via CSS so far. – Bumhan Yu Jan 18 '23 at 13:33

3 Answers3

0

A solution proposed here is inserting each character into its own span of fixed width: How to imitate a monospace font with a variable-width font?

Rhys Mills
  • 187
  • 7
0

In your class, you can try this property. As per your request, I'm sure it will work

.number { flex: 1 1 0%; }
0

Try adding font-kerning: none; to the numbers class. Perhaps in combination with letter-spacing: 0.1rem;, which you can adjust using positive and negative values, if needed.

d50org
  • 83
  • 6
  • Letters will still have variable width with `font-kerning: none`, as demonstrated here: https://developer.mozilla.org/en-US/docs/Web/CSS/font-kerning – Rhys Mills Jan 18 '23 at 11:12