Andiih
Andiih

Reputation: 12413

Display HTML child element when parent element is display:none

Is there any mechanism to display a child element when its parent element is display: none?

The situation is a validation error on a hidden tab. I want to surface the error message, even though the field is hidden.

A really simplified JSFiddle of the situation is here http://jsfiddle.net/vLYnk/

Markup:

<ul>
    <li>One</li>
    <li class="hide">
        Two
        <ul>
            <li class="reshow">Re Show Me</li>
            <li>Inner 2</li>
        </ul>
    </li>
    <li>Three</li>
</ul>

CSS:

.hide {display: none}
.reshow {display: block !important; position: fixed; top: 0; left: 0;}

I'm guessing this is impossible as the child would have no context, but just in case???

Upvotes: 70

Views: 80990

Answers (6)

Just add .hide:

font-size: 0;
margin: 0;
padding: 0;

It will not occupy the space just as display none

Upvotes: 4

Maciej
Maciej

Reputation: 126

For specific situations you can use this either:

.hidden-container *{display:none;}
.hidden-container .show-again{display:block}

That will keep .hidden-container displayed, but everything except .show-again container will have display property set to none.

jsFiddle

EDIT:

note, that it'll reset all display properties in childs of .hidden-container if declared after their styles.

Upvotes: 4

KP.
KP.

Reputation: 13730

No this isn't possible. display:none hides the element, and thus any child elements will not be displayed.

EDIT:

This might be along the lines of what you want, if you're able to switch from using display to visibility.

.hide {visibility: hidden;}
.reshow {visibility: visible;}

Using this method you can hide the contents of the parent element but show the specific <li> you want. The only caveat is the parent markup will still take up screen real estate. Just won't be shown to the user. That may or may not break the layout you're looking for.

http://jsfiddle.net/vLYnk/2/

Upvotes: 70

dave
dave

Reputation: 4104

You could instead of using display: none; to hide your element move it out of the viewport via position: relative/absolute; and left: -9999em; Than give the visible child a position: relative; and left: 9999em;

The downfall of this solution is, that the "reshown" element is out of the element flow if you used position: absolute. (Not occupying the space it needs and not pushing down following elements) Or that you occupy more space than is actually needed, when using position: relative.

http://jsfiddle.net/vLYnk/3/

Upvotes: 4

Louis Ricci
Louis Ricci

Reputation: 21086

$('body').append($('.reshow').clone(true));

Upvotes: 2

feeela
feeela

Reputation: 29922

No, this is not possible. You could instead move the child element out of its hidden parent and insert it somewhere else in the markup (e.g. via JavaScript).

Upvotes: 5

Related Questions