2

I'm trying to align "Previous" to the left, align "Next" to the right, and figure out how to center the page numbers.

I've been looking at tutorials and articles on flexbox but I'm having a hard time understanding it.

.nav-links {
  display: flex;
}
.prev.page-numbers {
  justify-content: flex-start;
}
.next.page-numbers {
  justify-content: flex-end;
}
<div class="nav-links">
  <a class="prev page-numbers">Previous</a>
  <a class="page-numbers">1</a>
  <a class="page-numbers">2</a>
  <a class="page-numbers">3</a>
  <a class="next page-numbers">Next</a>
</div>
dippas
  • 58,591
  • 15
  • 114
  • 126
Darren Bachan
  • 735
  • 2
  • 11
  • 30

3 Answers3

2

It seems you want to "push" flex items in one direction or another. To do that, you should use auto margins instead of justify-content.

.nav-links {
  display: flex;
}
.prev.page-numbers {
  margin-right: auto; /* Push to the left */
}
.next.page-numbers {
  margin-left: auto; /* Push to the right */
}
<div class="nav-links">
  <a class="prev page-numbers">Previous</a>
  <a class="page-numbers">1</a>
  <a class="page-numbers">2</a>
  <a class="page-numbers">3</a>
  <a class="next page-numbers">Next</a>
</div>

The remaining flex items will be centered between the first and last ones, not necessarily centered relatively to the container. If you want the latter, you can make the first and last flex items be equally wide.

.nav-links {
  display: flex;
}
.prev.page-numbers, .next.page-numbers {
  flex: 1; /* Disttribute remaining space equally */
}
.next.page-numbers {
  text-align: right;
}
<div class="nav-links">
  <a class="prev page-numbers">Previous</a>
  <a class="page-numbers">1</a>
  <a class="page-numbers">2</a>
  <a class="page-numbers">3</a>
  <a class="next page-numbers">Next</a>
</div>
Oriol
  • 274,082
  • 63
  • 437
  • 513
  • always forget the auto `margins` in `flexbox` ;) – dippas Jun 01 '16 at 23:20
  • @dippas They usually come in handy. Much more powerful than in block layout. – Oriol Jun 01 '16 at 23:26
  • I'll remember in the future :) – dippas Jun 01 '16 at 23:27
  • @Oriol how are they not confined to the widths of each other? Like your example goes 100% width, but my container doesn't have a set width. I was hoping that making it flex would push it all the way to the left/right of the parent container. – Darren Bachan Jun 01 '16 at 23:35
  • @Oriol Yeah, I can only get this to work if I define a width to the parent container, so in this case .nav-links. – Darren Bachan Jun 01 '16 at 23:48
  • @DarrenBachan `display: flex` creates a block-level box, which by default is as wide as its containing block. – Oriol Jun 02 '16 at 00:17
1

The justify-content property applies only to flex containers, although it aligns flex items.

In your code, because you're applying justify-content to flex items, it is being ignored.

Here are two working examples:

Example 1 - justify-content

.nav-links {
  display: flex;
  justify-content: space-between;
  background-color: lightgray;
}

a {
  flex: 0 0 10%;
  text-align: center;
  border: 1px solid black;
  background-color: yellow;
}
<div class="nav-links">
  <a class="prev page-numbers">Previous</a>
  <a class=" page-numbers">1</a>
  <a class="page-numbers">2</a>
  <a class=" page-numbers">3</a>
  <a class="next page-numbers">Next</a>
</div>

Example 2 - auto margins

.nav-links {
  display: flex;
  background-color: lightgray;
}

a {
  flex: 0 0 10%;
  text-align: center;
  border: 1px solid black;
  background-color: yellow;
}    

a:first-child { margin-right: auto; }
a:last-child  { margin-left: auto; }
<div class="nav-links">
  <a class="prev page-numbers">Previous</a>
  <a class=" page-numbers">1</a>
  <a class="page-numbers">2</a>
  <a class=" page-numbers">3</a>
  <a class="next page-numbers">Next</a>
</div>

More details:

Community
  • 1
  • 1
Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
1

justify-content only works in parents, not in children, so if you can wrap your .page-numbers then you just can simply set display:flex to parent and child div and flex:1 + justify-content to div child, which is parent of .page-numbers

div {
  display: flex;
}
div div {
  flex: 1;
  justify-content: center
}
<div class="nav-links">
  <a class="prev page-numbers">Previous</a>
  <div>
    <a class="page-numbers">1</a>
    <a class="page-numbers">2</a>
    <a class="page-numbers">3</a>
  </div>
  <a class="next page-numbers">Next</a>
</div>
dippas
  • 58,591
  • 15
  • 114
  • 126
  • I wish I could wrap it, but I'm using wordpress to generate the pagination, and I don't posses the skills to figure out how to wrap their markup in the divs I want to better target them, but I appreciate your answer and I do understand it so next time, in a scenario where I can control how to write my divs, I will not make this mistake. – Darren Bachan Jun 01 '16 at 23:50
  • @DarrenBachan if you want I can update my answer to make it wrap it dynamically using jQuery – dippas Jun 01 '16 at 23:52
  • That seems like overkill, well only because after seeing how flexbox works I thought this would be a really good time to try to learn and implement some of it. Part of me is curious though, so you could add in divs through jQuery? I feel like the right way would be to modify the wp theme somehow. – Darren Bachan Jun 01 '16 at 23:56
  • yes you could add elements/classes with jQuery, but you're right you should look into your theme ;) – dippas Jun 02 '16 at 00:17