2

I would like to use jQuery UI tabs but I need the tabs aligned right … That's "easy" since I can modify the tabs container class and extend it.

But the thing is I want to add a "title" on the left, as shown in this screenshot: http://cl.ly/400D0E3z0f272h1B3x3R

How can I do it in a clean way ?

(A dirty way could be to prepend/append a div to the tabs tag, adding the DOM on the fly … I'm looking a cleaner way :)

Thank you in advance

FlamingMoe
  • 2,709
  • 5
  • 39
  • 64

1 Answers1

5

First there is nothing dirty adding elements to the dom on the fly :-)

Secondly, you could simply add an element in the markup, for instance a <h3> (let's be semantic (and assume you got other titles before)):

<div id="tabs">
    <h3 class="ui-tab-title">My Title</h3>
    <ul>
        <li><a href="#tabs-1">Nunc tincidunt</a></li>
        <li><a href="#tabs-2">Proin dolor</a></li>
        <li><a href="#tabs-3">Aenean lacinia</a></li>
    </ul>
    <div id="tabs-1">
    ...
</div>

and position it with css:

/* float tab buttons to right */
.ui-tabs .ui-tabs-nav li { float: right !important; }

/* position:relative on container will make the title position:absolute relative to the container */
#tabs { position: relative; }

/* absolute position the title */
.ui-tab-title { position: absolute; left: 20px; top: 15px; }

Here's a jsfiddle to illustrate


Edit:

As you pointed out, floating right the <li> inverts their order.

You could invert the order of the list items in the markup itself but this will mess up the whole logic.

Here's a piece of css to right align the tab button while keeping the markup and the visual order in place:

/* align right the <ul> container */
.ui-tabs .ui-tabs-nav { height: 2.35em; text-align: right; }

/* jquery ui css floats-left the <li> so un-float them */
.ui-tabs .ui-tabs-nav li { display: inline-block; float: none; }

I've changed the fiddle accordingly.

Didier Ghys
  • 30,396
  • 9
  • 75
  • 81
  • I've had a discussion with a developer because he told me it's not right to invert the order of the tabs, because "the semantic". In our case we are working in a BACKOFFICE (it will never be public, therefore it does not need SEO) ... do you think it's so critical to invert the order of the tabs if they are being generated and sent to the browser just once (no ajax) as a one big HTML block ? – FlamingMoe Dec 14 '11 at 11:05
  • Indeed. Using `float: right` *visually inverts* the order of the tabs. Quick solution is to invert the `
  • ` in the markup **but** it will also invert the `index` of your tabs: "Aenean lacinia" will become the first tab. You can set the active tab when initializing the plugin but this might end being a bit confusing. I'll think of another way to right-align the tabs.
  • – Didier Ghys Dec 14 '11 at 11:18
  • I can not think in that "inversion" of content as a problem ... can you tell me in which cases you (as a developer) need to have the HTML blocks of tabs in the same order of the tab labels ? I mean, with your solution (in HTML source code) the li's will be tab3, tab2, tab1, and maybe the tab content divs will be tab1, tab2, tab3 ... so WHAT? ;) (that's my question) ... does it really matter ? (again, you don't need SEO features, it's an internal backoffice). – FlamingMoe Dec 14 '11 at 11:32
  • NOTE: We are using a Yii framework module that builds the HTML of jqueryUI-Tabs automatically, so in our case the order of li's is inverted (for the float:right feature) but the contents divs order is normal... but ... again ... so what? :) – FlamingMoe Dec 14 '11 at 11:35
  • Thank you for the edit !!! but ... I'm still thinking in the "problem" of having the content in the source code inverted :P (just for the discussion I'm having here in the office :) – FlamingMoe Dec 14 '11 at 11:37
  • Everything will still work with using float-right and li's being tab3,tab2,tab1 and div tab1,tab2,tab3. The only thing is that your tab-index 0 will point to tab3 and not to tab1 as *expected*. It inverts the logic a little bit (illutrsated in this [jsfiddle](http://jsfiddle.net/didierg/Nxsca/)). – Didier Ghys Dec 14 '11 at 11:40
  • Yes, I got that ... that would be a problem to operate with tab indexes, but ... if you have to put the li's in tab3,tab2,tab1 (being "tab3" the first name reading from left to right, "Aenean lacinia" in your example), and the content divs are also in that order "tab3, tab2, tab1" ... it does not matter that the CONTENT of the first div for human reading ("Aenean lacinia") is at the end of the source code ... that's the discussion with my partner ... I would find a problem there because of the SEO, but we don't need SEO here ... and what if the content of the "first tab" is at the end? :P – FlamingMoe Dec 14 '11 at 11:51
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/5845/discussion-between-user311188-and-didier-g) – FlamingMoe Dec 14 '11 at 11:52
  • The place of the content div in the markup does not matter. The system *finds* the content div with the ID (`li > a href="#tab-3"` + `div id="tab-3"`) and all div are hidden except the one for the active tab. – Didier Ghys Dec 14 '11 at 11:56