7

I have a div with only pagination related content within it. What is the best element/tag to use?

I looked into <nav> but it is not suited as pagination is not major navigation.

<div class="paginationBar">
    <button class="previous">Previous</button>
          <button>1</button>
          <button>2</button>
          <button>3</button>
    <button class="next">Next</button>
</div>

The outer div to me seems like it could be aside since it is related to the main section and is outside of an article element.

Any other suggestions to improve / fix my code are very welcome.

Mark Swardstrom
  • 17,217
  • 6
  • 62
  • 70
DominicM
  • 6,520
  • 13
  • 39
  • 60
  • 2
    Well, in my personal opinion, unless this paginated content is not the main content, `` _is_ the preferred one... And links instead of buttons, and there's no reason for someElement because you can add nodes any place you like between nodes with basic javascript... – Wrikken Dec 11 '12 at 18:44

5 Answers5

18

nav is the correct container if the pagination is the main way to navigate content in that ancestor sectioning element (not the whole page!). See my answer (to a similar question) with some examples.

Using aside for the pagination is probably not the right element. However, it might be correct to use nav inside aside, if it is the major navigation for that aside content.

You should use a instead of button, as the pagination (as the name implies) leads to a different page/URL. Then you could use the rel values next and prev for the corresponding pagination links. If you insist on using button but you still have separate pages, you could use the link element to provide these rel values.

Community
  • 1
  • 1
unor
  • 92,415
  • 26
  • 211
  • 360
  • What disadvantages are there when not using links? Problem is that I have no url to provide for href so I don't see the point. It's all ajax/jquery and since I use ALOT of filters/parameters in my queries there could be several million url combinations if I tried to generate urls... – DominicM Dec 14 '12 at 20:06
  • 3
    Well, if all your "pages" are on the same URL, visitors without JS and therefor all search engine bots can't visit/link/bookmark/crawl the other "pages". So your choice (`button` instead of `a`) would be correct (and so you can't use the `rel` values `prev`/`next`). – unor Dec 15 '12 at 01:00
8

I think you don't need that element at all. Your buttons belong together and should stay together, like

<div class="paginationBar">
    <button class="previous">Previous</button>
    <button>1</button>
    <button>2</button>
    <button>3</button>
    <button class="next">Next</button>
</div>

It's pretty easy to insert the dynamic buttons where you want them either on the back-end or front-end ( jQuery's .insertAfter() for example )


In case you don't have to support some old IE you can get rid of classes on your buttons as well and use :first-child - :last-child to style those:

<div class="paginationBar">
    <button>Previous</button>
    <button>1</button>
    <button>2</button>
    <button>3</button>
    <button>Next</button>
</div>

UPDATE

Talking about the button container the most appropriate tag is probably the <nav>.

Other possible uses of <nav>

  1. Table of Contents
  2. Previous/next buttons (or pagination)
  3. Search form
  4. Breadcrumbs

SOURCE

Zoltan Toth
  • 46,981
  • 12
  • 120
  • 134
  • you are very correct out insertAfter(), q edited. I need no support for old browsers so that's no issue. Reason I put in buttons is because they wount link to any url(js function instead). Also the elements will be transformed into buttons by jquery ui anyways... – DominicM Dec 11 '12 at 18:50
  • My question is more about the first div and whether it would be more semantically correct to use aside? – DominicM Dec 11 '12 at 18:52
  • I guess there is really no right answer here, but I think I will go with nav since I will only have 1 more nav on any page... Thanks. – DominicM Dec 12 '12 at 22:57
  • 2
    Here is a decent [article](http://coding.smashingmagazine.com/2011/11/11/our-pointless-pursuit-of-semantic-value/) about why don't treat semantics with overly importance. It's good to have, of course, but nothing catastrophic will happen if you'll have a `
    ` instead of let's say an `
    – Zoltan Toth Dec 12 '12 at 23:09
4

As others have pointed out, the best way is to use <nav> with anchor tags. Here is an example with 5 pages where the current page number is 3:

<nav>
  <ul class="pagination">
    <li><a href="?page=1" rel="first">First</a></li>
    <li><a href="?page=2" rel="prev">Previous</a></li>

    <li><a href="?page=1">1</a></li>
    <li><a href="?page=2">2</a></li>
    <li class="active"><a href="?page=3">3</a></li>
    <li><a href="?page=4">4</a></li>
    <li><a href="?page=5">5</a></li>

    <li><a href="?page=4" rel="next">Next</a></li>
    <li><a href="?page=5" rel="last">Last</a></li>
  </ul>
</nav>

The reason the anchor tags are so important is because the rel attribute makes your pages crawlable.

Also note that if your environment has bootstrap.css loaded then it will automatically recognize the pagination and active CSS classes. You should provide your own styles for these classes as a fallback in case bootstrap.css is not loaded. The result should look something like this:

simple pager

Now you can use JavaScript to intercept the <a> element's click event and fetch the next page using whatever technique you prefer.

jackvsworld
  • 1,453
  • 15
  • 17
0

Please have a look at the XHTML Vocabulary. It includes link relation types for next, prev etc.

edit: link relation types in HTML 5

zazi
  • 686
  • 1
  • 7
  • 19
  • 1
    What does XHTML have anything to do with my question? – DominicM Dec 12 '12 at 22:58
  • Sorry, but you asked for "What is the best element/tag to use?" and you asked for semantic markup to identify a next page for pagination. so please re-think my answer! Just a small hint: HTML in itself is only for structure not semantics! – zazi Dec 14 '12 at 10:06
  • 1
    Question was about HTML5 not XHTML. Even you updated link has nothing to do with the question. It has no mention of pagination. – DominicM Dec 14 '12 at 19:54
  • http://www.w3.org/TR/html5/links.html#link-type-next and http://www.w3.org/TR/html5/links.html#link-type-prev leads you the link relations types "next" and "prev" which @unor is talking about as well. – zazi Dec 15 '12 at 11:09
  • Yes but I did not use links, I used buttons. Also the question was what element is best for the pagination container (answer is nav). – DominicM Dec 15 '12 at 16:47
0
<ol class="paginationBar">
    <li><button class="previous">Previous</button></li>
          <li><button>1</button></li>
          <li><button>2</button></li>
          <li><button>3</button></li>
    <li><button class="next">Next</button></li>
</ol>

That helps a bit

Hikari
  • 3,797
  • 12
  • 47
  • 77