- Hide an element if the next one is empty
- 8 Answers 8
- CSS
- HTML
- Hiding empty elements with CSS :empty and :has()
- Hiding an empty element
- Hiding an element that has an empty child
- Hide div, if the div is empty
- CSS — Hide element if child is empty [duplicate]
- 2 Answers 2
- Hide a whole div with CSS with part of it is empty
- 3 Answers 3
Hide an element if the next one is empty
I would like to hide the h3 element when the div is empty. I’m willing to change the html structure but the h3 has to be outside of the div because its content is dynamically changed. Is there a way to do that in CSS ?
No. CSS has no way of selecting a previous sibling of an element. At all. Despite the number of people that claim CSS 4 will allow it (despite clear statements to the contrary, ref: dev.w3.org/csswg/selectors4/#profiles).
8 Answers 8
There is no syntax to select a parent element or any other non-child element from #divid . You can select #divid if it’s empty, by #divid:empty , but there is no way you can select .hideIfDivIsEmpty in any browser by selecting that. According to this question there is such a thing in CSS4 (specs), but it is not supported in any browser as of now and according to the same specs, the selector is too slow to be implemented in browsers.
See the other answers for the javascript solution to this problem.
Please, read the specifaction you linked to; specifically 2.1. Fast vs Complete Selector Profiles. Yes, the Selectors API has*/*may have a subject pseudo-class; but it won’t be available (as currently written) in browser CSS.
Thanks. I’ve altered the answer to reflect that. Had to read and re-read it a half dozen of times though.
well wait you are going to get some very good answers here. but my solution would be make a css class then assign it to both the h3 and div tags then using jquery selectors get both of them using the css class. Now you will get an arry of tags if the the element at index 1’s innertext = null or empty then the element at index 0 should hide. i hope this will help
I don’t think that you can do it with CSS.
var divs = $(".hideIfDivEmpty"); divs.each(function () < var div = $(this); if (div.next().html() === "") < div.hide(); >>);
And like @Prinzhorn correctly said: there is a liner solution:
$('h3.hideIfDivEmpty + div:empty').prev().hide();
@Prinzhorn unfortunately that didn’t work. However, it seems the one-liner is becoming as long as the standard version which is as easy as tacking on .trim() .
Add your label using the ::before css selector.
Hide your label for empty/null values using the :empty selector
/* HIDE IF EMPTY*/ .label:empty < display: none; >/* LABEL STYLES */ .label::before < font-weight:bold; >/* LABEL VALUES */ .l_colour::before < content:"Colour: "; >.l_size::before < content: "Size: "; >.l_shape::before
This problem can only be solved client-side with JavaScript (or one of its libraries). With plain JavaScript, I’d suggest:
function hideIfNextEmpty(el) < var text = 'textContent' in document ? 'textContent' : 'innerText'; if (el.nextElementSibling[text].replace(/\s/g,'').length === 0) < el.style.display = 'none'; >> hideIfNextEmpty(document.querySelector('h3.hideIfDivEmpty'));
A CSS-only version would not have very good browser support. It would involve putting the header tag after the content, followed by manipulating the positioning of the elements.
Here’s a very hacked together CSS-only solution. IE 9+. You should do this using JavaScript instead as others have suggested.
CSS
article p:empty + header < display: none; >article p:empty < margin: 0; >article p < float:left; >article header < position: absolute; top: 0; >article header h1 < margin: 0; >article > p:first-of-type:not(:empty) < padding-top: 1em; >article < position: relative; >/* include clearfix */
HTML
Hidden article
Cras mattis consectetur purus sit amet fermentum. Maecenas faucibus mollis interdum. Cras mattis consectetur purus sit amet fermentum. Integer posuere erat a ante venenatis dapibus posuere velit aliquet. Cras mattis consectetur purus sit amet fermentum. Nulla vitae elit libero, a pharetra augue.
Porta Malesuada
Hiding empty elements with CSS :empty and :has()
You might be used to adding and removing .open and .closed classes on div s and containers to control their state styles. What if we could just write CSS that reflected the state of the DOM, without having to additionally handle state by toggling classes? In this post, we’ll explore how to hide elements with the :empty pseudo-class if they have no children, and how to make the pattern more granular and practical when we combine it with :has() and :not() .
Hiding an empty element
The :empty matches any element that has no children. The pseudo-class is supported by all major browsers, and is safe to use even if you’re targeting Internet Explorer. We can use it in combination with the display property to hide an element if it’s empty:
.container:empty display: none; >
In this example, the :empty pseudo-class is used to select elements with the class .container that have no children (including text nodes), and the display: none rule is used to hide them.
class="container">Some text class="container">