67

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???

6 Answers 6

69

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/

1
  • 1
    @Andiih Ok - I've updated the answer with an alternate solution using visibility rather than display CSS attribute.
    – KP.
    Commented Oct 18, 2012 at 14:42
5

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).

1
  • 2
    that was what I thought...was just hoping I didn't have to go that route.
    – Andiih
    Commented Oct 18, 2012 at 14:31
4

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.

3
  • 4
    Sorry, this answer is misleading because it only works if you have no nesting for child elements. It works only for hiding siblings of the element, otherwise it doesn't work: jsfiddle.net/jyKSu/1
    – AaronLS
    Commented Nov 17, 2014 at 21:39
  • Though I like this answer, I've heard using * in CSS can really slow down a mobile browser. I wonder the effects of using it as dependent?
    – Jason
    Commented Feb 21, 2017 at 17:03
  • * is not too slow if you have an exact parent element.
    – PRMan
    Commented Sep 26, 2022 at 22:30
4

Just add .hide:

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

It will not occupy the space just as display none

1
  • This works great, but only if the element to be hidden is text.
    – scott8035
    Commented Feb 20, 2021 at 18:29
3

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/

1
$('body').append($('.reshow').clone(true));
1
  • The validation message is generated client side, so I'd need to hook into some event to fire this...already thinking down these lines.
    – Andiih
    Commented Oct 18, 2012 at 14:33

Not the answer you're looking for? Browse other questions tagged or ask your own question.