173

I have some code like this:

function switch_tabs(obj) {
    $('.tab-content').hide();
    $('.tabs a').removeClass("selected");

    var id = obj.attr("rel");
    $('#' + id).show();
    obj.addClass("selected");
}

The show function adds display:block. But I would like to add display:inline-block instead of block.

2
  • 1
    You'll need to provide some more details. It works for me in a quick test: jsfiddle.net/DD3Sf . Commented Feb 13, 2012 at 11:55
  • @gijs Sorry its working now. I think its some cache problem. Thanks for your time Commented Feb 13, 2012 at 12:10

13 Answers 13

228

Instead of show, try to use CSS to hide and show the content.

function switch_tabs(obj) {
    $('.tab-content').css('display', 'none'); // you could still use `.hide()` here
    $('.tabs a').removeClass("selected");
    var id = obj.attr("rel");

    $('#' + id).css('display', 'inline-block');
    obj.addClass("selected");
}
Sign up to request clarification or add additional context in comments.

4 Comments

This might seem obvious, but if you still want to utilize the timing aspect of show ($("#id").show(500)), just append the css function to it: $("#id").show(500).css("display", "inline-block");
I would take it a step further and take care of all the hiding/showing using CSS classes, not jQuery. $('.tab-content').addClass('hidden'); (...) $('#' + id).removeClass('hidden'); In the CSS: .hidden { display: none !important }
Even though this is the accepted answer but it doesn't reflect what's stated in jquery docs: This is roughly equivalent to calling .css( "display", "block" ), except that the display property is restored to whatever it was initially. If an element has a display value of inline, then is hidden and shown, it will once again be displayed inline. So to have a default display you need to add a class in your css with display:inline-block; then call hide() and show() using the element's id. This is cleaner and more predictable code and easier to style.
@AbdullahAdeeb the problem is you would need to hide() by js on page load. Setting #element{display:inline-block;display:none;} doesn't do. I think the cleanest solution is $('#element').fadeIn().addClass('displaytype'); - better than answer above, because you can set different .displaytypes in css and have one showing function in js. See my answer here: stackoverflow.com/questions/1091322/…
39

Setting the CSS property after you have used .show() should work. Maybe you are targeting the wrong element on your HTML page.

 $('#foo').css('display', 'inline-block');

But if you are not using any effects of .show(), .hide() why don't you set those CSS properties manually like:

$('#foo').css('display','none'); 
$('#foo').css('display','inline-block');

2 Comments

Note that jquery show/hide/toggle restores previous display values. So if you hide it with jquery, calling show will work.
"Setting the CSS property after you have used .show() should work." Whats the point of using show() if you are just going to manually update the css anyway?
11

Use css() just after show() or fadeIn() like this:

$('div.className').fadeIn().css('display', 'inline-block');

Comments

5

I did that

function showPanels()  {
    $('.panels').show("slow");
    $('.panels').css('display','inline-block');
}

works like a charm.

1 Comment

Is that an answer or an acknowledgement..?
5

Razz's solution would work for the .hide() and .show() methods but would not work for the .toggle() method.

Depending upon the scenario, having a css class .inline_block { display: inline-block; } and calling $(element).toggleClass('inline_block') solves the problem for me.

Comments

4

try this:

$('#foo').show(0).css('display','inline-block');

2 Comments

The show() in this is pointless. This is the same as $('#foo').css('display','inline-block');
@Liam Not pointless. You can change the 0 to ease in, or modify this to gain access to other unique show options.
2

You can use animate insted of show/hide

Something like this:

function switch_tabs(obj)
{
    $('.tab-content').animate({opacity:0},3000);
    $('.tabs a').removeClass("selected");
    var id = obj.attr("rel");

    $('#'+id).animate({opacity:1},3000);
    obj.addClass("selected");
}

Comments

2

I think you want both the animation and to set the display property at the end. In that case you better use show() callback as shown below

$("#my_obj").show(400,function() {
    $("#my_obj").css("display","inline-block")
}) ;

This way you will achieve both the results.

1 Comment

This is not correct, you'd end up with the inline-block mode only after the whole animation ran. That means it would "pop" between block and inline-block after the 400ms.
2
<style>
.demo-ele{display:inline-block}
</style>

<div class="demo-ele" style="display:none">...</div>

<script>
$(".demo-ele").show(1000);//hide first, show with inline-block
<script>

Comments

1

actually jQuery simply clears the value of the 'display' property, and doesn't set it to 'block' (see internal implementation of jQuery.showHide()) -

   function showHide(elements, show) {
    var display, elem, hidden,

...

         if (show) {
            // Reset the inline display of this element to learn if it is
            // being hidden by cascaded rules or not
            if (!values[index] && display === "none") {
                elem.style.display = "";
            }

...

        if (!show || elem.style.display === "none" || elem.style.display === "") {
            elem.style.display = show ? values[index] || "" : "none";
        }
    }

Please note that you can override $.fn.show()/$.fn.hide(); storing original display in element itself when hiding (e.g. as an attribute or in the $.data()); and then applying it back again when showing.

Also, using css important! will probably not work here - since setting a style inline is usually stronger than any other rule

Comments

0

The best .let it's parent display :inline-block or add a parent div what CSS only have display :inline-block.

Comments

0

add a css property to the element

#tab {
    display:inline-block;
}

in the JS, add and remove "hide" class

$("#" + id).addClass("hide");
$("#" + id).removeClass("hide");

Comments

-5

Best way is to add !important suffix to the selector .

Example:

 #selector{
     display: inline-block !important;                   
}

3 Comments

doesn't this prevent .hide() ?
!important is only necessary in edge cases when there is no other option. Not the right way to approach this problem in my opinion when it can be fixed with better jQuery code.
The important thing to remember about !important is that it's important to not use it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.