Home > Article > Web Front-end > How to Show Hidden Child Elements While Their Parent is Hidden?
Showing Hidden Child Elements
In certain situations, you may want to display a child element even when its parent element is hidden using CSS display: none. This can be useful for scenarios such as displaying validation errors for hidden fields.
However, it's important to note that this behavior is not inherently supported by CSS. When a parent element is hidden, it effectively removes both the parent and its children from the visual DOM.
Possible Solution Using Visibility
If using display: none is not essential, an alternative approach is to use CSS visibility: hidden instead. This property hides the contents of an element, including its children, but still reserves their place in the layout.
By using visibility: hidden, you can hide the parent element while making the desired child element visible. This technique effectively ignores the hidden parent, allowing the child element to be displayed.
Code Example
In the provided JSFiddle example:
<code class="html"><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></code>
<code class="css">.hide { visibility: hidden; } .reshow { visibility: visible; }</code>
In this case, visibility: hidden hides the parent element with class hide, but the child element with class reshow remains visible. This allows the validation error message to be displayed even though the parent field is hidden.
Remember that this approach preserves the parent element's space in the layout, which may not be ideal in all scenarios.
The above is the detailed content of How to Show Hidden Child Elements While Their Parent is Hidden?. For more information, please follow other related articles on the PHP Chinese website!